]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/filestrm.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / filestrm.c
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
3*
4* Copyright (C) 1997-2003, International Business Machines
5* Corporation and others. All Rights Reserved.
6*
7******************************************************************************
8*
9* File FILESTRM.C
10*
11* @author Glenn Marcy
12*
13* Modification History:
14*
15* Date Name Description
16* 5/8/98 gm Created
17* 03/02/99 stephen Reordered params in ungetc to match stdio
18* Added wopen
19* 3/29/99 helena Merged Stephen and Bertrand's changes.
20*
21******************************************************************************
22*/
23
24#include "filestrm.h"
25#include "cmemory.h"
26
27#include <stdio.h>
28
29U_CAPI FileStream* U_EXPORT2
30T_FileStream_open(const char* filename, const char* mode)
31{
32 if(filename != NULL && *filename != 0 && mode != NULL && *mode != 0) {
33 FILE *file = fopen(filename, mode);
34 return (FileStream*)file;
35 } else {
36 return NULL;
37 }
38}
39
40/*
41U_CAPI FileStream* U_EXPORT2
42T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode)
43{
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;
48#else
49 size_t fnMbsSize, mdMbsSize;
50 char *fn, *md;
51 FILE *result;
52
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);
57 fn[fnMbsSize] = 0;
58
59 mdMbsSize = wcstombs(NULL, mode, ((size_t)-1) >> 1);
60 md = (char*)uprv_malloc(mdMbsSize+2);
61 wcstombs(md, mode, mdMbsSize);
62 md[mdMbsSize] = 0;
63
64 result = fopen(fn, md);
65 uprv_free(fn);
66 uprv_free(md);
67 return (FileStream*)result;
68#endif
69}
70*/
71U_CAPI void U_EXPORT2
72T_FileStream_close(FileStream* fileStream)
73{
74 if (fileStream != 0)
75 fclose((FILE*)fileStream);
76}
77
78U_CAPI UBool U_EXPORT2
79T_FileStream_file_exists(const char* filename)
80{
81 FILE* temp = fopen(filename, "r");
82 if (temp) {
83 fclose(temp);
84 return TRUE;
85 } else
86 return FALSE;
87}
88
89/*static const int32_t kEOF;
90const int32_t FileStream::kEOF = EOF;*/
91
92/*
93U_CAPI FileStream*
94T_FileStream_tmpfile()
95{
96 FILE* file = tmpfile();
97 return (FileStream*)file;
98}
99*/
100
101U_CAPI int32_t U_EXPORT2
102T_FileStream_read(FileStream* fileStream, void* addr, int32_t len)
103{
104 return fread(addr, 1, len, (FILE*)fileStream);
105}
106
107U_CAPI int32_t U_EXPORT2
108T_FileStream_write(FileStream* fileStream, const void* addr, int32_t len)
109{
110
111 return fwrite(addr, 1, len, (FILE*)fileStream);
112}
113
114U_CAPI void U_EXPORT2
115T_FileStream_rewind(FileStream* fileStream)
116{
117 rewind((FILE*)fileStream);
118}
119
120U_CAPI int32_t U_EXPORT2
121T_FileStream_putc(FileStream* fileStream, int32_t ch)
122{
123 int32_t c = fputc(ch, (FILE*)fileStream);
124 return c;
125}
126
127U_CAPI int U_EXPORT2
128T_FileStream_getc(FileStream* fileStream)
129{
130 int c = fgetc((FILE*)fileStream);
131 return c;
132}
133
134U_CAPI int32_t U_EXPORT2
135T_FileStream_ungetc(int32_t ch, FileStream* fileStream)
136{
137
138 int32_t c = ungetc(ch, (FILE*)fileStream);
139 return c;
140}
141
142U_CAPI int32_t U_EXPORT2
143T_FileStream_peek(FileStream* fileStream)
144{
145 int32_t c = fgetc((FILE*)fileStream);
146 return ungetc(c, (FILE*)fileStream);
147}
148
149U_CAPI char* U_EXPORT2
150T_FileStream_readLine(FileStream* fileStream, char* buffer, int32_t length)
151{
152 return fgets(buffer, length, (FILE*)fileStream);
153}
154
155U_CAPI int32_t U_EXPORT2
156T_FileStream_writeLine(FileStream* fileStream, const char* buffer)
157{
158 return fputs(buffer, (FILE*)fileStream);
159}
160
161U_CAPI int32_t U_EXPORT2
162T_FileStream_size(FileStream* fileStream)
163{
164 int32_t savedPos = ftell((FILE*)fileStream);
165 int32_t size = 0;
166
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);
172 return size;
173}
174
175U_CAPI int U_EXPORT2
176T_FileStream_eof(FileStream* fileStream)
177{
178 return feof((FILE*)fileStream);
179}
180
181/*
182 Warning
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)
186*/
187U_CAPI int U_EXPORT2
188T_FileStream_error(FileStream* fileStream)
189{
190 return (fileStream == 0 || ferror((FILE*)fileStream));
191}
192
193/* This function doesn't work. */
194/* force the stream to set its error flag*/
195/*U_CAPI void U_EXPORT2
196T_FileStream_setError(FileStream* fileStream)
197{
198 fseek((FILE*)fileStream, 99999, SEEK_SET);
199}
200*/
201
202U_CAPI FileStream* U_EXPORT2
203T_FileStream_stdin(void)
204{
205 return (FileStream*)stdin;
206}
207
208U_CAPI FileStream* U_EXPORT2
209T_FileStream_stdout(void)
210{
211 return (FileStream*)stdout;
212}
213
214
215U_CAPI FileStream* U_EXPORT2
216T_FileStream_stderr(void)
217{
218 return (FileStream*)stderr;
219}
220
221U_CAPI UBool U_EXPORT2
222T_FileStream_remove(const char* fileName){
223 return (remove(fileName) == 0);
224}
225
226