[cairo-commit] roadster/src main.c, 1.34, 1.35 road.c, 1.9, 1.10 road.h, 1.6, 1.7 scenemanager.c, 1.16, 1.17 util.c, 1.19, 1.20 util.h, 1.19, 1.20

Ian McIntosh commit at pdx.freedesktop.org
Tue Oct 25 22:04:55 PDT 2005


Committed by: ian

Update of /cvs/cairo/roadster/src
In directory gabe:/tmp/cvs-serv32137/src

Modified Files:
	main.c road.c road.h scenemanager.c util.c util.h 
Log Message:
	* src/road.c:
	* src/util.c:
	* data/road-suffix-list.txt: Moved list of road suffixes to data file.


Index: main.c
===================================================================
RCS file: /cvs/cairo/roadster/src/main.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- main.c	18 Oct 2005 03:05:25 -0000	1.34
+++ main.c	26 Oct 2005 05:04:53 -0000	1.35
@@ -32,6 +32,7 @@
 #include <gtk/gtk.h>
 #include "main.h"
 #include "gui.h"
+#include "road.h"
 #include "db.h"
 #include "map.h"
 #include "map_style.h"
@@ -141,6 +142,9 @@
 	g_free(pszApplicationDir);
 #endif
 
+	g_print("initializing road\n");
+	road_init();
+
 	g_print("initializing map styles\n");
 	map_style_init();
 	g_print("initializing map\n");

Index: road.c
===================================================================
RCS file: /cvs/cairo/roadster/src/road.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- road.c	5 Oct 2005 06:09:36 -0000	1.9
+++ road.c	26 Oct 2005 05:04:53 -0000	1.10
@@ -23,6 +23,31 @@
 
 #include <gtk/gtk.h>
 #include "road.h"
