Exemple #1
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;
}
Exemple #2
0
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;
}