Пример #1
0
static int
is_srt(const char *buf, size_t len)
{
  linereader_t lr;

  int n;
  int64_t start, stop;

  srt_skip_preamble(&buf, &len);

  linereader_init(&lr, buf, len);

  if(linereader_next(&lr) < 0)
    return 0;
  if(get_int(&lr, &n))
    return 0;
  if(linereader_next(&lr) < 0)
    return 0;
  if(get_srt_timestamp(&lr, &start, &stop))
    return 0;

  if(stop < start)
    return 0;

  return 1;
}
Пример #2
0
static ext_subtitles_t *
load_srt(const char *url, const char *buf, size_t len)
{
  int n;
  size_t tlen = 0;
  int64_t start, stop, pstart = -1, pstop = -1;
  linereader_t lr;
  ext_subtitles_t *es = calloc(1, sizeof(ext_subtitles_t));
  char *txt = NULL;
  size_t txtoff = 0;

  const int tag_flags = TEXT_PARSE_HTML_TAGS | TEXT_PARSE_HTML_ENTITIES |
    TEXT_PARSE_SLOPPY_TAGS | TEXT_PARSE_SUB_TAGS;

  srt_skip_preamble(&buf, &len);

  TAILQ_INIT(&es->es_entries);
  linereader_init(&lr, buf, len);
  while(1) {
    if((n = linereader_next(&lr)) < 0)
      break;

    if(get_srt_timestamp(&lr, &start, &stop) == 0) {
      if(txt != NULL && pstart != -1 && pstop != -1) {
	txt[txtoff] = 0;
	es_insert_text(es, txt, pstart, pstop, tag_flags);
	free(txt);
	txt = NULL;
	tlen = 0;
	txtoff = 0;
      }
      pstart = start;
      pstop  = stop;
      continue;
    }
    if(pstart == -1)
      continue;

    txt = realloc(txt, tlen + lr.ll + 1);
    memcpy(txt + tlen, lr.buf, lr.ll);
    txt[tlen + lr.ll] = 0x0a;
    if(lr.ll == 0 && tlen > 0)
      txtoff = tlen - 1;
    tlen += lr.ll + 1;
  }

  if(txt != NULL && pstart != -1 && pstop != -1) {
    txt[txtoff] = 0;
    es_insert_text(es, txt, pstart, pstop, tag_flags);
  }
  free(txt);
  return es;
}
Пример #3
0
int
main( int argc, char *argv[] )
{
   FILE       *out_a, *out_b;
   char       *h1, *h2, *s, *q;
   char       *id = NULL;
   char       *outname_a = NULL, *outname_b = NULL, *outdirname = NULL;
   int         i;
   struct options *o = options_new(  );
   struct tokenset *t = tokenset_new(  );
   struct fqreader *fq = fqreader_new( NULL );

   /* Get the command line options */
   options_cmdline( o, argc, argv );

   if ( _IS_NULL( o->outname ) || strlen( o->outname ) == 0 ) {
      o->outname = realloc( o->outname, 4 * sizeof ( char ) );
      strcpy( o->outname, "FQU" );
   }

   /* Create the output directory if necessary */
   outdirname = realloc( outdirname, sizeof ( char ) * ( 2 + strlen( o->outname ) ) );
   strcpy( outdirname, o->outname );
   if ( strchr( outdirname, '/' ) ) {            /* not the current directory */
      dirname( outdirname );
      mkdirp( outdirname, S_IRWXU | S_IRWXG | S_IRWXO );
   }


   /* Read the id files */
   for ( i = o->optind; i < argc; i++ ) {

      char       *line;
      struct linereader *z = linereader_new(  );

      linereader_init( z, argv[i] );

      if ( _IS_NULL( z ) ) {
         fprintf( stderr, "[ERROR] %s: Cannot open input file \"%s\"\n", _I_AM, argv[i] );
         exit( 1 );
      }

      while ( ( line = ( char * ) linereader_next( z ) ) ) {
         unsigned    len;

         stru_trim( line );

         if ( line[0] == '#' || stru_is_ws( line ) )
            continue;

         len = strcspn( line, "\f\n\r\t\v " );
         line[len] = '\0';

         tokenset_add( t, line );
      }

      linereader_free( z );
   }

   outname_a = realloc( outname_a, sizeof ( char ) * ( 6 + strlen( o->outname ) ) );
   strcpy( outname_a, o->outname );
   strcat( outname_a, "_A.fq" );
   out_a = fopen( outname_a, "wb" );
   if ( _IS_NULL( out_a ) ) {
      fprintf( stderr, "Could not open first output file \"%s\"\n", outname_a );
      exit( 1 );
   }

   outname_b = realloc( outname_b, sizeof ( char ) * ( 6 + strlen( o->outname ) ) );
   strcpy( outname_b, o->outname );
   strcat( outname_b, "_B.fq" );
   out_b = fopen( outname_b, "wb" );
   if ( _IS_NULL( out_b ) ) {
      fprintf( stderr, "Could not open second output file \"%s\"\n", outname_b );
      exit( 1 );
   }


   while ( fqreader_next( fq, &h1, &h2, &s, &q ) ) {

      utils_extract_id( &id, h1 );               /* get the identifier from header 1 */

      if ( tokenset_exists( t, id ) )
         fprintf( out_a, "@%s\n%s\n+%s\n%s\n", h1, s, h2, q );

      else
         fprintf( out_b, "@%s\n%s\n+%s\n%s\n", h1, s, h2, q );
   }

   fclose( out_a );
   fclose( out_b );

   _FREE( id );
   _FREE( outname_a );
   _FREE( outname_b );
   fqreader_free( fq );
   options_free( o );
   tokenset_free( t );

   return 0;
}