+#include "util.h"
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#define	ROAD_SUFFIX_LIST_FILE_NAME ("road-suffix-list.txt")
+#define ROAD_SUFFIX_LIST_MIN_WORDS (2)
+
+struct {
+	GArray* pRoadNameSuffixArray;	// an array of null-terminated vectors (an array of gchar* with the last being NULL)
+} g_Road = {0};
+
+void road_init()
+{
+	gchar* pszPath = g_strdup_printf(PACKAGE_SOURCE_DIR"/data/%s", ROAD_SUFFIX_LIST_FILE_NAME);
+	if(util_load_array_of_string_vectors(pszPath, &(g_Road.pRoadNameSuffixArray), ROAD_SUFFIX_LIST_MIN_WORDS) == FALSE) {
+		g_free(pszPath);
+		pszPath = g_strdup_printf(PACKAGE_DATA_DIR"/data/%s", ROAD_SUFFIX_LIST_FILE_NAME);
+		if(util_load_array_of_string_vectors(pszPath, &(g_Road.pRoadNameSuffixArray), ROAD_SUFFIX_LIST_MIN_WORDS) == FALSE) {
+			g_error("Unable to load %s", ROAD_SUFFIX_LIST_FILE_NAME);
+		}
+	}
+	g_free(pszPath);
+}
 
 struct {
 	gchar* pszLong;
@@ -158,8 +183,7 @@
 	{"Tunl", ROAD_SUFFIX_TUNNEL},
 	
 	{"Walk", ROAD_SUFFIX_WALK},
-	{"Walk", ROAD_SUFFIX_WALK},
-	
+
 	{"Branch", ROAD_SUFFIX_BRANCE},
 	{"Br", ROAD_SUFFIX_BRANCE},
 	
@@ -193,24 +217,29 @@
 
 const gchar* road_suffix_itoa(gint nSuffixID, ESuffixLength eSuffixLength)
 {
-	if(nSuffixID >= ROAD_SUFFIX_FIRST && nSuffixID <= ROAD_SUFFIX_LAST) {
-		if(eSuffixLength == ROAD_SUFFIX_LENGTH_SHORT) {
-			return g_RoadNameSuffix[nSuffixID].pszShort;
-		}
-		else {
-			return g_RoadNameSuffix[nSuffixID].pszLong;			
-		}
+	if(nSuffixID < g_Road.pRoadNameSuffixArray->len) {
+		//g_debug("looking up suffixID %d", nSuffixID);
+		gchar** apszWords = g_array_index(g_Road.pRoadNameSuffixArray, gchar**, nSuffixID);
+		g_assert(eSuffixLength == 0 || eSuffixLength == 1);	// we know there are at least 2 words in the array (see call to util_load_name_list)
+		return apszWords[eSuffixLength];
+	}
+	else {
+		return "";
 	}
-	if(nSuffixID != ROAD_SUFFIX_NONE) return "???";
-	return "";
 }
 
 gboolean road_suffix_atoi(const gchar* pszSuffix, gint* pReturnSuffixID)
 {
+	g_assert(pszSuffix != NULL);
+	g_assert(pReturnSuffixID != NULL);
+
 	gint i;
-	for(i=0 ; i<G_N_ELEMENTS(g_RoadNameSuffixLookup) ; i++) {
-		if(g_ascii_strcasecmp(pszSuffix, g_RoadNameSuffixLookup[i].pszName) == 0) {
-			*pReturnSuffixID = g_RoadNameSuffixLookup[i].nID;
+	for(i=0 ; i<g_Road.pRoadNameSuffixArray->len ; i++) {
+		gchar** apszWords = g_array_index(g_Road.pRoadNameSuffixArray, gchar**, i);
+
+		// Does this list of words contain the string?
+		if(util_find_string_in_string_vector(pszSuffix, apszWords, NULL)) {
+			*pReturnSuffixID = i;
 			return TRUE;
 		}
 	}

Index: road.h
===================================================================
RCS file: /cvs/cairo/roadster/src/road.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- road.h	1 Oct 2005 01:42:12 -0000	1.6
+++ road.h	26 Oct 2005 05:04:53 -0000	1.7
@@ -26,6 +26,8 @@
 
 #include "map.h"
 
+void road_init();
+
 typedef struct {
 	GArray* pMapPointsArray;
 
@@ -40,8 +42,8 @@
 
 // ESuffixLength
 typedef enum {
-	ROAD_SUFFIX_LENGTH_SHORT,
-	ROAD_SUFFIX_LENGTH_LONG
+	ROAD_SUFFIX_LENGTH_LONG,
+	ROAD_SUFFIX_LENGTH_SHORT
 } ESuffixLength;
 
 enum ERoadNameSuffix {			// these can't change once stored in DB

Index: scenemanager.c
===================================================================
RCS file: /cvs/cairo/roadster/src/scenemanager.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- scenemanager.c	5 Oct 2005 06:09:36 -0000	1.16
+++ scenemanager.c	26 Oct 2005 05:04:53 -0000	1.17
@@ -22,7 +22,7 @@
  */
 
 /*
-Goals:
+Purpose of scenemanager.c:
  - Keep text labels and other screen objects from overlapping
  - Prevent the same text from showing up too often (currently not more than once)
 */

Index: util.c
===================================================================
RCS file: /cvs/cairo/roadster/src/util.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- util.c	20 Oct 2005 06:56:22 -0000	1.19
+++ util.c	26 Oct 2005 05:04:53 -0000	1.20
@@ -21,6 +21,8 @@
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#include <gnome-vfs-2.0/libgnomevfs/gnome-vfs.h>
+
 #include "main.h"
 #include "util.h"
 
@@ -615,3 +617,90 @@
 	}
 	return eDirection;
 }
+
+// Load a \n separated list of \t separated names as a GArray of gchar* vectors... :)
+gboolean util_load_array_of_string_vectors(const gchar* pszPath, GArray** ppReturnArray, gint nMinVectorLength)
+{
+	g_assert(pszPath != NULL);
+	g_assert(ppReturnArray != NULL);
+	g_assert(*ppReturnArray == NULL);	// require pointer to NULL pointer
+
+	//
+	// 1. Load entire file into memory.  XXX: Better to load one line at a time?
+	//
+	gchar* pszFileContent = NULL;
+	if(gnome_vfs_read_entire_file(pszPath, NULL, &pszFileContent) != GNOME_VFS_OK) {
+		return FALSE;
+	}
+
+	//
+	// 2. Split into lines.
+	//
+	gchar** apszLines = g_strsplit(pszFileContent, "\n", -1);		// -1 = no maximum
+	gint nNumLines = g_strv_length(apszLines);
+
+	//
+	// 3. Create array and add 0 element
+	//
+	GArray* pNewArray = g_array_sized_new(FALSE, FALSE, sizeof(gchar**), nNumLines);
+
+	{
+		// Make the first nMinVectorLength indexes point to "" and the last NULL
+		gchar** apszFirst = g_malloc(sizeof(gchar*) * (nMinVectorLength+1));
+		gint iVector;
+		for(iVector=0 ; iVector<nMinVectorLength ; iVector++) {
+			apszFirst[iVector] = g_strdup("");	// Just so we don't mix allocated and static strings
+		}
+		apszFirst[nMinVectorLength] = NULL;
+		g_array_append_val(pNewArray, apszFirst);
+	}
+
+	//
+	// 4. Add one NULL-terminated char* vector per row
+	//
+	gint i;
+	for(i=0 ; i<nNumLines ; i++) {
+		if(apszLines[i][0] == '\0') {
+			g_debug("skipping blank line");
+			continue;
+		}
+		if(apszLines[i][0] == '#') {
+			g_debug("skipping comment line");
+			continue;
+		}
+
+		gchar** apszWords = g_strsplit(apszLines[i], "\t", -1);
+		if(g_strv_length(apszWords) < nMinVectorLength) {
+			g_error("line %d contains fewer than %d words", i+1, nMinVectorLength);
+		}
+		g_array_append_val(pNewArray, apszWords);
+	}
+
+	//
+	// 5. Cleanup and return.
+	//
+	g_strfreev(apszLines);
+	g_free(pszFileContent);
+
+	*ppReturnArray = pNewArray;
+	return TRUE;
+}
+
+gboolean util_find_string_in_string_vector(const gchar* pszSearch, const gchar** apszVector, gint* pnReturnIndex)
+{
+	g_assert(pszSearch != NULL);
+	g_assert(apszVector != NULL);
+
+	gint i=0;
+	while(apszVector[i] != NULL) {
+		if(g_ascii_strcasecmp(pszSearch, apszVector[i]) == 0) {
+			if(pnReturnIndex != NULL) {
+				g_assert(*pnReturnIndex == -1);		// require either NULL or pointer to (gint)-1
+				*pnReturnIndex = i;
+			}
+			return TRUE;
+		}
+		i++;
+	}
+	return FALSE;
+}

Index: util.h
===================================================================
RCS file: /cvs/cairo/roadster/src/util.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- util.h	20 Oct 2005 06:56:22 -0000	1.19
+++ util.h	26 Oct 2005 05:04:53 -0000	1.20
@@ -68,6 +68,9 @@
 gboolean util_gtk_range_instant_set_on_value_changing_callback(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data);
 const gchar* util_gtk_menu_item_get_label_text(GtkMenuItem* pMenuItem);
 
+gboolean util_load_array_of_string_vectors(const gchar* pszPath, GArray** ppReturnArray, gint nMinNamesPerRow);
+gboolean util_find_string_in_string_vector(const gchar* pszSearch, const gchar** apszVector, gint* pnReturnIndex);
+
 // if glib < 2.6
 #if(!GLIB_CHECK_VERSION(2,6,0))
 gint g_strv_length(const gchar** a);



More information about the cairo-commit mailing list