Exemplo n.º 1
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;
}
Exemplo n.º 2
0
int main4(int argc, char **argv)
{
    int show_match_count = 0;
    int match_firstletter = 0;
    int match_firstletter_only = 0;

    while(1)
    {
        static struct option long_options[] =  
        {
            {"help", no_argument, NULL, 'h'}, 
            {"show-match-count", no_argument, NULL, 'c'},
            {"firstletter", no_argument, NULL, 'f'},
            {"firstletter-only", no_argument, NULL, 'F'},
            {0,0,0,0}
        };
    
        int option_index = 0;
        int c = getopt_long(argc, argv, "hcfF", long_options, &option_index);

        if (c == -1) break;

        switch(c)
        {
            case 'h':
                printf("help\n");
                break;
            case 'c':
                show_match_count = 1;
                break;
            case 'f':
                match_firstletter = 1;
                break;
            case 'F':
                match_firstletter_only = 1;
                break;
            default:
                break;
        }
    }
    
    if (optind >= argc)
    {
        fprintf(stderr, "keyword missing\n");
        return 1;
    }
    
    char *keyword = argv[optind];

    linereader reader = linereader_create(STDIN_FILENO);
    int count;
    while ((count = linereader_readline(reader)) != -1)
    {
        const char *line = reader->line_buffer;
        int match_count = -1;
        if (!match_firstletter_only)
        {
            match_count = match_line_with_keyword(line, count, keyword, MatchModeFull);

            if (match_count == -1 && match_firstletter)
            {
                match_count = match_line_with_keyword(line, count, keyword, MatchModeFirstLetter);
            }

        }
        else
        {
            match_count = match_line_with_keyword(line, count, keyword, MatchModeFirstLetter);
        }

        if (match_count != -1)
        {
            if (show_match_count)
                printf("%d\t%.*s\n", match_count, count, line);
            else
                printf("%.*s\n", count, line);
        }
    }
    linereader_free(reader);
    return 0;
}
Exemplo n.º 3
0
Arquivo: hz2py.c Projeto: donliu/unp
int main(int argc, char **argv)
{
    int add_blank= 1;
    int polyphone_support = 1;
    int first_letter_only = 0;
    int convert_double_char = 1;
    int show_tones = 0;

    while(1)
    {
        static struct option long_options[] =  
        {
            {"disable-blank", no_argument, NULL, 'B'}, 
            {"disable-polyphone", no_argument, NULL, 'P'},
            {"firstletter-only", no_argument, NULL, 'f'},
            {"convert-double-char", no_argument, NULL, 'd'},
            {"show-tones", no_argument, NULL, 't'},
            {"help", no_argument, NULL, 'h'},
            {0,0,0,0}
        };

        int option_index = 0;
        int c = getopt_long(argc, argv, "BPfdth", long_options, &option_index);

        if (c == -1) break;

        switch(c)
        {
            case 'B':
                add_blank = 0;
                break;
            case 'P':
                polyphone_support = 0;
                break;
            case 'f':
                first_letter_only = 1;
                break;
            case 'd':
                convert_double_char = 1;
                break;
            case 't':
                show_tones = 1;
                break;
            case 'h':
                useage();
                return 0;
                break;
            default:
                useage();
                return 0;
                break;
        }
    }

    linereader reader = linereader_create(STDIN_FILENO);
    int count;
    while ((count = linereader_readline(reader)) != -1)
    {
        const char *line = reader->line_buffer;
        hz2py(line, count, add_blank, polyphone_support, first_letter_only, convert_double_char, show_tones);
    }
    linereader_free(reader);
    return 0;
}