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

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998           *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File ufile.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   11/19/98    stephen        Creation.
  18. *   03/12/99    stephen     Modified for new C API.
  19. *******************************************************************************
  20. */
  21.  
  22. #include <stdio.h>
  23. #include "ustdio.h"
  24. #include "ufile.h"
  25. #include "cmemory.h"
  26.  
  27. #define MIN(a,b) (a < b ? a : b)
  28. #define MAX(a,b) (a > b ? a : b)
  29.  
  30. UFILE*
  31. u_finit(FileStream *f,
  32.     UErrorCode *status)
  33. {
  34.   UFILE *result    = (UFILE*) icu_malloc(sizeof(UFILE));
  35.   if(result == 0) {
  36.     *status = U_MEMORY_ALLOCATION_ERROR;
  37.     return 0;
  38.   }
  39.  
  40.   result->fFile     = f;
  41.   result->fOwnFile     = FALSE;
  42.   result->fUCPos     = result->fUCBuffer;
  43.   result->fUCLimit     = result->fUCBuffer;
  44.   
  45.   result->fConverter = ucnv_open(0, status);
  46.   if(U_FAILURE(*status) || result->fConverter == 0) {
  47.     T_FileStream_close(result->fFile);
  48.     icu_free(result);
  49.     return 0;
  50.   }
  51.  
  52.   return result;
  53. }
  54.  
  55. void
  56. u_fclose(UFILE *file)
  57. {
  58.   if(file->fOwnFile)
  59.     T_FileStream_close(file->fFile);
  60.  
  61.   ucnv_close(file->fConverter);
  62.   icu_free(file);
  63. }
  64.  
  65. /* private function used for buffering input */
  66. void
  67. ufile_fill_uchar_buffer(UFILE *f,
  68.             UErrorCode *status)
  69. {
  70.   const char        *mySource;
  71.   const char        *mySourceEnd;
  72.   UChar            *myTarget;
  73.   int32_t        bufferSize;
  74.   int32_t        maxCPBytes;
  75.   int32_t        bytesRead;
  76.   int32_t        availLength;
  77.   int32_t        dataSize;
  78.  
  79.   if(U_FAILURE(*status)) return;
  80.  
  81.   /* shift the buffer if it isn't empty */
  82.   dataSize = f->fUCLimit - f->fUCPos;
  83.   if(dataSize != 0) {
  84.     icu_memmove(f->fUCBuffer, 
  85.         f->fUCPos, 
  86.         dataSize * sizeof(UChar));
  87.   }
  88.   
  89.   /* record how much buffer space is available */
  90.   availLength = UFILE_UCHARBUFFER_SIZE - dataSize;
  91.   
  92.   /* Determine the # of codepage bytes needed to fill our UChar buffer */
  93.   maxCPBytes = availLength * ucnv_getMaxCharSize(f->fConverter);
  94.   
  95.   /* Read in the data to convert */
  96.   bytesRead = T_FileStream_read(f->fFile,f->fCharBuffer, 
  97.             MIN(maxCPBytes, UFILE_CHARBUFFER_SIZE));
  98.   
  99.   /* Set up conversion parameters */
  100.   *status    = U_ZERO_ERROR;
  101.   mySource       = f->fCharBuffer;
  102.   mySourceEnd     = f->fCharBuffer + bytesRead;
  103.   myTarget     = f->fUCBuffer + dataSize;
  104.   bufferSize    = UFILE_UCHARBUFFER_SIZE;
  105.  
  106.   /* Perform the conversion */
  107.   ucnv_toUnicode(f->fConverter,
  108.          &myTarget, 
  109.          f->fUCBuffer + bufferSize,
  110.          &mySource,
  111.          mySourceEnd,
  112.          NULL,
  113.          TRUE,
  114.          status);
  115.   
  116.   /* update the pointers into our array */
  117.   f->fUCPos    = f->fUCBuffer;
  118.   f->fUCLimit     = myTarget;
  119. }
  120.