1/*---------------------------------------------------------------------------*
2 *  PFileWrap.c  *
3 *                                                                           *
4 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
5 *                                                                           *
6 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
7 *  you may not use this file except in compliance with the License.         *
8 *                                                                           *
9 *  You may obtain a copy of the License at                                  *
10 *      http://www.apache.org/licenses/LICENSE-2.0                           *
11 *                                                                           *
12 *  Unless required by applicable law or agreed to in writing, software      *
13 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 *  See the License for the specific language governing permissions and      *
16 *  limitations under the License.                                           *
17 *                                                                           *
18 *---------------------------------------------------------------------------*/
19
20#include <stdio.h>
21#include <stdarg.h>
22#include "LCHAR.h"
23#include "pendian.h"
24#include "PFile.h"
25#include "plog.h"
26#include "pstdio.h"
27#include "ptypes.h"
28
29
30
31
32ESR_ReturnCode PFileClose( PFile *self )
33    {
34
35    fclose ( (FILE *)self );
36    return ( ESR_SUCCESS );
37    }
38
39
40
41ESR_ReturnCode PFileRead ( PFile *self, void *buffer, size_t size, size_t *count )
42    {
43    ESR_ReturnCode  read_status;
44    size_t          items_read;
45    int             ferror_status;
46
47    items_read = fread ( buffer, size, *count, (FILE *)self );
48
49    if ( items_read > 0 )
50        {
51        read_status = ESR_SUCCESS;
52        *count = items_read;
53        }
54    else
55        {
56        ferror_status = ferror ( (FILE *)self );
57
58        if ( ferror_status == 0 )
59            {
60            read_status = ESR_SUCCESS;
61            *count = items_read;
62            }
63        else
64            {
65            read_status = ESR_READ_ERROR;
66            }
67        }
68    return ( read_status );
69    }
70
71
72
73ESR_ReturnCode PFileWrite ( PFile *self, const void *buffer, size_t size, size_t *count )
74    {
75    ESR_ReturnCode  write_status;
76    size_t          items_written;
77
78    items_written = fwrite ( buffer, size, *count, (FILE *)self );
79
80    if ( items_written == ( *count ) )
81        {
82        write_status = ESR_SUCCESS;
83        *count = items_written;
84        }
85    else
86        {
87        write_status = ESR_READ_ERROR;
88        }
89    return ( write_status );
90    }
91
92
93
94ESR_ReturnCode PFileFlush ( PFile *self )
95    {
96    ESR_ReturnCode  flush_status;
97    size_t          flush_ok;
98
99    flush_ok = fflush ( (FILE *)self );
100
101    if ( flush_ok == 0 )
102        {
103        flush_status = ESR_SUCCESS;
104        }
105    else
106        {
107        flush_status = ESR_FLUSH_ERROR;
108        }
109    return ( flush_status );
110    }
111
112
113
114ESR_ReturnCode PFileSeek ( PFile *self, long offset, int origin )
115    {
116    ESR_ReturnCode  seek_status;
117    size_t          seek_ok;
118
119    seek_ok = fseek ( (FILE *)self, offset, origin );
120
121    if ( seek_ok == 0 )
122        {
123        seek_status = ESR_SUCCESS;
124        }
125    else
126        {
127        seek_status = ESR_SEEK_ERROR;
128        }
129    return ( seek_status );
130    }
131
132
133
134ESR_ReturnCode PFileGetPosition ( PFile *self, size_t *position )
135    {
136    ESR_ReturnCode  get_status;
137    long            ftell_result;
138
139    ftell_result = ftell ( (FILE *)self );
140
141    if ( ftell_result >= 0 )
142        {
143	*position = (size_t)ftell_result;
144        get_status = ESR_SUCCESS;
145        }
146    else
147        {
148        get_status = ESR_INVALID_STATE;
149        }
150    return ( get_status );
151    }
152
153
154
155ESR_ReturnCode PFileIsEOF ( PFile *self, ESR_BOOL *isEof )
156    {
157#ifdef NO_FEOF
158    long            posCur;    /* remember current file position */
159    long            posEnd;    /* end of file position */
160
161    posCur = ftell ( self );
162    fseek ( self, 0, SEEK_END );
163    posEnd = ftell ( self );
164
165    if ( posCur == posEnd )
166        *isEof = ESR_TRUE;
167    else
168        *isEof = ESR_FALSE;
169    fseek ( self, posCur, SEEK_SET );  /* restore position in file */
170#else
171    int             is_eof;
172
173    is_eof = feof ( (FILE *)self );
174
175    if ( is_eof != 0 )
176        *isEof = ESR_TRUE;
177    else
178        *isEof = ESR_FALSE;
179#endif
180    return ( ESR_SUCCESS );
181    }
182
183
184
185ESR_ReturnCode PFileIsErrorSet ( PFile *self, ESR_BOOL *isError )
186    {
187    int is_error;
188
189    is_error = ferror ( (FILE *)self );
190
191    if ( is_error != 0 )
192        *isError = ESR_TRUE;
193    else
194        *isError = ESR_FALSE;
195    return ( ESR_SUCCESS );
196    }
197
198
199
200ESR_ReturnCode PFileClearError ( PFile *self )
201    {
202
203    clearerr ( (FILE *)self );
204    return ( ESR_SUCCESS );
205    }
206
207
208
209ESR_ReturnCode PFileVfprintf ( PFile *self, int *result, const LCHAR *format, va_list args )
210    {
211    int bytes_printed;
212
213    bytes_printed = vfprintf ( (FILE *)self, format, args );
214
215    if ( result != NULL )
216        *result = bytes_printed;
217    return ( ESR_SUCCESS );
218    }
219
220
221
222ESR_ReturnCode PFileFgetc ( PFile *self, LINT *result )
223    {
224    ESR_ReturnCode  fgetc_status;
225    int             error_status;
226
227    *result = fgetc ( (FILE *)self );
228
229    if ( ( *result ) != EOF )
230        {
231        fgetc_status = ESR_SUCCESS;
232        }
233    else
234        {
235        error_status = ferror ( (FILE *)self );
236
237        if ( error_status == 0 )
238            fgetc_status = ESR_SUCCESS;
239        else
240            fgetc_status = ESR_INVALID_STATE;
241        }
242    return ( fgetc_status );
243    }
244
245
246
247ESR_ReturnCode PFileFgets ( PFile *self, LCHAR *string, int n, LCHAR **result )
248    {
249    ESR_ReturnCode  fgets_status;
250    int             error_status;
251    LCHAR           *temp;
252
253    temp = fgets ( string, n, (FILE *)self );
254
255    if ( temp != NULL )
256        {
257        fgets_status = ESR_SUCCESS;
258
259        if ( result != NULL )
260            *result = temp;
261        }
262    else
263        {
264        error_status = ferror ( (FILE *)self );
265
266        if ( error_status == 0 )
267            {
268            fgets_status = ESR_SUCCESS;
269
270            if ( result != NULL )
271                *result = NULL;
272            }
273        else
274            fgets_status = ESR_INVALID_STATE;
275        }
276    return ( fgets_status );
277    }
278
279
280
281PFile *pfopen ( const LCHAR *filename, const LCHAR *mode )
282    {
283    PFile           *result;
284
285    result = (PFile *)fopen ( filename, mode );
286    return ( result );
287    }
288
289
290
291size_t pfread ( void *buffer, size_t size, size_t count, PFile *stream )
292    {
293    ESR_ReturnCode rc;
294
295    rc = PFileRead ( stream, buffer, size, &count );
296
297    if ( rc != ESR_SUCCESS )
298        return ( 0 );
299    return ( count );
300    }
301
302
303
304size_t pfwrite ( const void *buffer, size_t size, size_t count, PFile *stream )
305    {
306    ESR_ReturnCode rc;
307
308    rc = PFileWrite ( stream, buffer, size, &count );
309    if ( rc != ESR_SUCCESS )
310        return ( 0 );
311    return ( count );
312    }
313
314
315
316int pfclose ( PFile *stream )
317    {
318
319    fclose ( (FILE *)stream );
320
321    return ( 0 );
322    }
323
324
325
326void prewind (PFile *stream)
327    {
328
329    PFileSeek ( stream, 0, SEEK_SET );
330    }
331
332
333
334int pfseek ( PFile *stream, long offset, int origin )
335    {
336    ESR_ReturnCode rc;
337
338    rc = PFileSeek ( stream, offset, origin );
339
340    if ( rc != ESR_SUCCESS )
341        return ( 1 );
342    return ( 0 );
343    }
344
345
346
347long pftell ( PFile *stream )
348    {
349    ESR_ReturnCode  rc;
350    size_t          result;
351
352    rc = PFileGetPosition ( stream, &result );
353
354    if ( rc != ESR_SUCCESS )
355        return ( -1 );
356    return ( result );
357    }
358
359
360
361int pfeof ( PFile *stream )
362    {
363    ESR_BOOL eof;
364
365    PFileIsEOF ( stream, &eof );
366
367    if ( ! eof )
368        return ( 0 );
369        return ( 1 );
370    }
371
372
373
374int pferror ( PFile *stream )
375    {
376    ESR_BOOL error;
377
378    PFileIsErrorSet ( stream, &error );
379
380    if ( ! error )
381        return ( 0 );
382    return ( 1 );
383    }
384
385
386
387void pclearerr ( PFile *stream )
388    {
389
390    PFileClearError ( stream );
391    }
392
393
394
395int pfflush ( PFile *stream )
396    {
397    ESR_ReturnCode rc;
398
399    rc = PFileFlush ( stream );
400
401    if ( rc != ESR_SUCCESS )
402        return ( PEOF );
403    return ( 0 );
404    }
405
406
407
408LCHAR* pfgets ( LCHAR *string, int n, PFile *self )
409    {
410    LCHAR           *result;
411    ESR_ReturnCode  rc;
412
413    rc = PFileFgets ( self, string, n, &result );
414
415    if ( rc != ESR_SUCCESS )
416        return ( NULL );
417    return ( result );
418    }
419
420
421
422LINT pfgetc ( PFile *self )
423    {
424    ESR_ReturnCode  rc;
425    LINT            result;
426
427    rc = PFileFgetc ( self, &result );
428
429    if ( rc != ESR_SUCCESS )
430        return ( PEOF );
431    return ( result );
432    }
433
434
435
436int pfprintf ( PFile *stream, const LCHAR *format, ... )
437    {
438    ESR_ReturnCode  rc;
439    int             result;
440    va_list         args;
441
442    va_start ( args, format );
443    rc = PFileVfprintf ( stream, &result, format, args );
444    va_end ( args );
445
446    if ( rc != ESR_SUCCESS )
447        return ( -1 );
448    return ( result );
449    }
450
451
452
453int pvfprintf ( PFile *stream, const LCHAR *format, va_list argptr )
454    {
455    ESR_ReturnCode  rc;
456    int             result;
457
458    rc = PFileVfprintf ( stream, &result, format, argptr );
459
460    if ( rc != ESR_SUCCESS )
461        return ( -1 );
462    return ( result );
463    }
464
465
466ESR_ReturnCode pf_convert_backslashes_to_forwardslashes ( LCHAR *string_to_convert )
467    {
468    ESR_ReturnCode  rc;
469    int             string_status;
470
471    if ( string_to_convert != NULL )
472        {
473	string_status = lstrreplace ( string_to_convert, L('\\'), L('/') );
474
475	if ( string_status == 0 )
476	    rc = ESR_SUCCESS;
477	else
478	    rc = ESR_INVALID_ARGUMENT;
479	}
480    else
481	{
482	rc = ESR_INVALID_ARGUMENT;
483	}
484    return ( rc );
485    }
486
487
488
489ESR_ReturnCode pf_is_path_absolute ( const LCHAR* input_path, ESR_BOOL* isAbsolute )
490    {
491    ESR_ReturnCode rc;
492    LCHAR path [P_PATH_MAX];
493
494    if ( isAbsolute != NULL )
495        {
496	LSTRCPY ( path, input_path );
497	rc = pf_convert_backslashes_to_forwardslashes ( path );
498
499	if ( rc == ESR_SUCCESS )
500	    {
501	    if ( ( path [0] == '/' ) || ( ( LISALPHA ( path [0] ) ) && ( path [1] == ':' ) && ( path [2] == '/' ) ) )
502		*isAbsolute = ESR_TRUE;
503	    else
504		*isAbsolute = ESR_FALSE;
505	    }
506	}
507    else
508	{
509	rc = ESR_INVALID_ARGUMENT;
510	}
511    return ( rc );
512    }
513
514