cho2txt

Extract lyrics from chordpro files
git clone git://git.relim.de/cho2txt.git
Log | Files | Refs | README | LICENSE

commit 9fd4330a3cb959e03a6332aeac7d18e682dffc24
parent 31cf5f0c3953f6204b42b0bf567734e0710b0a4c
Author: nibo <kroekerrobin@gmail.com>
Date:   Fri,  7 Jul 2023 19:27:26 +0200

Filter out grids and tabs

Diffstat:
Mcho2txt.c | 138++++++++++++++++++++++++++++---------------------------------------------------
Acho2txt.h | 49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 98 insertions(+), 89 deletions(-)

diff --git a/cho2txt.c b/cho2txt.c @@ -5,20 +5,7 @@ #include <stdbool.h> #include <string.h> #include <getopt.h> - -enum print -{ - PRINT_NO, - PRINT_TITLE, - PRINT_TITLE_DIRECTIVE -}; - -enum grid -{ - GRID_NO, - GRID_START, - GRID_END -}; +#include "cho2txt.h" void printHelp() { @@ -29,6 +16,34 @@ void printHelp() printf("%s", help); } +bool isDirective(enum direc d, const char *str) +{ + bool isDirective = false; + size_t len; + int i = 0; + int k = 1; + char **names = (char **)dirs[d].names; + while (names[i] != NULL) + { + isDirective = true; + len = strlen(names[i]); + while (k <= len) + { + if (names[i][k-1] != str[k]) + { + isDirective = false; + break; + } + k++; + } + if (isDirective) + return true; + k = 1; + i++; + } + return false; +} + char *trim(char *text) { char *trimmedText = NULL; @@ -99,69 +114,10 @@ char *parseTitle(const char *directive) return trim(title); } -bool isTitle(const char *directive) -{ - static const char title[] = "title:"; - int t = 0; - for (int i=0; i<strlen(directive); i++) - { - if (i > 0 && t < 7) - { - if (directive[i] != title[t]) - return false; - t++; - if (t == 6) - return true; - } - } -} - -enum grid isGrid(const char *directive) -{ - enum grid g = GRID_NO; - static const char start[] = "start_of_grid"; - static const char end[] = "end_of_grid"; - size_t directiveLen = strlen(directive); - int i = 1; - while (i<directiveLen) - { - if (directive[i] != start[i-1]) - { - g = GRID_NO; - break; - } - i++; - if (i == 14) - { - g = GRID_START; - break; - } - } - if (g != GRID_NO) - return g; - i = 1; - while (i<directiveLen) - { - if (directive[i] != end[i-1]) - { - g = GRID_NO; - break; - } - i++; - if (i == 12) - { - g = GRID_END; - break; - } - } - return g; -} - char *extractLyrics(int fd, enum print printTitle) { char *text = malloc(sizeof(char)); - int *i = malloc(sizeof(int)); - *i = 0; + int i = 0; int d = 0; char buf; bool isLyric = true; @@ -189,9 +145,9 @@ char *extractLyrics(int fd, enum print printTitle) { if (buf == '\n' && !isLyricInLine && isDirectiveInLine) goto IGNORE; - text[*i] = buf; - (*i)++; - text = realloc(text, ((*i)+1) * sizeof(char)); + text[i] = buf; + i++; + text = realloc(text, (i+1) * sizeof(char)); isLyricInLine = true; IGNORE: } @@ -209,12 +165,17 @@ char *extractLyrics(int fd, enum print printTitle) isLyric = true; directive = realloc(directive, (d+1) * sizeof(char)); directive[d] = 0; - g = isGrid(directive); - if (g == GRID_START) + if ( + isDirective(DIREC_GRID_START, directive) || + isDirective(DIREC_TAB_START, directive) + ) isLyric = false; - else if (g == GRID_END) + else if ( + isDirective(DIREC_GRID_END, directive) || + isDirective(DIREC_TAB_END, directive) + ) isLyric = true; - if (printTitle > 0 && isTitle(directive)) + if (printTitle > 0 && isDirective(DIREC_TITLE, directive)) { char *title; if (printTitle == PRINT_TITLE) @@ -226,14 +187,14 @@ char *extractLyrics(int fd, enum print printTitle) } for (int k=0; k<strlen(title); k++) { - text[*i] = title[k]; - (*i)++; - text = realloc(text, ((*i)+1) * sizeof(char)); + text[i] = title[k]; + i++; + text = realloc(text, (i+1) * sizeof(char)); } free(title); - text[*i] = '\n'; - (*i)++; - text = realloc(text, ((*i)+1) * sizeof(char)); + text[i] = '\n'; + i++; + text = realloc(text, (i+1) * sizeof(char)); } d = 0; free(directive); @@ -251,8 +212,7 @@ char *extractLyrics(int fd, enum print printTitle) else break; } - text[*i] = '\0'; - free(i); + text[i] = '\0'; return text; } diff --git a/cho2txt.h b/cho2txt.h @@ -0,0 +1,49 @@ +enum print +{ + PRINT_NO, + PRINT_TITLE, + PRINT_TITLE_DIRECTIVE +}; + +enum grid +{ + GRID_NO, + GRID_START, + GRID_END +}; + +enum tab +{ + TAB_NO, + TAB_START, + TAB_END +}; + +enum direc +{ + DIREC_TITLE, + DIREC_GRID_START, + DIREC_GRID_END, + DIREC_TAB_START, + DIREC_TAB_END +}; + +struct directive +{ + enum direc type; + const void *names; +}; + +static const char *title[] = { "title:", NULL }; +static const char *gridStarts[] = { "start_of_grid", NULL }; +static const char *gridEnds[] = { "end_of_grid", NULL }; +static const char *tabStarts[] = { "start_of_tab", "sot", NULL }; +static const char *tabEnds[] = { "end_of_tab", "eot", NULL }; + +static const struct directive dirs[] = { + { DIREC_TITLE, title }, + { DIREC_GRID_START, gridStarts }, + { DIREC_GRID_END, gridEnds }, + { DIREC_TAB_START, tabStarts }, + { DIREC_TAB_END, tabEnds }, +};