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