home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / filestrm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  4.9 KB  |  215 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright Taligent, Inc.,  1997                                       *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1998     *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  10. *                                                                             *
  11. *******************************************************************************
  12. *
  13. * File FILESTRM.C
  14. *
  15. * @author       Glenn Marcy
  16. *
  17. * Modification History:
  18. *
  19. *   Date        Name        Description
  20. *   5/8/98      gm          Created
  21. *  03/02/99     stephen     Reordered params in ungetc to match stdio
  22. *                           Added wopen
  23. *   3/29/99     helena      Merged Stephen and Bertrand's changes.
  24. *
  25. *******************************************************************************
  26. */
  27.  
  28. #include "filestrm.h"
  29. #include "cmemory.h"
  30.  
  31. #include <stdio.h>
  32.  
  33. U_CAPI FileStream*
  34. T_FileStream_open(const char* filename, const char* mode)
  35. {
  36.   FILE *file = fopen(filename, mode);
  37.   return (FileStream*)file;
  38. }
  39.  
  40.  
  41. U_CAPI FileStream*
  42. T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode)
  43. {
  44.   /* TBD: is _wfopen MS-specific?  If so, change _WIN32 to WIN32 */
  45. /*#ifdef _WIN32*/
  46. #if defined(_WIN32) && !defined(__WINDOWS__)
  47.     FILE* result = _wfopen(filename, mode);
  48.     return (FileStream*)result;
  49. #else
  50.     size_t fnMbsSize, mdMbsSize;
  51.     char *fn, *md;
  52.     FILE *result;
  53.  
  54.     /* convert from wchar_t to char */
  55.     fnMbsSize = wcstombs(NULL, filename, ((size_t)-1) >> 1);
  56.     fn = (char*)icu_malloc(fnMbsSize+2);
  57.     wcstombs(fn, filename, fnMbsSize);
  58.     fn[fnMbsSize] = 0;
  59.  
  60.     mdMbsSize = wcstombs(NULL, mode, ((size_t)-1) >> 1);
  61.     md = (char*)icu_malloc(mdMbsSize+2);
  62.     wcstombs(md, mode, mdMbsSize);
  63.     md[mdMbsSize] = 0;
  64.  
  65.     result = fopen(fn, md);
  66.     icu_free(fn);
  67.     icu_free(md);
  68.     return (FileStream*)result;
  69. #endif
  70. }
  71.  
  72. U_CAPI void
  73. T_FileStream_close(FileStream* fileStream)
  74. {
  75.     if (fileStream != 0) fclose((FILE*)fileStream);
  76. }
  77.  
  78. U_CAPI bool_t
  79. T_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;
  90. const int32_t FileStream::kEOF = EOF;*/
  91.  
  92. U_CAPI FileStream*
  93. T_FileStream_tmpfile()
  94. {
  95.     FILE* file = tmpfile();
  96.     return (FileStream*)file;
  97. }
  98.  
  99. U_CAPI int32_t
  100. T_FileStream_read(FileStream* fileStream, void* addr, int32_t len)
  101. {
  102.     return fread(addr, 1, len, (FILE*)fileStream);
  103. }
  104.  
  105. U_CAPI int32_t
  106. T_FileStream_write(FileStream* fileStream, const void* addr, int32_t len)
  107. {
  108.  
  109.     return fwrite(addr, 1, len, (FILE*)fileStream);
  110. }
  111.  
  112. U_CAPI void
  113. T_FileStream_rewind(FileStream* fileStream)
  114. {
  115.     rewind((FILE*)fileStream);
  116. }
  117.  
  118. U_CAPI int32_t
  119. T_FileStream_putc(FileStream* fileStream, int32_t ch)
  120. {
  121.     int32_t c = fputc(ch, (FILE*)fileStream);
  122.     return c;
  123. }
  124.  
  125. U_CAPI int
  126. T_FileStream_getc(FileStream* fileStream)
  127. {
  128.     int c = fgetc((FILE*)fileStream);
  129.     return c;
  130. }
  131.  
  132. U_CAPI int32_t
  133. T_FileStream_ungetc(int32_t ch, FileStream* fileStream)
  134. {
  135.  
  136.     int32_t c = ungetc(ch, (FILE*)fileStream);
  137.     return c;
  138. }
  139.  
  140. U_CAPI int32_t
  141. T_FileStream_peek(FileStream* fileStream)
  142. {
  143.     int32_t c = fgetc((FILE*)fileStream);
  144.     return ungetc(c, (FILE*)fileStream);
  145. }
  146.  
  147. /*Added by Bertrand A. D. */
  148. U_CAPI char *
  149. T_FileStream_readLine(FileStream* fileStream, char* buffer, int32_t length)
  150. {
  151.     return fgets(buffer, length, (FILE*)fileStream);
  152. }
  153.  
  154. U_CAPI int32_t
  155. T_FileStream_writeLine(FileStream* fileStream, const char* buffer)
  156. {
  157.     return fputs(buffer, (FILE*)fileStream);
  158. }
  159.  
  160. U_CAPI int32_t
  161. T_FileStream_size(FileStream* fileStream)
  162. {
  163.     int32_t savedPos = ftell((FILE*)fileStream);
  164.     int32_t size = 0;
  165.  
  166.     /*Changes by Bertrand A. D. doesn't affect the current position
  167.     goes to the end of the file before ftell*/
  168.     fseek((FILE*)fileStream, 0, SEEK_END);
  169.     size = ftell((FILE*)fileStream);
  170.     fseek((FILE*)fileStream, savedPos, SEEK_SET);
  171.     return size;
  172. }
  173.  
  174. U_CAPI int
  175. T_FileStream_eof(FileStream* fileStream)
  176. {
  177.     return feof((FILE*)fileStream);
  178. }
  179.  
  180. U_CAPI int
  181. T_FileStream_error(FileStream* fileStream)
  182. {
  183.     return (fileStream == 0 || ferror((FILE*)fileStream));
  184. }
  185.  
  186. U_CAPI void
  187. T_FileStream_setError(FileStream* fileStream)
  188. {
  189.     /* force the stream to set its error flag*/
  190.     fseek((FILE*)fileStream, 99999, SEEK_SET);
  191. }
  192.  
  193.  
  194. U_CAPI FileStream*
  195. T_FileStream_stdin(void)
  196. {
  197.     return (FileStream*)stdin;
  198. }
  199.  
  200. U_CAPI FileStream*
  201. T_FileStream_stdout(void)
  202. {
  203.     return (FileStream*)stdout;
  204. }
  205.  
  206.  
  207. U_CAPI FileStream*
  208. T_FileStream_stderr(void)
  209. {
  210.     return (FileStream*)stderr;
  211. }
  212.  
  213.  
  214.  
  215.