aboutsummaryrefslogtreecommitdiff
path: root/src/system_wrappers/source/file_impl.cc
blob: 6046c2c77c3721719ac17f9ac86d397724100b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "file_impl.h"

#include <cassert>

#ifdef _WIN32
    #include <Windows.h>
#else
    #include <stdarg.h>
    #include <string.h>
#endif

namespace webrtc {
FileWrapper* FileWrapper::Create()
{
    return new FileWrapperImpl();
}

FileWrapperImpl::FileWrapperImpl()
    : _id(NULL),
      _open(false),
      _looping(false),
      _readOnly(false),
      _text(false),
      _maxSizeInBytes(-1),
      _sizeInBytes(0)
{
    memset(_fileNameUTF8, 0, kMaxFileNameSize);
}

FileWrapperImpl::~FileWrapperImpl()
{
    if (_id != NULL)
    {
        fclose(_id);
    }
}

WebRtc_Word32 FileWrapperImpl::CloseFile()
{
    if (_id != NULL)
    {
        fclose(_id);
        _id = NULL;
    }
    memset(_fileNameUTF8, 0, kMaxFileNameSize);
    _open = false;
    return 0;
}

int FileWrapperImpl::Rewind()
{
    if(_looping || !_readOnly)
    {
        if (_id != NULL)
        {
            _sizeInBytes = 0;
            return fseek(_id, 0, SEEK_SET);
        }
    }
    return -1;
}

WebRtc_Word32 FileWrapperImpl::SetMaxFileSize(WebRtc_Word32 bytes)
{
    _maxSizeInBytes = bytes;
    return 0;
}

WebRtc_Word32 FileWrapperImpl::Flush()
{
    if (_id != NULL)
    {
        return fflush(_id);
    }
    return -1;
}

WebRtc_Word32 FileWrapperImpl::FileName(WebRtc_Word8* fileNameUTF8,
                                        WebRtc_UWord32 size) const
{
    WebRtc_Word32 len = static_cast<WebRtc_Word32>(strlen(_fileNameUTF8));
    if(len > kMaxFileNameSize)
    {
        assert(false);
        return -1;
    }
    if(len < 1)
    {
        return -1;
    }
    // Make sure to NULL terminate
    if(size < (WebRtc_UWord32)len)
    {
        len = size - 1;
    }
    memcpy(fileNameUTF8, _fileNameUTF8, len);
    fileNameUTF8[len] = 0;
    return 0;
}

bool
FileWrapperImpl::Open() const
{
    return _open;
}

WebRtc_Word32 FileWrapperImpl::OpenFile(const WebRtc_Word8 *fileNameUTF8,
                                        const bool readOnly, const bool loop,
                                        const bool text)
{
    WebRtc_Word32 length = (WebRtc_Word32)strlen(fileNameUTF8);
    if (length > kMaxFileNameSize)
    {
        return -1;
    }

    _readOnly = readOnly;

    FILE *tmpId = NULL;
#if defined _WIN32
    wchar_t wideFileName[kMaxFileNameSize];
    wideFileName[0] = 0;

    MultiByteToWideChar(CP_UTF8,
                        0 /*UTF8 flag*/,
                        fileNameUTF8,
                        -1 /*Null terminated string*/,
                        wideFileName,
                        kMaxFileNameSize);
    if(text)
    {
        if(readOnly)
        {
            tmpId = _wfopen(wideFileName, L"rt");
        } else {
            tmpId = _wfopen(wideFileName, L"wt");
        }
    } else {
        if(readOnly)
        {
            tmpId = _wfopen(wideFileName, L"rb");
        } else {
            tmpId = _wfopen(wideFileName, L"wb");
        }
    }
#else
    if(text)
    {
        if(readOnly)
        {
            tmpId = fopen(fileNameUTF8, "rt");
        } else {
            tmpId = fopen(fileNameUTF8, "wt");
        }
    } else {
        if(readOnly)
        {
            tmpId = fopen(fileNameUTF8, "rb");
        } else {
            tmpId = fopen(fileNameUTF8, "wb");
        }
    }
#endif

    if (tmpId != NULL)
    {
        // + 1 comes fro copying the NULL termination charachter too
        memcpy(_fileNameUTF8, fileNameUTF8, length + 1);
        if (_id != NULL)
        {
            fclose(_id);
        }
        _id = tmpId;
        _looping = loop;
        _open = true;
        return 0;
    }
    return -1;
}

int FileWrapperImpl::Read(void *buf, int len)
{
    if(len < 0)
    {
        return 0;
    }
    if (_id != NULL)
    {
        WebRtc_Word32 res = static_cast<WebRtc_Word32>(fread(buf, 1, len, _id));
        if (res != len)
        {
            if(!_looping)
            {
                CloseFile();
            }
        }
        return res;
    }
    return -1;
}

WebRtc_Word32 FileWrapperImpl::WriteText(const WebRtc_Word8* text, ...)
{
    assert(!_readOnly);
    assert(!_text);

    if (_id == NULL)
    {
        return -1;
    }

    char tempBuff[kFileMaxTextMessageSize];
    if (text)
    {
        va_list args;
        va_start(args, text);
#ifdef _WIN32
        _vsnprintf(tempBuff, kFileMaxTextMessageSize-1, text, args);
#else
        vsnprintf(tempBuff, kFileMaxTextMessageSize-1, text, args);
#endif
        va_end(args);
        WebRtc_Word32 nBytes;
        nBytes = fprintf(_id, "%s", tempBuff);
        if (nBytes > 0)
        {
            return 0;
        }
        CloseFile();
    }
    return -1;
}

bool FileWrapperImpl::Write(const void* buf, int len)
{
    assert(!_readOnly);
    if (_id != NULL)
    {
        // Check if it's time to stop writing.
        if ((_maxSizeInBytes != -1) &&
             _sizeInBytes + len > (WebRtc_UWord32)_maxSizeInBytes)
        {
            Flush();
            return false;
        }

        size_t nBytes = fwrite((WebRtc_UWord8*)buf, 1, len, _id);
        if (nBytes > 0)
        {
            _sizeInBytes += static_cast<WebRtc_Word32>(nBytes);
            return true;
        }
        CloseFile();
    }
    return false;
}
} // namespace webrtc