]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/filestrm.c
2 ******************************************************************************
4 * Copyright (C) 1997-2011, 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 ******************************************************************************
30 U_CAPI FileStream
* U_EXPORT2
31 T_FileStream_open(const char* filename
, const char* mode
)
33 if(filename
!= NULL
&& *filename
!= 0 && mode
!= NULL
&& *mode
!= 0) {
34 FILE *file
= fopen(filename
, mode
);
35 return (FileStream
*)file
;
42 U_CAPI FileStream* U_EXPORT2
43 T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode)
45 // TBD: _wfopen is believed to be MS-specific?
46 #if U_PLATFORM_USES_ONLY_WIN32_API
47 FILE* result = _wfopen(filename, mode);
48 return (FileStream*)result;
50 size_t fnMbsSize, mdMbsSize;
54 // convert from wchar_t to char
55 fnMbsSize = wcstombs(NULL, filename, ((size_t)-1) >> 1);
56 fn = (char*)uprv_malloc(fnMbsSize+2);
57 wcstombs(fn, filename, fnMbsSize);
60 mdMbsSize = wcstombs(NULL, mode, ((size_t)-1) >> 1);
61 md = (char*)uprv_malloc(mdMbsSize+2);
62 wcstombs(md, mode, mdMbsSize);
65 result = fopen(fn, md);
68 return (FileStream*)result;
73 T_FileStream_close(FileStream
* fileStream
)
76 fclose((FILE*)fileStream
);
79 U_CAPI UBool U_EXPORT2
80 T_FileStream_file_exists(const char* filename
)
82 FILE* temp
= fopen(filename
, "r");
90 /*static const int32_t kEOF;
91 const int32_t FileStream::kEOF = EOF;*/
95 T_FileStream_tmpfile()
97 FILE* file = tmpfile();
98 return (FileStream*)file;
102 U_CAPI
int32_t U_EXPORT2
103 T_FileStream_read(FileStream
* fileStream
, void* addr
, int32_t len
)
105 return fread(addr
, 1, len
, (FILE*)fileStream
);
108 U_CAPI
int32_t U_EXPORT2
109 T_FileStream_write(FileStream
* fileStream
, const void* addr
, int32_t len
)
112 return fwrite(addr
, 1, len
, (FILE*)fileStream
);
115 U_CAPI
void U_EXPORT2
116 T_FileStream_rewind(FileStream
* fileStream
)
118 rewind((FILE*)fileStream
);
121 U_CAPI
int32_t U_EXPORT2
122 T_FileStream_putc(FileStream
* fileStream
, int32_t ch
)
124 int32_t c
= fputc(ch
, (FILE*)fileStream
);
129 T_FileStream_getc(FileStream
* fileStream
)
131 int c
= fgetc((FILE*)fileStream
);
135 U_CAPI
int32_t U_EXPORT2
136 T_FileStream_ungetc(int32_t ch
, FileStream
* fileStream
)
139 int32_t c
= ungetc(ch
, (FILE*)fileStream
);
143 U_CAPI
int32_t U_EXPORT2
144 T_FileStream_peek(FileStream
* fileStream
)
146 int32_t c
= fgetc((FILE*)fileStream
);
147 return ungetc(c
, (FILE*)fileStream
);
150 U_CAPI
char* U_EXPORT2
151 T_FileStream_readLine(FileStream
* fileStream
, char* buffer
, int32_t length
)
153 return fgets(buffer
, length
, (FILE*)fileStream
);
156 U_CAPI
int32_t U_EXPORT2
157 T_FileStream_writeLine(FileStream
* fileStream
, const char* buffer
)
159 return fputs(buffer
, (FILE*)fileStream
);
162 U_CAPI
int32_t U_EXPORT2
163 T_FileStream_size(FileStream
* fileStream
)
165 int32_t savedPos
= ftell((FILE*)fileStream
);
168 /*Changes by Bertrand A. D. doesn't affect the current position
169 goes to the end of the file before ftell*/
170 fseek((FILE*)fileStream
, 0, SEEK_END
);
171 size
= (int32_t)ftell((FILE*)fileStream
);
172 fseek((FILE*)fileStream
, savedPos
, SEEK_SET
);
177 T_FileStream_eof(FileStream
* fileStream
)
179 return feof((FILE*)fileStream
);
184 This function may not work consistently on all platforms
185 (e.g. HP-UX, FreeBSD and MacOSX don't return an error when
186 putc is used on a file opened as readonly)
189 T_FileStream_error(FileStream
* fileStream
)
191 return (fileStream
== 0 || ferror((FILE*)fileStream
));
194 /* This function doesn't work. */
195 /* force the stream to set its error flag*/
196 /*U_CAPI void U_EXPORT2
197 T_FileStream_setError(FileStream* fileStream)
199 fseek((FILE*)fileStream, 99999, SEEK_SET);
203 U_CAPI FileStream
* U_EXPORT2
204 T_FileStream_stdin(void)
206 return (FileStream
*)stdin
;
209 U_CAPI FileStream
* U_EXPORT2
210 T_FileStream_stdout(void)
212 return (FileStream
*)stdout
;
216 U_CAPI FileStream
* U_EXPORT2
217 T_FileStream_stderr(void)
219 return (FileStream
*)stderr
;
222 U_CAPI UBool U_EXPORT2
223 T_FileStream_remove(const char* fileName
){
224 return (remove(fileName
) == 0);