Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <inttypes.h>
  2. #include <stdio.h>
  3.  
  4. #include <parserutils/parserutils.h>
  5. #include <parserutils/charset/utf8.h>
  6. #include <parserutils/input/inputstream.h>
  7.  
  8. #include "utils/utils.h"
  9.  
  10. #include "testutils.h"
  11.  
  12. #ifdef __riscos
  13. const char * const __dynamic_da_name = "InputStream";
  14. int __dynamic_da_max_size = 128*1024*1024;
  15. #endif
  16.  
  17. static void *myrealloc(void *ptr, size_t len, void *pw)
  18. {
  19.         UNUSED(pw);
  20.  
  21.         return realloc(ptr, len);
  22. }
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.         parserutils_inputstream *stream;
  27.         FILE *fp;
  28.         size_t len;
  29. #define CHUNK_SIZE (4096)
  30.         uint8_t buf[CHUNK_SIZE];
  31.         const uint8_t *c;
  32.         size_t clen;
  33.  
  34.         if (argc != 2) {
  35.                 printf("Usage: %s <filename>\n", argv[0]);
  36.                 return 1;
  37.         }
  38.  
  39.         assert(parserutils_inputstream_create("UTF-8", 1, NULL,
  40.                         myrealloc, NULL, &stream) == PARSERUTILS_OK);
  41.  
  42.         fp = fopen(argv[1], "rb");
  43.         if (fp == NULL) {
  44.                 printf("Failed opening %s\n", argv[1]);
  45.                 return 1;
  46.         }
  47.  
  48.         fseek(fp, 0, SEEK_END);
  49.         len = ftell(fp);
  50.         fseek(fp, 0, SEEK_SET);
  51.  
  52.         while (len >= CHUNK_SIZE) {
  53.                 size_t read = fread(buf, 1, CHUNK_SIZE, fp);
  54.                 assert(read == CHUNK_SIZE);
  55.  
  56.                 assert(parserutils_inputstream_append(stream,
  57.                                 buf, CHUNK_SIZE) == PARSERUTILS_OK);
  58.  
  59.                 len -= CHUNK_SIZE;
  60.  
  61.                 while (parserutils_inputstream_peek(stream, 0, &c, &clen) !=
  62.                                 PARSERUTILS_NEEDDATA) {
  63.                         parserutils_inputstream_advance(stream, clen);
  64.                         if (*c == 'a') {
  65.                                 assert(parserutils_inputstream_insert(stream,
  66.                                                 (const uint8_t *) "hello!!!",
  67.                                                 SLEN("hello!!!")) == PARSERUTILS_OK);
  68.                         }
  69.                 }
  70.         }
  71.  
  72.         if (len > 0) {
  73.                 size_t read = fread(buf, 1, len, fp);
  74.                 assert(read == len);
  75.  
  76.                 assert(parserutils_inputstream_append(stream,
  77.                                 buf, len) == PARSERUTILS_OK);
  78.  
  79.                 len = 0;
  80.         }
  81.  
  82.         fclose(fp);
  83.  
  84.         assert(parserutils_inputstream_insert(stream,
  85.                         (const uint8_t *) "hello!!!",
  86.                         SLEN("hello!!!")) == PARSERUTILS_OK);
  87.  
  88.         assert(parserutils_inputstream_append(stream, NULL, 0) ==
  89.                         PARSERUTILS_OK);
  90.  
  91.         while (parserutils_inputstream_peek(stream, 0, &c, &clen) !=
  92.                         PARSERUTILS_EOF) {
  93.                 parserutils_inputstream_advance(stream, clen);
  94.         }
  95.  
  96.         parserutils_inputstream_destroy(stream);
  97.  
  98.         printf("PASS\n");
  99.  
  100.         return 0;
  101. }
  102.  
  103.