commit 737edf3ca2d92b956364a7ec00d4708dfdaffdd4
parent eea428c2c0770069dc104d4ea7fcee8c70f7a3d4
Author: nibo <nibo@relim.de>
Date: Wed, 18 Dec 2024 20:33:35 +0100
Enable lorid to take multiple files on cmd line
Diffstat:
4 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/chordpro.c b/chordpro.c
@@ -4768,9 +4768,8 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config)
break;
}
prev_buf = buf;
- } else if (ferror(fp) != 0) {
- LOG_DEBUG("fread failed.");
- return NULL;
+ } else {
+ break;
}
}
int e = 0;
diff --git a/lorid.c b/lorid.c
@@ -24,6 +24,9 @@ main(int argc, char *argv[])
const char *chordpro_filepath = NULL;
char *config_filepath = NULL;
char *output = NULL;
+ char *file_content = NULL;
+ char *mem = NULL;
+ struct ChoSong **songs = NULL;
FILE *fp;
while ((o = getopt_long(argc, argv, "pc:o:vh", long_options, &option_index)) != -1) {
switch(o) {
@@ -62,10 +65,26 @@ main(int argc, char *argv[])
}
chordpro_filepath = argv[argc-1];
} else {
- util_log(LOG_ERR, "Provide only one file.");
- return 1;
+ int file_count = argc - optind;
+ int i;
+ size_t size;
+ fp = open_memstream(&mem, &size);
+ if (!fp) {
+ perror("open_memstream");
+ return 1;
+ }
+ for (i = argc-file_count; i<argc; i++) {
+ chordpro_filepath = argv[i];
+ file_content = file_read(chordpro_filepath);
+ fprintf(fp, "%s", file_content);
+ if (i != argc-1) {
+ fprintf(fp, "{new_song}\n");
+ }
+ free(file_content);
+ }
+ chordpro_filepath = NULL;
}
- struct ChoSong **songs = cho_songs_parse(fp, chordpro_filepath, config);
+ songs = cho_songs_parse(fp, chordpro_filepath, config);
if (!songs) {
LOG_DEBUG("cho_songs_parse failed.");
return 1;
@@ -78,6 +97,7 @@ main(int argc, char *argv[])
util_log(LOG_INFO, "Writing pdf to file: '%s'.", pdf_filename);
free(pdf_filename);
free(output);
+ free(mem);
cho_songs_free(songs);
config_free(config);
fclose(fp);
diff --git a/util.c b/util.c
@@ -273,6 +273,34 @@ file_type(const char *path)
}
char *
+file_read(const char *filepath)
+{
+ char *str = NULL;
+ char buf;
+ size_t read;
+ int i = 0;
+ FILE *fp = fopen(filepath, "r");
+ if (!fp) {
+ LOG_DEBUG("fopen failed.");
+ return NULL;
+ }
+ while (1) {
+ read = fread(&buf, 1, 1, fp);
+ if (read == 1) {
+ str = erealloc(str, (i+1) * sizeof(char));
+ str[i] = buf;
+ i++;
+ } else {
+ str = erealloc(str, (i+1) * sizeof(char));
+ str[i] = 0;
+ break;
+ }
+ }
+ fclose(fp);
+ return str;
+}
+
+char *
file_extension_replace_or_add(const char *old, const char *extension)
{
size_t extension_len = strlen(extension);
diff --git a/util.h b/util.h
@@ -53,6 +53,7 @@ int str_compare(const char *a, const char *b);
long str_to_number(const char *str);
enum FileType file_type(const char *path);
+char *file_read(const char *filepath);
char *file_extension_replace_or_add(const char *old, const char *extension);
char *filepath_add_ending_slash_if_missing(const char *path);