WebVTT Parser in C

To implement WebVTT parser, I went through the spec written by Kyle and also the pseudo code he wrote for the parser. The document is really insightful. Me and Anh are working together to write the parser. Before I started working on it, he already had a version of it released in his Github repo. He used Raplh’s code for parser as basis for the final version. The version he has right now tests, for the most part, the webvtt files that have correct syntax. I have started to work on the coding part. For now, I am trying to implement the file structure part of the spec. Following is the method that I am trying to make follow the spec. I will post another blog once I have made some considerable progress and I am going to start working on another part of the spec.

int hasRequiredFileIdentifier(webvtt_parser *ctx) {

  char *p = ctx->buffer;

  // Check for signature

  //if the length of the buffer is less than 6 chracters, print error

  if (ctx->length < 6) {

    fprintf(stderr, “Too short. Not a webvtt file\n”);

    return 0;

  }

  //check if the file has BOM characters in the beginning, do following

  if (p[0] == (char)0xef && p[1] == (char)0xbb && p[2] == (char)0xbf) {

    fprintf(stderr, “Byte order mark\n”);

    //move the offset by 3

    ctx->offset += 3;

    //check again if the buffer has less than 9 charcters including BOM characters, print error

    if (ctx->length < 9) {

      fprintf(stderr, “Too short. Not a webvtt file\n”);

      return 0;

    }

  }

  if (memcmp(p + ctx->offset, “WEBVTT”, 6)) {

    fprintf(stderr, “Bad magic. Not a webvtt file?\n”);

    return 0;

  }

  ctx->offset += 6;

  /*

  if(memcmp(p + ctx->offset, “\t”, 7) || memcmp(p + ctx->offset, “\ “, 7)){

    fprintf(stderr, “Bad magic. Not a webvtt file?\n”);

    return 0;

  }

  */

  fprintf(stderr, “Found signature\n”);

  return 1;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s