From 2106a76e90a768ddda4fe08ae0c589ce1bc2e33f Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Fri, 26 Nov 2010 00:12:57 -0500
Subject: [PATCH] [INI] Refactoring error reporting

Minor cleanup in the error reporting.
---
 ini/ini_config.h      |    8 --------
 ini/ini_config_priv.h |    3 +++
 ini/ini_configobj.h   |    8 --------
 ini/ini_defines.h     |    6 ++++++
 ini/ini_fileobj.c     |    3 ++-
 ini/ini_print.c       |   46 +++++++++++++++++++++++++++++++++++-----------
 6 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/ini/ini_config.h b/ini/ini_config.h
index c69e235dee73528fe7dc5f6064a0f75b7a77cc2b..3b75012a04d0e963d584d15716855b1b21eed545 100644
--- a/ini/ini_config.h
+++ b/ini/ini_config.h
@@ -498,14 +498,6 @@ struct parse_error {
  * @{
  */
 
-/** @brief Function to return a parsing error as a string.
- *
- * @param[in] parsing_error    Error code for the parsing error.
- *
- * @return Error string.
- */
-const char *parsing_error_str(int parsing_error);
-
 
 /**
  * @brief Read configuration information from a file.
diff --git a/ini/ini_config_priv.h b/ini/ini_config_priv.h
index 9185e14ede5567dd7b9d848d0967c3ec88ad2434..1880c3ff4caa91e7ed3a37e445a20294e76d8037 100644
--- a/ini/ini_config_priv.h
+++ b/ini/ini_config_priv.h
@@ -78,4 +78,7 @@ void ini_cleanup_cb(const char *property,
                     int length,
                     void *custom_data);
 
+/* Get parsing error */
+const char *ini_get_error_str(int parsing_error, int family);
+
 #endif
diff --git a/ini/ini_configobj.h b/ini/ini_configobj.h
index 8ecb148bb2296f5332db63592c089066972a8b08..81578c0c442896f69d62c4ba2c56cbd9523228f3 100644
--- a/ini/ini_configobj.h
+++ b/ini/ini_configobj.h
@@ -142,14 +142,6 @@ int ini_config_parse(struct ini_cfgfile *file_ctx,
 void ini_print_errors(FILE *file, char **error_list);
 
 
-/* TBD rename the function */
-/** @brief Function to return a parsing error as a string.
- *
- * @param[in] parsing_error    Error code for the parsing error.
- *
- * @return Error string.
- */
-const char *parsing_error_str(int parsing_error);
 
 
 /* Merge two configurations together creating a new one */
diff --git a/ini/ini_defines.h b/ini/ini_defines.h
index bead998b125cb2500aa9c8040a03b65336594790..1d03835c3262c0ac09c43f6225840ce4142592a7 100644
--- a/ini/ini_defines.h
+++ b/ini/ini_defines.h
@@ -95,6 +95,12 @@
  */
 #define COL_CLASS_INI_META        COL_CLASS_INI_BASE + 4
 
+/* Family of errors */
+#define INI_FAMILY_PARSING      0
+#define INI_FAMILY_VALIDATION   1
+#define INI_FAMILY_GRAMMAR      2
+
+
 
 /* Different error string functions can be passed as callbacks */
 typedef const char * (*error_fn)(int error);
diff --git a/ini/ini_fileobj.c b/ini/ini_fileobj.c
index 117608c334084419cc5460e049e4f6f10b9f864d..29d5f90de69d88ffa82ffbad353c5736607183b8 100644
--- a/ini/ini_fileobj.c
+++ b/ini/ini_fileobj.c
@@ -241,7 +241,8 @@ int ini_config_get_errors(struct ini_cfgfile *file_ctx,
                      col_get_item_property(item, NULL),
                      pe->error,
                      pe->line,
-                     parsing_error_str(pe->error));
+                     ini_get_error_str(pe->error,
+                                       INI_FAMILY_PARSING));
 
             errlist[count] = line;
             count++;
diff --git a/ini/ini_print.c b/ini/ini_print.c
index f12b27e85286460ed3c586581e10ba7de03d457f..0430976cd56e89313cb386b99ad387263498c639 100644
--- a/ini/ini_print.c
+++ b/ini/ini_print.c
@@ -66,7 +66,7 @@
 
 
 /* Function to return parsing error */
-const char *parsing_error_str(int parsing_error)
+static const char *parsing_error_str(int parsing_error)
 {
     const char *placeholder= _("Unknown pasing error.");
     const char *str_error[] = { _("Data is too long."),
@@ -109,7 +109,7 @@ const char *parsing_error_str(int parsing_error)
  * @return Error string.
  */
 
-const char *grammar_error_str(int grammar_error)
+static const char *grammar_error_str(int grammar_error)
 {
     const char *placeholder= _("Unknown grammar error.");
     /* THIS IS A TEMPORARY PLACEHOLDER !!!! */
@@ -150,7 +150,7 @@ const char *grammar_error_str(int grammar_error)
  *
  * @return Error string.
  */
-const char *validation_error_str(int validation_error)
+static const char *validation_error_str(int validation_error)
 {
     const char *placeholder= _("Unknown validation error.");
     /* THIS IS A TEMPORARY PLACEHOLDER !!!! */
@@ -170,7 +170,30 @@ const char *validation_error_str(int validation_error)
             return str_error[validation_error-1];
 }
 
+/* Wrapper to print errors */
+const char *ini_get_error_str(int error, int family)
+{
+    const char *val;
+    TRACE_FLOW_ENTRY();
+
+    switch(family) {
+    case INI_FAMILY_PARSING:
+        val = parsing_error_str(error);
+        break;
+    case INI_FAMILY_VALIDATION:
+        val = validation_error_str(error);
+        break;
+    case INI_FAMILY_GRAMMAR:
+        val = grammar_error_str(error);
+        break;
+    default:
+        val = _("Unknown error category.");
+        break;
+    }
 
+    TRACE_FLOW_EXIT();
+    return val;
+}
 
 /* Internal function that prints errors */
 static void print_error_list(FILE *file,
@@ -180,7 +203,7 @@ static void print_error_list(FILE *file,
                              char *failed_to_process,
                              char *error_header,
                              char *line_format,
-                             error_fn error_function)
+                             int family)
 {
     struct collection_iterator *iterator;
     int error;
@@ -235,10 +258,11 @@ static void print_error_list(FILE *file,
             /* Put error into provided format */
             pe = (struct parse_error *)(col_get_item_data(item));
             fprintf(file, line_format,
-                    col_get_item_property(item, NULL),      /* Error or warning */
-                    pe->error,                          /* Error */
-                    pe->line,                           /* Line */
-                    error_function(pe->error));         /* Error str */
+                    col_get_item_property(item, NULL), /* Error or warning */
+                    pe->error,                         /* Error */
+                    pe->line,                          /* Line */
+                    ini_get_error_str(pe->error,
+                                      family));        /* Error str */
         }
 
     }
@@ -260,7 +284,7 @@ void print_file_parsing_errors(FILE *file,
                      FAILED_TO_PROCCESS,
                      ERROR_HEADER,
                      LINE_FORMAT,
-                     parsing_error_str);
+                     INI_FAMILY_PARSING);
 }
 
 
@@ -290,7 +314,7 @@ void print_grammar_errors(FILE *file,
                      FAILED_TO_PROC_G,
                      ERROR_HEADER_G,
                      LINE_FORMAT,
-                     grammar_error_str);
+                     INI_FAMILY_GRAMMAR);
 }
 
 /* Print errors and warnings that were detected while validating INI file.
@@ -319,7 +343,7 @@ void print_validation_errors(FILE *file,
                      FAILED_TO_PROC_V,
                      ERROR_HEADER_V,
                      LINE_FORMAT,
-                     validation_error_str);
+                     INI_FAMILY_VALIDATION);
 }
 
 /* Print errors and warnings that were detected while parsing
-- 
1.5.5.6

