]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/filestrm.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1997-2011, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
15 * Modification History:
17 * Date Name Description
19 * 03/02/99 stephen Reordered params in ungetc to match stdio
21 * 3/29/99 helena Merged Stephen and Bertrand's changes.
23 ******************************************************************************
32 U_CAPI FileStream
* U_EXPORT2
33 T_FileStream_open(const char* filename
, const char* mode
)
35 if(filename
!= NULL
&& *filename
!= 0 && mode
!= NULL
&& *mode
!= 0) {
36 FILE *file
= fopen(filename
, mode
);
37 return (FileStream
*)file
;
44 U_CAPI FileStream* U_EXPORT2
45 T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode)
47 // TBD: _wfopen is believed to be MS-specific?
48 #if U_PLATFORM_USES_ONLY_WIN32_API
49 FILE* result = _wfopen(filename, mode);
50 return (FileStream*)result;
52 size_t fnMbsSize, mdMbsSize;
56 // convert from wchar_t to char
57 fnMbsSize = wcstombs(NULL, filename, ((size_t)-1) >> 1);
58 fn = (char*)uprv_malloc(fnMbsSize+2);
59 wcstombs(fn, filename, fnMbsSize);
62 mdMbsSize = wcstombs(NULL, mode, ((size_t)-1) >> 1);
63 md = (char*)uprv_malloc(mdMbsSize+2);
64 wcstombs(md, mode, mdMbsSize);
67 result = fopen(fn, md);
70 return (FileStream*)result;
75 T_FileStream_close(FileStream
* fileStream
)
78 fclose((FILE*)fileStream
);
81 U_CAPI UBool U_EXPORT2
82 T_FileStream_file_exists(const char* filename
)
84 FILE* temp
= fopen(filename
, "r");
92 /*static const int32_t kEOF;
93 const int32_t FileStream::kEOF = EOF;*/
97 T_FileStream_tmpfile()
99 FILE* file = tmpfile();
100 return (FileStream*)file;
104 U_CAPI
int32_t U_EXPORT2
105 T_FileStream_read(FileStream
* fileStream
, void* addr
, int32_t len
)
107 return static_cast<int32_t>(fread(addr
, 1, len
, (FILE*)fileStream
));
110 U_CAPI
int32_t U_EXPORT2
111 T_FileStream_write(FileStream
* fileStream
, const void* addr
, int32_t len
)
114 return static_cast<int32_t>(fwrite(addr
, 1, len
, (FILE*)fileStream
));
117 U_CAPI
void U_EXPORT2
118 T_FileStream_rewind(FileStream
* fileStream
)
120 rewind((FILE*)fileStream
);
123 U_CAPI
int32_t U_EXPORT2
124 T_FileStream_putc(FileStream
* fileStream
, int32_t ch
)
126 int32_t c
= fputc(ch
, (FILE*)fileStream
);
131 T_FileStream_getc(FileStream
* fileStream
)
133 int c
= fgetc((FILE*)fileStream
);
137 U_CAPI
int32_t U_EXPORT2
138 T_FileStream_ungetc(int32_t ch
, FileStream
* fileStream
)
141 int32_t c
= ungetc(ch
, (FILE*)fileStream
);
145 U_CAPI
int32_t U_EXPORT2
146 T_FileStream_peek(FileStream
* fileStream
)
148 int32_t c
= fgetc((FILE*)fileStream
);
149 return ungetc(c
, (FILE*)fileStream
);
152 U_CAPI
char* U_EXPORT2
153 T_FileStream_readLine(FileStream
* fileStream
, char* buffer
, int32_t length
)
155 return fgets(buffer
, length
, (FILE*)fileStream
);
158 U_CAPI
int32_t U_EXPORT2
159 T_FileStream_writeLine(FileStream
* fileStream
, const char* buffer
)
161 return fputs(buffer
, (FILE*)fileStream
);
164 U_CAPI
int32_t U_EXPORT2
165 T_FileStream_size(FileStream
* fileStream
)
167 int32_t savedPos
= ftell((FILE*)fileStream
);
170 /*Changes by Bertrand A. D. doesn't affect the current position
171 goes to the end of the file before ftell*/
172 fseek((FILE*)fileStream
, 0, SEEK_END
);
173 size
= (int32_t)ftell((FILE*)fileStream
);
174 fseek((FILE*)fileStream
, savedPos
, SEEK_SET
);
179 T_FileStream_eof(FileStream
* fileStream
)
181 return feof((FILE*)fileStream
);
186 This function may not work consistently on all platforms
187 (e.g. HP-UX, FreeBSD and MacOSX don't return an error when
188 putc is used on a file opened as readonly)
191 T_FileStream_error(FileStream
* fileStream
)
193 return (fileStream
== 0 || ferror((FILE*)fileStream
));
196 /* This function doesn't work. */
197 /* force the stream to set its error flag*/
198 /*U_CAPI void U_EXPORT2
199 T_FileStream_setError(FileStream* fileStream)
201 fseek((FILE*)fileStream, 99999, SEEK_SET);
205 U_CAPI FileStream
* U_EXPORT2
206 T_FileStream_stdin(void)
208 return (FileStream
*)stdin
;
211 U_CAPI FileStream
* U_EXPORT2
212 T_FileStream_stdout(void)
214 return (FileStream
*)stdout
;
218 U_CAPI FileStream
* U_EXPORT2
219 T_FileStream_stderr(void)
221 return (FileStream
*)stderr
;
224 U_CAPI UBool U_EXPORT2
225 T_FileStream_remove(const char* fileName
){
226 return (remove(fileName
) == 0);