1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/tools/toolutil/filestrm.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,225 @@ 1.4 +/* 1.5 +****************************************************************************** 1.6 +* 1.7 +* Copyright (C) 1997-2011, International Business Machines 1.8 +* Corporation and others. All Rights Reserved. 1.9 +* 1.10 +****************************************************************************** 1.11 +* 1.12 +* File FILESTRM.C 1.13 +* 1.14 +* @author Glenn Marcy 1.15 +* 1.16 +* Modification History: 1.17 +* 1.18 +* Date Name Description 1.19 +* 5/8/98 gm Created 1.20 +* 03/02/99 stephen Reordered params in ungetc to match stdio 1.21 +* Added wopen 1.22 +* 3/29/99 helena Merged Stephen and Bertrand's changes. 1.23 +* 1.24 +****************************************************************************** 1.25 +*/ 1.26 + 1.27 +#include "filestrm.h" 1.28 + 1.29 +#include "cmemory.h" 1.30 + 1.31 +#include <stdio.h> 1.32 + 1.33 +U_CAPI FileStream* U_EXPORT2 1.34 +T_FileStream_open(const char* filename, const char* mode) 1.35 +{ 1.36 + if(filename != NULL && *filename != 0 && mode != NULL && *mode != 0) { 1.37 + FILE *file = fopen(filename, mode); 1.38 + return (FileStream*)file; 1.39 + } else { 1.40 + return NULL; 1.41 + } 1.42 +} 1.43 + 1.44 +/* 1.45 +U_CAPI FileStream* U_EXPORT2 1.46 +T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode) 1.47 +{ 1.48 + // TBD: _wfopen is believed to be MS-specific? 1.49 +#if U_PLATFORM_USES_ONLY_WIN32_API 1.50 + FILE* result = _wfopen(filename, mode); 1.51 + return (FileStream*)result; 1.52 +#else 1.53 + size_t fnMbsSize, mdMbsSize; 1.54 + char *fn, *md; 1.55 + FILE *result; 1.56 + 1.57 + // convert from wchar_t to char 1.58 + fnMbsSize = wcstombs(NULL, filename, ((size_t)-1) >> 1); 1.59 + fn = (char*)uprv_malloc(fnMbsSize+2); 1.60 + wcstombs(fn, filename, fnMbsSize); 1.61 + fn[fnMbsSize] = 0; 1.62 + 1.63 + mdMbsSize = wcstombs(NULL, mode, ((size_t)-1) >> 1); 1.64 + md = (char*)uprv_malloc(mdMbsSize+2); 1.65 + wcstombs(md, mode, mdMbsSize); 1.66 + md[mdMbsSize] = 0; 1.67 + 1.68 + result = fopen(fn, md); 1.69 + uprv_free(fn); 1.70 + uprv_free(md); 1.71 + return (FileStream*)result; 1.72 +#endif 1.73 +} 1.74 +*/ 1.75 +U_CAPI void U_EXPORT2 1.76 +T_FileStream_close(FileStream* fileStream) 1.77 +{ 1.78 + if (fileStream != 0) 1.79 + fclose((FILE*)fileStream); 1.80 +} 1.81 + 1.82 +U_CAPI UBool U_EXPORT2 1.83 +T_FileStream_file_exists(const char* filename) 1.84 +{ 1.85 + FILE* temp = fopen(filename, "r"); 1.86 + if (temp) { 1.87 + fclose(temp); 1.88 + return TRUE; 1.89 + } else 1.90 + return FALSE; 1.91 +} 1.92 + 1.93 +/*static const int32_t kEOF; 1.94 +const int32_t FileStream::kEOF = EOF;*/ 1.95 + 1.96 +/* 1.97 +U_CAPI FileStream* 1.98 +T_FileStream_tmpfile() 1.99 +{ 1.100 + FILE* file = tmpfile(); 1.101 + return (FileStream*)file; 1.102 +} 1.103 +*/ 1.104 + 1.105 +U_CAPI int32_t U_EXPORT2 1.106 +T_FileStream_read(FileStream* fileStream, void* addr, int32_t len) 1.107 +{ 1.108 + return fread(addr, 1, len, (FILE*)fileStream); 1.109 +} 1.110 + 1.111 +U_CAPI int32_t U_EXPORT2 1.112 +T_FileStream_write(FileStream* fileStream, const void* addr, int32_t len) 1.113 +{ 1.114 + 1.115 + return fwrite(addr, 1, len, (FILE*)fileStream); 1.116 +} 1.117 + 1.118 +U_CAPI void U_EXPORT2 1.119 +T_FileStream_rewind(FileStream* fileStream) 1.120 +{ 1.121 + rewind((FILE*)fileStream); 1.122 +} 1.123 + 1.124 +U_CAPI int32_t U_EXPORT2 1.125 +T_FileStream_putc(FileStream* fileStream, int32_t ch) 1.126 +{ 1.127 + int32_t c = fputc(ch, (FILE*)fileStream); 1.128 + return c; 1.129 +} 1.130 + 1.131 +U_CAPI int U_EXPORT2 1.132 +T_FileStream_getc(FileStream* fileStream) 1.133 +{ 1.134 + int c = fgetc((FILE*)fileStream); 1.135 + return c; 1.136 +} 1.137 + 1.138 +U_CAPI int32_t U_EXPORT2 1.139 +T_FileStream_ungetc(int32_t ch, FileStream* fileStream) 1.140 +{ 1.141 + 1.142 + int32_t c = ungetc(ch, (FILE*)fileStream); 1.143 + return c; 1.144 +} 1.145 + 1.146 +U_CAPI int32_t U_EXPORT2 1.147 +T_FileStream_peek(FileStream* fileStream) 1.148 +{ 1.149 + int32_t c = fgetc((FILE*)fileStream); 1.150 + return ungetc(c, (FILE*)fileStream); 1.151 +} 1.152 + 1.153 +U_CAPI char* U_EXPORT2 1.154 +T_FileStream_readLine(FileStream* fileStream, char* buffer, int32_t length) 1.155 +{ 1.156 + return fgets(buffer, length, (FILE*)fileStream); 1.157 +} 1.158 + 1.159 +U_CAPI int32_t U_EXPORT2 1.160 +T_FileStream_writeLine(FileStream* fileStream, const char* buffer) 1.161 +{ 1.162 + return fputs(buffer, (FILE*)fileStream); 1.163 +} 1.164 + 1.165 +U_CAPI int32_t U_EXPORT2 1.166 +T_FileStream_size(FileStream* fileStream) 1.167 +{ 1.168 + int32_t savedPos = ftell((FILE*)fileStream); 1.169 + int32_t size = 0; 1.170 + 1.171 + /*Changes by Bertrand A. D. doesn't affect the current position 1.172 + goes to the end of the file before ftell*/ 1.173 + fseek((FILE*)fileStream, 0, SEEK_END); 1.174 + size = (int32_t)ftell((FILE*)fileStream); 1.175 + fseek((FILE*)fileStream, savedPos, SEEK_SET); 1.176 + return size; 1.177 +} 1.178 + 1.179 +U_CAPI int U_EXPORT2 1.180 +T_FileStream_eof(FileStream* fileStream) 1.181 +{ 1.182 + return feof((FILE*)fileStream); 1.183 +} 1.184 + 1.185 +/* 1.186 + Warning 1.187 + This function may not work consistently on all platforms 1.188 + (e.g. HP-UX, FreeBSD and MacOSX don't return an error when 1.189 + putc is used on a file opened as readonly) 1.190 +*/ 1.191 +U_CAPI int U_EXPORT2 1.192 +T_FileStream_error(FileStream* fileStream) 1.193 +{ 1.194 + return (fileStream == 0 || ferror((FILE*)fileStream)); 1.195 +} 1.196 + 1.197 +/* This function doesn't work. */ 1.198 +/* force the stream to set its error flag*/ 1.199 +/*U_CAPI void U_EXPORT2 1.200 +T_FileStream_setError(FileStream* fileStream) 1.201 +{ 1.202 + fseek((FILE*)fileStream, 99999, SEEK_SET); 1.203 +} 1.204 +*/ 1.205 + 1.206 +U_CAPI FileStream* U_EXPORT2 1.207 +T_FileStream_stdin(void) 1.208 +{ 1.209 + return (FileStream*)stdin; 1.210 +} 1.211 + 1.212 +U_CAPI FileStream* U_EXPORT2 1.213 +T_FileStream_stdout(void) 1.214 +{ 1.215 + return (FileStream*)stdout; 1.216 +} 1.217 + 1.218 + 1.219 +U_CAPI FileStream* U_EXPORT2 1.220 +T_FileStream_stderr(void) 1.221 +{ 1.222 + return (FileStream*)stderr; 1.223 +} 1.224 + 1.225 +U_CAPI UBool U_EXPORT2 1.226 +T_FileStream_remove(const char* fileName){ 1.227 + return (remove(fileName) == 0); 1.228 +}