From: Bastian Dehn Date: Wed, 4 Mar 2026 19:46:19 +0000 (+0100) Subject: refactor main method X-Git-Tag: 1.1.7^2~2 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=31db2da2f0e057482d72f584e703f3f6198e9ce1;p=mv_none_space.git refactor main method --- diff --git a/src/config.h.in b/src/config.h.in index e7bb852..b2e66f5 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -1 +1,2 @@ +#define PROJECT_NAME "@PROJECT_NAME@" #define VERSION "@CMAKE_PROJECT_VERSION@" \ No newline at end of file diff --git a/src/main.c b/src/main.c index 46976a9..ac0e6ab 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,7 @@ #include "rename.h" #include "config.h" -glob_t get_files(const char* dir_path) +glob_t _get_files(const char* dir_path) { glob_t files; char* pattern = malloc(sizeof(char) * strlen(dir_path) + 3); @@ -20,7 +20,7 @@ glob_t get_files(const char* dir_path) return files; } -bool is_dir(const char* path) +bool _is_dir(const char* path) { struct stat statbuf; @@ -30,7 +30,7 @@ bool is_dir(const char* path) return isdir; } -void rename_file(const char* input, const char* output) +void _rename_file(const char* input, const char* output) { if (strcmp(input, output) == 0) return; @@ -39,17 +39,17 @@ void rename_file(const char* input, const char* output) rename(input, output); } -void rename_files(const char* dir_path, rename_ptr renamefunc) +int _rename_files(const char* dir_path, rename_ptr renamefunc) { - glob_t files = get_files(dir_path); + glob_t files = _get_files(dir_path); for (size_t i = 0; i < files.gl_pathc; i++) { char* output = strdup(files.gl_pathv[i]); output = renamefunc(&output); - rename_file(files.gl_pathv[i], output); + _rename_file(files.gl_pathv[i], output); - if (is_dir(output)) { + if (_is_dir(output)) { output = escape(&output); - rename_files(output, renamefunc); + _rename_files(output, renamefunc); } free(output); @@ -57,37 +57,54 @@ void rename_files(const char* dir_path, rename_ptr renamefunc) } globfree(&files); + return EXIT_SUCCESS; +} + +bool _validate_number_of_args(int argc) +{ + if (argc > 1 && argc < 4) + return true; + + printf("ERROR: %s []\n", PROJECT_NAME); + return false; +} + +bool _validate_command(const char* cmd) +{ + if (strcmp(cmd, "lower") == 0) + return true; + + if (strcmp(cmd, "point") == 0) + return true; + + if (strcmp(cmd, "revert") == 0) + return true; + + printf("ERROR: Unknown command\n"); + return false; } int main(int argc, char* argv[]) { printf("Version %s\n", VERSION); - if (argc < 2) { - printf("ERROR: %s \n", argv[0]); + + if (!_validate_number_of_args(argc)) return EXIT_FAILURE; - } const char* dir = argv[1]; - if (argc == 2) { - rename_files(dir, rename_string); - return EXIT_SUCCESS; - } + if (argc == 2) + return _rename_files(dir, rename_string); const char* cmd = argv[2]; - if (strcmp(cmd, "lower") == 0) { - rename_files(dir, rename_lower); - return EXIT_SUCCESS; - } + if (!_validate_command(cmd)) + return EXIT_FAILURE; - if (strcmp(cmd, "point") == 0) { - rename_files(dir, rename_point); - return EXIT_SUCCESS; - } + if (strcmp(cmd, "lower") == 0) + return _rename_files(dir, rename_lower); - if (strcmp(cmd, "revert") == 0) { - rename_files(dir, rename_revert); - return EXIT_SUCCESS; - } + if (strcmp(cmd, "point") == 0) + return _rename_files(dir, rename_point); - return EXIT_SUCCESS; + if (strcmp(cmd, "revert") == 0) + return _rename_files(dir, rename_revert); } \ No newline at end of file