]> granicus.if.org Git - fortune-mod/commitdiff
Extract a method or a function.
authorShlomi Fish <shlomif@shlomifish.org>
Thu, 30 Apr 2020 06:37:39 +0000 (09:37 +0300)
committerShlomi Fish <shlomif@shlomifish.org>
Thu, 30 Apr 2020 06:37:39 +0000 (09:37 +0300)
This is Refactoring / code cleanup.

See:

* https://refactoring.com/catalog/extractMethod.html

* https://en.wikipedia.org/wiki/Code_refactoring

* https://www.refactoring.com/

* https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/

Some small optimisations may have slipped in as well.

fortune-mod/util/fortune-util.h [new file with mode: 0644]
fortune-mod/util/randstr.c
fortune-mod/util/unstr.c

diff --git a/fortune-mod/util/fortune-util.h b/fortune-mod/util/fortune-util.h
new file mode 100644 (file)
index 0000000..26c2579
--- /dev/null
@@ -0,0 +1,22 @@
+#pragma once
+
+static void input_fn_2_data_fn(void)
+{
+    if (strlen(input_filename) > COUNT(data_filename) - 10)
+    {
+        perror("input is too long");
+        exit(1);
+    }
+    /* Hmm.  Don't output anything if we can help it.
+     * fprintf(stderr, "Input file: %s\n",input_filename); */
+    char *const extc = strrchr(input_filename, '.');
+    if (!extc)
+    {
+        sprintf(data_filename, "%s.dat", input_filename);
+    }
+    else
+    {
+        strcpy(data_filename, input_filename);
+        *extc = '\0';
+    }
+}
index 5b1f4cc4cbaa1501c51bf5b2405ddd1a97a8c87f..547aff1620f7a6294aa42450daf8c6ebacaffc61 100644 (file)
@@ -94,6 +94,8 @@ static FILE *Inf, *Dataf, *Outf;
 
 static off_t pos, Seekpts[2]; /* seek pointers to fortunes */
 
+#include "fortune-util.h"
+
 static void getargs(char *av[])
 {
     av += optind + 1;
@@ -101,23 +103,7 @@ static void getargs(char *av[])
     if (*av)
     {
         input_filename = *av;
-        if (strlen(input_filename) > COUNT(data_filename) - 10)
-        {
-            perror("input is too long");
-            exit(1);
-        }
-        /* Hmm.  Don't output anything if we can help it.
-         * fprintf(stderr, "Input file: %s\n",input_filename); */
-        char *const extc = strrchr(input_filename, '.');
-        if (!extc)
-        {
-            sprintf(data_filename, "%s.dat", input_filename);
-        }
-        else
-        {
-            strcpy(data_filename, input_filename);
-            *extc = '\0';
-        }
+        input_fn_2_data_fn();
     }
     else
         /*    {
index acb1e346be52e617ae2f052b757ba35352e77b91..8a226f42e477714fdc1e2ccca8998a7ab7defbc6 100644 (file)
@@ -84,6 +84,8 @@ static char new_delimiter_char = '\0';
 
 static FILE *Inf, *Dataf, *Outf;
 
+#include "fortune-util.h"
+
 /* ARGSUSED */
 static void getargs(int ac, char *av[])
 {
@@ -112,22 +114,7 @@ static void getargs(int ac, char *av[])
     if (*av)
     {
         input_filename = *av;
-        fprintf(stderr, "Input file: %s\n", input_filename);
-        if (strlen(input_filename) > COUNT(data_filename) - 10)
-        {
-            perror("input is too long");
-            exit(1);
-        }
-        char *const extc = strrchr(input_filename, '.');
-        if (!extc)
-        {
-            sprintf(data_filename, "%s.dat", input_filename);
-        }
-        else
-        {
-            strcpy(data_filename, input_filename);
-            *extc = '\0';
-        }
+        input_fn_2_data_fn();
         if (*++av)
         {
             strcpy(output_filename, *av);