xml/xmlexpatparser/src/expat-1.95.5/tests/chardata.c
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
       
     2    See the file COPYING for copying permission.
       
     3 */
       
     4 /*  chardata.c
       
     5  *
       
     6  *
       
     7  */
       
     8 
       
     9 #include <assert.h>
       
    10 #include <check.h>
       
    11 #include <stdio.h>
       
    12 #include <string.h>
       
    13 
       
    14 #include "chardata.h"
       
    15 
       
    16 
       
    17 static int
       
    18 xmlstrlen(const XML_Char *s)
       
    19 {
       
    20     int len = 0;
       
    21     assert(s != NULL);
       
    22     while (s[len] != 0)
       
    23         ++len;
       
    24     return len;
       
    25 }
       
    26 
       
    27 
       
    28 void
       
    29 CharData_Init(CharData *storage)
       
    30 {
       
    31     assert(storage != NULL);
       
    32     storage->count = -1;
       
    33 }
       
    34 
       
    35 void
       
    36 CharData_AppendString(CharData *storage, const char *s)
       
    37 {
       
    38     int maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
       
    39     int len;
       
    40 
       
    41     assert(s != NULL);
       
    42     len = strlen(s);
       
    43     if (storage->count < 0)
       
    44         storage->count = 0;
       
    45     if ((len + storage->count) > maxchars) {
       
    46         len = (maxchars - storage->count);
       
    47     }
       
    48     if (len + storage->count < sizeof(storage->data)) {
       
    49         memcpy(storage->data + storage->count, s, len);
       
    50         storage->count += len;
       
    51     }
       
    52 }
       
    53 
       
    54 void
       
    55 CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len)
       
    56 {
       
    57     int maxchars;
       
    58 
       
    59     assert(storage != NULL);
       
    60     assert(s != NULL);
       
    61     maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
       
    62     if (storage->count < 0)
       
    63         storage->count = 0;
       
    64     if (len < 0)
       
    65         len = xmlstrlen(s);
       
    66     if ((len + storage->count) > maxchars) {
       
    67         len = (maxchars - storage->count);
       
    68     }
       
    69     if (len + storage->count < sizeof(storage->data)) {
       
    70         memcpy(storage->data + storage->count, s,
       
    71                len * sizeof(storage->data[0]));
       
    72         storage->count += len;
       
    73     }
       
    74 }
       
    75 
       
    76 int
       
    77 CharData_CheckString(CharData *storage, const char *expected)
       
    78 {
       
    79     char buffer[1280];
       
    80     int len;
       
    81     int count;
       
    82 
       
    83     assert(storage != NULL);
       
    84     assert(expected != NULL);
       
    85     count = (storage->count < 0) ? 0 : storage->count;
       
    86     len = strlen(expected);
       
    87     if (len != count) {
       
    88         if (sizeof(XML_Char) == 1)
       
    89             sprintf(buffer, "wrong number of data characters:"
       
    90                     " got %d, expected %d:\n%s", count, len, storage->data);
       
    91         else
       
    92             sprintf(buffer,
       
    93                     "wrong number of data characters: got %d, expected %d",
       
    94                     count, len);
       
    95         fail(buffer);
       
    96         return 0;
       
    97     }
       
    98     if (memcmp(expected, storage->data, len) != 0) {
       
    99         fail("got bad data bytes");
       
   100         return 0;
       
   101     }
       
   102     return 1;
       
   103 }
       
   104 
       
   105 int
       
   106 CharData_CheckXMLChars(CharData *storage, const XML_Char *expected)
       
   107 {
       
   108     char buffer[1024];
       
   109     int len = xmlstrlen(expected);
       
   110     int count;
       
   111 
       
   112     assert(storage != NULL);
       
   113     count = (storage->count < 0) ? 0 : storage->count;
       
   114     if (len != count) {
       
   115         sprintf(buffer, "wrong number of data characters: got %d, expected %d",
       
   116                 count, len);
       
   117         fail(buffer);
       
   118         return 0;
       
   119     }
       
   120     if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
       
   121         fail("got bad data bytes");
       
   122         return 0;
       
   123     }
       
   124     return 1;
       
   125 }