]> Kevux Git Server - controller/commitdiff
Update: Better utilize functionality of C for extern types.
authorKevin Day <kevin@kevux.org>
Tue, 9 Apr 2024 01:37:34 +0000 (20:37 -0500)
committerKevin Day <kevin@kevux.org>
Tue, 9 Apr 2024 01:37:34 +0000 (20:37 -0500)
I am creating a string pointer and passing it through a structure to dynamically pass different progam names for each program.
This is fine for many languages that are not C/C++.

For C/C++, this can be done in a cleaner and more efficient way.
The extern type can be declared in the main project and use that.
Then, the final binary is the only case where the actual implementation of the extern variable must exist.
That means each program (init and controller) will define their own program name string.
No extra memory in the structure needs to be used.
This is cleaner, simpler, and more maintainable.

sources/c/controller/main.c
sources/c/controller/string.c
sources/c/controller/string.h
sources/c/init/main.c
sources/c/init/string.c
sources/c/init/string.h
sources/c/main/common/string.h
sources/c/main/common/type.h
sources/c/main/print/message.c

index 2d413c667e317d4d58f08b23cabc83179f9ba9e6..4483d448977b78f2b64723a31812e527f474e972 100644 (file)
@@ -15,9 +15,6 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) {
   data.program.output.custom = (void *) &data;
   data.program.warning.custom = (void *) &data;
 
-  data.setting.program_name = &controller_controller_program_name_s;
-  data.setting.program_name_long = &controller_controller_program_name_long_s;
-
   f_console_parameter_t parameters[] = controller_console_parameter_t_initialize;
 
   data.program.parameters.array = parameters;
index c7b2c2b9b4760996cd340cf3fd9b16e793f95fdf..3bad69b5877c0e4cffd2441029417e126579c023 100644 (file)
@@ -4,10 +4,10 @@
 extern "C" {
 #endif
 
-#ifndef _di_controller_controller_program_name_s_
-  const f_string_static_t controller_controller_program_name_s = macro_f_string_static_t_initialize_1(CONTROLLER_controller_program_name_s, 0, CONTROLLER_controller_program_name_s_length);
-  const f_string_static_t controller_controller_program_name_long_s = macro_f_string_static_t_initialize_1(CONTROLLER_controller_program_name_long_s, 0, CONTROLLER_controller_program_name_long_s_length);
-#endif // _di_controller_controller_program_name_s_
+#ifndef _di_controller_program_name_s_
+  const f_string_static_t controller_program_name_s = macro_f_string_static_t_initialize_1(CONTROLLER_program_name_s, 0, CONTROLLER_program_name_s_length);
+  const f_string_static_t controller_program_name_long_s = macro_f_string_static_t_initialize_1(CONTROLLER_program_name_long_s, 0, CONTROLLER_program_name_long_s_length);
+#endif // _di_controller_program_name_s_
 
 #ifdef __cplusplus
 } // extern "C"
index ab36bb1a9b4d6b92f96502d8afbd8cb5df1f06c6..2f7ed971064889936127b819f900ce7219739909 100644 (file)
@@ -19,16 +19,13 @@ extern "C" {
 /**
  * The program name.
  */
-#ifndef _di_controller_controller_program_name_s_
-  #define CONTROLLER_controller_program_name_s      "controller"
-  #define CONTROLLER_controller_program_name_long_s "Controller"
+#ifndef _di_controller_program_name_s_
+  #define CONTROLLER_program_name_s      "controller"
+  #define CONTROLLER_program_name_long_s "Controller"
 
-  #define CONTROLLER_controller_program_name_s_length      10
-  #define CONTROLLER_controller_program_name_long_s_length 10
-
-  extern const f_string_static_t controller_controller_program_name_s;
-  extern const f_string_static_t controller_controller_program_name_long_s;
-#endif // _di_controller_controller_program_name_s_
+  #define CONTROLLER_program_name_s_length      10
+  #define CONTROLLER_program_name_long_s_length 10
+#endif // _di_controller_program_name_s_
 
 #ifdef __cplusplus
 } // extern "C"
index fc670d3e74d6801e4f94178749b6d6f81c68572a..1aff3da60c58a7400940a4371b97063f7060db6d 100644 (file)
@@ -15,9 +15,6 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) {
   data.program.output.custom = (void *) &data;
   data.program.warning.custom = (void *) &data;
 
-  data.setting.program_name = &controller_init_program_name_s;
-  data.setting.program_name_long = &controller_init_program_name_long_s;
-
   f_console_parameter_t parameters[] = controller_console_parameter_t_initialize;
 
   data.program.parameters.array = parameters;
index ff1fd9e4124b2a1ffa895d1a845706dfe19a5d50..2884a041d7b0839e5d618c9e7070f64b07817b6a 100644 (file)
@@ -4,10 +4,10 @@
 extern "C" {
 #endif
 
-#ifndef _di_controller_init_program_name_s_
-  const f_string_static_t controller_init_program_name_s = macro_f_string_static_t_initialize_1(CONTROLLER_init_program_name_s, 0, CONTROLLER_init_program_name_s_length);
-  const f_string_static_t controller_init_program_name_long_s = macro_f_string_static_t_initialize_1(CONTROLLER_init_program_name_long_s, 0, CONTROLLER_init_program_name_long_s_length);
-#endif // _di_controller_init_program_name_s_
+#ifndef _di_controller_program_name_s_
+  const f_string_static_t controller_program_name_s = macro_f_string_static_t_initialize_1(CONTROLLER_program_name_s, 0, CONTROLLER_program_name_s_length);
+  const f_string_static_t controller_program_name_long_s = macro_f_string_static_t_initialize_1(CONTROLLER_program_name_long_s, 0, CONTROLLER_program_name_long_s_length);
+#endif // _di_controller_program_name_s_
 
 #ifdef __cplusplus
 } // extern "C"
index 5f8e82dd270bfe7523656bf62a6c7f19f5b0560e..c9dd068303b68cf73296b77299fcfd9342de0a04 100644 (file)
@@ -19,16 +19,13 @@ extern "C" {
 /**
  * The program name.
  */
-#ifndef _di_controller_init_program_name_s_
-  #define CONTROLLER_init_program_name_s      "init"
-  #define CONTROLLER_init_program_name_long_s "Init"
+#ifndef _di_controller_program_name_s_
+  #define CONTROLLER_program_name_s      "init"
+  #define CONTROLLER_program_name_long_s "Init"
 
-  #define CONTROLLER_init_program_name_s_length      4
-  #define CONTROLLER_init_program_name_long_s_length 4
-
-  extern const f_string_static_t controller_init_program_name_s;
-  extern const f_string_static_t controller_init_program_name_long_s;
-#endif // _di_controller_init_program_name_s_
+  #define CONTROLLER_program_name_s_length      4
+  #define CONTROLLER_program_name_long_s_length 4
+#endif // _di_controller_program_name_s_
 
 #ifdef __cplusplus
 } // extern "C"
index 5d2ea633a7f3d998b90bb904191f36e062ee1d78..406c7b57ccd865e716e75c7968217895b561a966 100644 (file)
@@ -46,6 +46,16 @@ extern "C" {
 #endif // _di_controller_program_version_s_
 
 /**
+ * The program name.
+ *
+ * The implementation for this is defined within the individual programs.
+ */
+#ifndef _di_controller_program_name_s_
+  extern const f_string_static_t controller_program_name_s;
+  extern const f_string_static_t controller_program_name_long_s;
+#endif // _di_controller_program_name_s_
+
+/**
  * The main program parameters.
  */
 #ifndef _di_controller_parameter_s_
index 5f59d747601bdb1febff4b4b0707e2f925952c24..a3d3c4b5e40eb661285fb1ab37faed781c19678e 100644 (file)
@@ -42,9 +42,6 @@ extern "C" {
  *
  * status_signal: A status used eclusively by the threaded signal handler.
  * state:         The state data used when processing the FSS data.
- *
- * program_name:      The short program name.
- * program_name_long: The long program name.
  */
 #ifndef _di_controller_setting_t_
   typedef struct {
@@ -52,9 +49,6 @@ extern "C" {
 
     f_status_t status_signal;
     f_state_t state;
-
-    const f_string_static_t *program_name;
-    const f_string_static_t *program_name_long;
   } controller_setting_t;
 
   #define controller_setting_t_initialize \
@@ -62,8 +56,6 @@ extern "C" {
       controller_main_flag_none_e, \
       F_okay, \
       f_state_t_initialize, \
-      0, \
-      0, \
     }
 #endif // _di_controller_setting_t_
 
index 42f1e771b74defdbf2b4eb7acf9e4859b6255246..6a3ee162fd3b5ba18eabc35d0f5eae47b926b00f 100644 (file)
@@ -13,7 +13,7 @@ extern "C" {
 
     f_file_stream_lock(print->to);
 
-    fll_program_print_help_header(print, *main->setting.program_name_long, controller_program_version_s);
+    fll_program_print_help_header(print, controller_program_name_long_s, controller_program_version_s);
 
     fll_program_print_help_option_standard(print);
 
@@ -32,7 +32,7 @@ extern "C" {
 
     f_print_dynamic_raw(f_string_eol_s, print->to);
 
-    fll_program_print_help_usage(print, *main->setting.program_name, f_string_empty_s);
+    fll_program_print_help_usage(print, controller_program_name_s, f_string_empty_s);
 
     fl_print_format("%r  When both the %[%r%r%] parameter and the", print->to, f_string_eol_s, print->set->notable, f_console_symbol_long_normal_s, controller_long_simulate_s, print->set->notable);
     fl_print_format(" %[%r%r%] parameter are specified, then additional information on each would be executed rule is printed but no simulation is performed.%r%r", print->to, print->set->notable, f_console_symbol_long_normal_s, controller_long_validate_s, print->set->notable, f_string_eol_s, f_string_eol_s);