]> gitweb.hhaalo.de Git - mv_none_space.git/commitdiff
refactor main method
authorBastian Dehn <hhaalo@arcor.de>
Wed, 4 Mar 2026 19:46:19 +0000 (20:46 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Wed, 4 Mar 2026 19:46:19 +0000 (20:46 +0100)
src/config.h.in
src/main.c

index e7bb852c19d420962eb9b670e3e8cc2cf982da50..b2e66f51ef5c32f626e240f0deca1527817739d0 100644 (file)
@@ -1 +1,2 @@
+#define PROJECT_NAME "@PROJECT_NAME@"
 #define VERSION "@CMAKE_PROJECT_VERSION@"
\ No newline at end of file
index 46976a994ebf282f2893084235f9d7bf1782ddca..ac0e6ab1aa788053a1bec91de8147c62400f2e19 100644 (file)
@@ -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 <dir> [<command>]\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 <dir>\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