]> git.street.me.uk Git - andy/viking.git/commitdiff
[QA] Refactor general string processing to avoid pointer arithmetic.
authorRob Norris <rw_norris@hotmail.com>
Wed, 23 Jan 2013 20:14:39 +0000 (20:14 +0000)
committerRob Norris <rw_norris@hotmail.com>
Sat, 16 Feb 2013 19:35:26 +0000 (19:35 +0000)
src/preferences.c
src/util.c
src/util.h

index bcae6243bb0f7ee9bb30773acabc485e6451e41e..6763f100e4c10a89e74698c382348f2704d166c1 100644 (file)
@@ -26,6 +26,7 @@
 #include "preferences.h"
 #include "dir.h"
 #include "file.h"
 #include "preferences.h"
 #include "dir.h"
 #include "file.h"
+#include "util.h"
 
 // TODO: STRING_LIST
 // TODO: share code in file reading
 
 // TODO: STRING_LIST
 // TODO: share code in file reading
@@ -88,11 +89,12 @@ static gboolean preferences_load_from_file()
     while ( ! feof (f) ) {
       if (fgets(buf,sizeof(buf),f) == NULL)
         break;
     while ( ! feof (f) ) {
       if (fgets(buf,sizeof(buf),f) == NULL)
         break;
-      if ( split_string_from_file_on_equals(buf, &key, &val ) ) {
+      if ( split_string_from_file_on_equals ( buf, &key, &val ) ) {
         // if it's not in there, ignore it
         oldval = g_hash_table_lookup ( values, key );
         if ( ! oldval ) {
           g_free(key);
         // if it's not in there, ignore it
         oldval = g_hash_table_lookup ( values, key );
         if ( ! oldval ) {
           g_free(key);
+          g_free(val);
           continue;
         }
 
           continue;
         }
 
@@ -105,6 +107,7 @@ static gboolean preferences_load_from_file()
         g_hash_table_insert ( values, key, newval );
 
         g_free(key);
         g_hash_table_insert ( values, key, newval );
 
         g_free(key);
+        g_free(val);
         // change value
       }
     }
         // change value
       }
     }
index 2e3e28f751b2cc10162a75450fcb0c5f3b653375..92457b82526049bd3e44c22050a0a29a38300179 100644 (file)
@@ -126,24 +126,48 @@ GList * str_array_to_glist(gchar* data[])
   return g_list_reverse(gl);
 }
 
   return g_list_reverse(gl);
 }
 
-/* MAKES A COPY OF THE KEY!!! */
-gboolean split_string_from_file_on_equals ( gchar *buf, gchar **key, gchar **val )
+/**
+ * split_string_from_file_on_equals:
+ *
+ * @buf: the input string
+ * @key: newly allocated string that is before the '='
+ * @val: newly allocated string after the '='
+ *
+ * Designed for file line processing, so it ignores strings beginning with special
+ *  characters, such as '#'; returns false in such situations.
+ * Also returns false if no equals character is found
+ *
+ * e.g. if buf = "GPS.parameter=42"
+ *   key = "GPS.parameter"
+ *   val = "42"
+ */
+gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
 {
 {
-  gchar *eq_pos;
-  gint len;
-
   // comments, special characters in viking file format
   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
     return FALSE;
   // comments, special characters in viking file format
   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
     return FALSE;
-  eq_pos = strchr ( buf, '=' );
-  if ( ! eq_pos )
+
+  if ( ! strchr ( buf, '=' ) )
     return FALSE;
     return FALSE;
-  *key = g_strndup ( buf, eq_pos - buf );
-  *val = eq_pos + 1;
-  len = strlen(*val);
-  if ( len > 0 )
-    if ( (*val)[len - 1] == '\n' )
-      (*val) [ len - 1 ] = '\0'; /* cut off newline */
+
+  gchar **strv = g_strsplit ( buf, "=", 2 );
+
+  gint gi = 0;
+  gchar *str = strv[gi];
+  while ( str ) {
+       if ( gi == 0 )
+         *key = g_strdup ( str );
+       else
+         *val = g_strdup ( str );
+    gi++;
+    str = strv[gi];
+  }
+
+  g_strfreev ( strv );
+
+  // Remove newline from val and also any other whitespace
+  *key = g_strstrip ( *key );
+  *val = g_strstrip ( *val );
+
   return TRUE;
 }
   return TRUE;
 }
-
index b492b0d90b53f5befeb0fd97c93457c93e3c9bf5..8c10de45fe042aff609f48102a7ff8c58d126cd7 100644 (file)
@@ -36,7 +36,7 @@ gchar *uri_escape(gchar *str);
 
 GList * str_array_to_glist(gchar* data[]);
 
 
 GList * str_array_to_glist(gchar* data[]);
 
-gboolean split_string_from_file_on_equals ( gchar *buf, gchar **key, gchar **val );
+gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val );
 
 G_END_DECLS
 
 
 G_END_DECLS