]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/filestrm.c
2 ******************************************************************************
4 * Copyright (C) 1997-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
13 * Modification History:
15 * Date Name Description
17 * 03/02/99 stephen Reordered params in ungetc to match stdio
19 * 3/29/99 helena Merged Stephen and Bertrand's changes.
21 ******************************************************************************
29 U_CAPI FileStream
* U_EXPORT2
30 T_FileStream_open(const char* filename
, const char* mode
)
32 if(filename
!= NULL
&& *filename
!= 0 && mode
!= NULL
&& *mode
!= 0) {
33 FILE *file
= fopen(filename
, mode
);
34 return (FileStream
*)file
;
41 U_CAPI FileStream* U_EXPORT2
42 T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode)
44 // TBD: _wfopen is believed to be MS-specific?
45 #if defined(WIN32) && !defined(__WINDOWS__)
46 FILE* result = _wfopen(filename, mode);
47 return (FileStream*)result;
49 size_t fnMbsSize, mdMbsSize;
53 // convert from wchar_t to char
54 fnMbsSize = wcstombs(NULL, filename, ((size_t)-1) >> 1);
55 fn = (char*)uprv_malloc(fnMbsSize+2);
56 wcstombs(fn, filename, fnMbsSize);
59 mdMbsSize = wcstombs(NULL, mode, ((size_t)-1) >> 1);
60 md = (char*)uprv_malloc(mdMbsSize+2);
61 wcstombs(md, mode, mdMbsSize);
64 result = fopen(fn, md);
67 return (FileStream*)result;
72 T_FileStream_close(FileStream
* fileStream
)
75 fclose((FILE*)fileStream
);
78 U_CAPI UBool U_EXPORT2
79 T_FileStream_file_exists(const char* filename
)
81 FILE* temp
= fopen(filename
, "r");
89 /*static const int32_t kEOF;
90 const int32_t FileStream::kEOF = EOF;*/
94 T_FileStream_tmpfile()
96 FILE* file = tmpfile();
97 return (FileStream*)file;
101 U_CAPI
int32_t U_EXPORT2
102 T_FileStream_read(FileStream
* fileStream
, void* addr
, int32_t len
)
104 return fread(addr
, 1, len
, (FILE*)fileStream
);
107 U_CAPI
int32_t U_EXPORT2
108 T_FileStream_write(FileStream
* fileStream
, const void* addr
, int32_t len
)
111 return fwrite(addr
, 1, len
, (FILE*)fileStream
);
114 U_CAPI
void U_EXPORT2
115 T_FileStream_rewind(FileStream
* fileStream
)
117 rewind((FILE*)fileStream
);
120 U_CAPI
int32_t U_EXPORT2
121 T_FileStream_putc(FileStream
* fileStream
, int32_t ch
)
123 int32_t c
= fputc(ch
, (FILE*)fileStream
);
128 T_FileStream_getc(FileStream
* fileStream
)
130 int c
= fgetc((FILE*)fileStream
);
134 U_CAPI
int32_t U_EXPORT2
135 T_FileStream_ungetc(int32_t ch
, FileStream
* fileStream
)
138 int32_t c
= ungetc(ch
, (FILE*)fileStream
);
142 U_CAPI
int32_t U_EXPORT2
143 T_FileStream_peek(FileStream
* fileStream
)
145 int32_t c
= fgetc((FILE*)fileStream
);
146 return ungetc(c
, (FILE*)fileStream
);
149 U_CAPI
char* U_EXPORT2
150 T_FileStream_readLine(FileStream
* fileStream
, char* buffer
, int32_t length
)
152 return fgets(buffer
, length
, (FILE*)fileStream
);
155 U_CAPI
int32_t U_EXPORT2
156 T_FileStream_writeLine(FileStream
* fileStream
, const char* buffer
)
158 return fputs(buffer
, (FILE*)fileStream
);
161 U_CAPI
int32_t U_EXPORT2
162 T_FileStream_size(FileStream
* fileStream
)
164 int32_t savedPos
= ftell((FILE*)fileStream
);
167 /*Changes by Bertrand A. D. doesn't affect the current position
168 goes to the end of the file before ftell*/
169 fseek((FILE*)fileStream
, 0, SEEK_END
);
170 size
= ftell((FILE*)fileStream
);
171 fseek((FILE*)fileStream
, savedPos
, SEEK_SET
);
176 T_FileStream_eof(FileStream
* fileStream
)
178 return feof((FILE*)fileStream
);
183 This function may not work consistently on all platforms
184 (e.g. HP-UX, FreeBSD and MacOSX don't return an error when
185 putc is used on a file opened as readonly)
188 T_FileStream_error(FileStream
* fileStream
)
190 return (fileStream
== 0 || ferror((FILE*)fileStream
));
193 /* This function doesn't work. */
194 /* force the stream to set its error flag*/
195 /*U_CAPI void U_EXPORT2
196 T_FileStream_setError(FileStream* fileStream)
198 fseek((FILE*)fileStream, 99999, SEEK_SET);
202 U_CAPI FileStream
* U_EXPORT2
203 T_FileStream_stdin(void)
205 return (FileStream
*)stdin
;
208 U_CAPI FileStream
* U_EXPORT2
209 T_FileStream_stdout(void)
211 return (FileStream
*)stdout
;
215 U_CAPI FileStream
* U_EXPORT2
216 T_FileStream_stderr(void)
218 return (FileStream
*)stderr
;
221 U_CAPI UBool U_EXPORT2
222 T_FileStream_remove(const char* fileName
){
223 return (remove(fileName
) == 0);