Changeset 646 for proto/u8u16


Ignore:
Timestamp:
Oct 12, 2010, 4:58:35 AM (9 years ago)
Author:
cameron
Message:

Modify driver for truncated chars at end of buffer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/u8u16/template.c

    r624 r646  
    247247
    248248  int buf_pos = 0;
    249   int chars_read = 0;
     249  int bytes_available = 0;
     250  int excess_bytes, i;
    250251  char srcbuf[BUFFER_SIZE+BLOCK_SIZE];
    251252  char * srclimit;
     
    257258  UErrorCode status;
    258259 
    259   chars_read = fread((void *)srcbuf, sizeof(char), BUFFER_SIZE, infile);
    260   srclimit = srcbuf + chars_read;
     260  bytes_available = fread((void *)srcbuf, sizeof(char), BUFFER_SIZE, infile);
     261  srclimit = srcbuf + bytes_available;
    261262  targetbuf_start = targetbuf;
    262263
    263   while(chars_read>0){
     264  while(bytes_available>0){
    264265
    265266    PERF_SEC_START(transcode_timer);
     
    269270
    270271    u8u16(&targetbuf_ptr, targetlimit, &srcbuf_ptr, srclimit, &status);
    271 
    272 
    273     PERF_SEC_END(transcode_timer, chars_read);
     272    excess_bytes = (int) (srclimit - srcbuf_ptr);
     273    buf_pos += bytes_available - excess_bytes;
     274
     275    PERF_SEC_END(transcode_timer, bytes_available - excess_bytes);
    274276   
    275277   
     
    277279   
    278280    if (status == U_ILLEGAL_CHAR_FOUND) {
    279             fprintf(stderr, "Illegal UTF-8 sequence at position %lu in source.\n", buf_pos + chars_read-(srclimit-srcbuf_ptr));
     281            fclose(infile);
     282            fclose(outfile);
     283            fprintf(stderr, "Illegal UTF-8 sequence at position %lu in source.\n", (unsigned long) buf_pos);
    280284            exit(-1);
    281285    }
    282286    else if (status == U_TRUNCATED_CHAR_FOUND) {
    283             fprintf(stderr, "EOF with incomplete UTF-8 sequence at position %lu in source.\n",buf_pos + chars_read-(srclimit-srcbuf_ptr));
     287        for (i = 0; i < excess_bytes; i++) {
     288          srcbuf[i] = srcbuf_ptr[i];
     289        }
     290        bytes_available = fread((void *) &(srcbuf[excess_bytes]), sizeof(char), BUFFER_SIZE-excess_bytes, infile);
     291        if (bytes_available == 0) {
     292            fclose(infile);
     293            fclose(outfile);
     294            fprintf(stderr, "EOF with incomplete UTF-8 sequence at position %lu in source.\n", (unsigned long) buf_pos);
    284295            exit(-1);
     296        }
     297        bytes_available += excess_bytes;
     298        srclimit = srcbuf + bytes_available;
    285299    }
    286 
    287     buf_pos += chars_read;
    288     chars_read = fread((void *)srcbuf, sizeof(char), BUFFER_SIZE, infile);
    289     srclimit = srcbuf + chars_read;
     300    else {
     301      bytes_available = fread((void *)srcbuf, sizeof(char), BUFFER_SIZE, infile);
     302      srclimit = srcbuf + bytes_available;
     303    }
    290304  }
    291  
    292305}
    293306
Note: See TracChangeset for help on using the changeset viewer.