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 <libcss/libcss.h>
  5.  
  6. #include "charset/detect.h"
  7. #include "utils/utils.h"
  8.  
  9. #include "lex/lex.h"
  10. #include "parse/parse.h"
  11.  
  12. #include "testutils.h"
  13.  
  14. #define ITERATIONS (1)
  15. #define DUMP_EVENTS (0)
  16.  
  17. #if DUMP_EVENTS
  18. static const char *event_names[] = {
  19.         "START_STYLESHEET",
  20.         "END_STYLESHEET",
  21.         "START_RULESET",
  22.         "END_RULESET",
  23.         "START_ATRULE",
  24.         "END_ATRULE",
  25.         "START_BLOCK",
  26.         "END_BLOCK",
  27.         "BLOCK_CONTENT",
  28.         "DECLARATION"
  29. };
  30. #endif
  31.  
  32. static void *myrealloc(void *data, size_t len, void *pw)
  33. {
  34.         UNUSED(pw);
  35.  
  36.         return realloc(data, len);
  37. }
  38.  
  39. static css_error event_handler(css_parser_event type,
  40.                 const parserutils_vector *tokens, void *pw)
  41. {
  42. #if !DUMP_EVENTS
  43.         UNUSED(type);
  44.         UNUSED(tokens);
  45.         UNUSED(pw);
  46. #else
  47.         int32_t ctx = 0;
  48.         const css_token *token;
  49.  
  50.         UNUSED(pw);
  51.  
  52.         printf("%s%s", tokens != NULL ? "  " : "", event_names[type]);
  53.  
  54.         if (tokens == NULL) {
  55.                 printf("\n");
  56.                 return CSS_OK;
  57.         }
  58.  
  59.         do {
  60.                 token = parserutils_vector_iterate(tokens, &ctx);
  61.                 if (token == NULL)
  62.                         break;
  63.  
  64.                 printf("\n    %d", token->type);
  65.  
  66.                 if (token->data.data != NULL)
  67.                         printf(" %.*s", (int) token->data.len, token->data.data);
  68.         } while (token != NULL);
  69.  
  70.         printf("\n");
  71. #endif
  72.  
  73.         return CSS_OK;
  74. }
  75.  
  76. int main(int argc, char **argv)
  77. {
  78.         css_parser_optparams params;
  79.         css_parser *parser;
  80.         FILE *fp;
  81.         size_t len;
  82. #define CHUNK_SIZE (4096)
  83.         uint8_t buf[CHUNK_SIZE];
  84.         css_error error;
  85.         int i;
  86.  
  87.         if (argc != 2) {
  88.                 printf("Usage: %s <filename>\n", argv[0]);
  89.                 return 1;
  90.         }
  91.  
  92.         for (i = 0; i < ITERATIONS; i++) {
  93.                 assert(css__parser_create("UTF-8", CSS_CHARSET_DICTATED,
  94.                                 myrealloc, NULL, &parser) == CSS_OK);
  95.  
  96.                 params.event_handler.handler = event_handler;
  97.                 params.event_handler.pw = NULL;
  98.                 assert(css__parser_setopt(parser, CSS_PARSER_EVENT_HANDLER,
  99.                                 &params) == CSS_OK);
  100.  
  101.                 fp = fopen(argv[1], "rb");
  102.                 if (fp == NULL) {
  103.                         printf("Failed opening %s\n", argv[1]);
  104.                         return 1;
  105.                 }
  106.  
  107.                 fseek(fp, 0, SEEK_END);
  108.                 len = ftell(fp);
  109.                 fseek(fp, 0, SEEK_SET);
  110.  
  111.                 while (len >= CHUNK_SIZE) {
  112.                         size_t read = fread(buf, 1, CHUNK_SIZE, fp);
  113.                         assert(read == CHUNK_SIZE);
  114.  
  115.                         error = css__parser_parse_chunk(parser, buf, CHUNK_SIZE);
  116.                         assert(error == CSS_OK || error == CSS_NEEDDATA);
  117.  
  118.                         len -= CHUNK_SIZE;
  119.                 }
  120.  
  121.                 if (len > 0) {
  122.                         size_t read = fread(buf, 1, len, fp);
  123.                         assert(read == len);
  124.  
  125.                         error = css__parser_parse_chunk(parser, buf, len);
  126.                         assert(error == CSS_OK || error == CSS_NEEDDATA);
  127.  
  128.                         len = 0;
  129.                 }
  130.  
  131.                 fclose(fp);
  132.  
  133.                 assert(css__parser_completed(parser) == CSS_OK);
  134.  
  135.                 css__parser_destroy(parser);
  136.  
  137.         }
  138.  
  139.         printf("PASS\n");
  140.        
  141.         return 0;
  142. }
  143.  
  144.