[Tux4kids-commits] r1657 - tuxtype/trunk/src

David Bruce dbruce-guest at alioth.debian.org
Tue Nov 17 19:12:57 UTC 2009


Author: dbruce-guest
Date: 2009-11-17 19:12:57 +0000 (Tue, 17 Nov 2009)
New Revision: 1657

Modified:
   tuxtype/trunk/src/main.c
   tuxtype/trunk/src/setup.c
   tuxtype/trunk/src/titlescreen.c
Log:
cleanup and hopefully bugfix of list-scanning code

Modified: tuxtype/trunk/src/main.c
===================================================================
--- tuxtype/trunk/src/main.c	2009-11-17 19:12:51 UTC (rev 1656)
+++ tuxtype/trunk/src/main.c	2009-11-17 19:12:57 UTC (rev 1657)
@@ -34,13 +34,16 @@
   Uint32 lib_flags = 0;
   int i;
 
-
-
   srand(time(NULL));
 
+  //FIXME we should inspect return values and bail if these functions fail:
   Opts_Initialize(); // First, initialize settings with hard-coded defaults 
   // This sets settings.default_data_path to the default theme file path:
-  SetupPaths(NULL);
+  if(!SetupPaths(NULL))
+  {  
+    fprintf(stderr, "Error - SetupPaths() failed, exiting program.\n");
+    return 0;
+  }  
   LoadSettings();    // Second, read saved any saved settings
 
   // Third, check command line args as these should override saved settings

Modified: tuxtype/trunk/src/setup.c
===================================================================
--- tuxtype/trunk/src/setup.c	2009-11-17 19:12:51 UTC (rev 1656)
+++ tuxtype/trunk/src/setup.c	2009-11-17 19:12:57 UTC (rev 1657)
@@ -389,10 +389,10 @@
 
 int SetupPaths(const char* theme_dir)
 {
-  //DEBUGCODE
-  //{
+  DEBUGCODE
+  {
     fprintf(stderr, "Entering SetupPaths()\n");
-  //}
+  }
 
   settings.use_english = 1; // default is to use English if we cannot find theme
   char fn[FNLEN];           // used later when setting settings.user_settings_path
@@ -436,7 +436,6 @@
       /* an explicit statement to use the default(                  */
       strncpy(settings.theme_font_name, DEFAULT_FONT_NAME, FNLEN);
       
-
       /* Load fontname or any other theme-specific settings: */
       sprintf(theme_settings_path, "%s/settings.txt", full_theme_path);
 
@@ -465,12 +464,6 @@
   }
 
 
-  DEBUGCODE
-  {
-    fprintf(stderr, "Leaving SetupPaths():\n");
-    fprintf(stderr, "default_data_path: '%s'\n", settings.default_data_path);
-    fprintf(stderr, "theme_data_path: '%s'\n\n", settings.theme_data_path);
-  }
 
   /* Now check for VAR_PREFIX (for modifiable data shared by all users, */ 
   /* such as custom word lists, high scores, etc:                       */
@@ -490,17 +483,18 @@
   /* Now check for CONF_PREFIX (for program wide settings that apply to all users). */ 
   /* This would typically be /etc/tuxtype if tuxtype is installed by a distro pkg,  */
   /* or /usr/local/etc/tuxtype if the package is built locally                      */
-  if (CheckFile(VAR_PREFIX))
+  if (CheckFile(CONF_PREFIX))
   {
-    strncpy(settings.var_data_path, VAR_PREFIX, FNLEN - 1);
-    DEBUGCODE {fprintf(stderr, "path '%s' found, copy to settings.var_data_path\n", VAR_PREFIX);}
+    strncpy(settings.global_settings_path, CONF_PREFIX, FNLEN - 1);
+    DEBUGCODE {fprintf(stderr, "path '%s' found, copy to settings.global_settings_path\n", CONF_PREFIX);}
   }
   else
   {
-    fprintf(stderr, "Error - VAR_PREFIX = '%s' not found!\n", VAR_PREFIX);
+    fprintf(stderr, "Error - CONF_PREFIX = '%s' not found!\n", CONF_PREFIX);
     return 0;
   }
 
+
   /* Determine the user data path (for user specific settings)  this would normally be     */
   /* /home/user/.tuxtype for POSIX systems or Documents and Settings/user/Application Data */
   /* for windows systems                                                                   */
@@ -530,12 +524,14 @@
     }
     else
     {
+      //NOTE this shouldn't happen, right? We should always be able to make a directory
+      //in our own home folder - DSB
       fprintf(stderr, "Error - Could not create: '%s', falling back to the global settings.\n", fn);
   #ifdef WIN32
       strncpy(settings.user_settings_path, "userdata", FNLEN - 1);
   #else
       // FIXME: in this fallback case if we are forced to use global settings, and /etc/tuxtype
-      // contains those settings we can't write to it, so use /tmp  -but then we have nothing to
+      // contains those settings we can't write to it, so use /tmp - but then we have nothing to
       // read from, copy the global settings to temp first?  Or just forget about saving the settings
       // when we exit? - MDT
       strncpy(settings.user_settings_path, "/tmp", FNLEN - 1);
@@ -543,6 +539,16 @@
     }
   }
 
+DEBUGCODE
+  {
+    fprintf(stderr, "Leaving SetupPaths():\n");
+    fprintf(stderr, "default_data_path: '%s'\n", settings.default_data_path);
+    fprintf(stderr, "theme_data_path: '%s'\n\n", settings.theme_data_path);
+    fprintf(stderr, "var_data_path: '%s'\n\n", settings.var_data_path);
+    fprintf(stderr, "user_settings_path: '%s'\n\n", settings.user_settings_path);
+    fprintf(stderr, "global_settings_path: '%s'\n\n", settings.global_settings_path);
+  }
+
   return 1;	
 }
 

Modified: tuxtype/trunk/src/titlescreen.c
===================================================================
--- tuxtype/trunk/src/titlescreen.c	2009-11-17 19:12:51 UTC (rev 1656)
+++ tuxtype/trunk/src/titlescreen.c	2009-11-17 19:12:57 UTC (rev 1657)
@@ -1213,15 +1213,8 @@
 
   /* find the directory to load wordlists from */
 
-  /* First Check the user's personal settings */
-  sprintf(wordPath, "%s/words", settings.user_settings_path);
-  if (CheckFile(wordPath))
+  /* First we add the bundled word lists in either NLS or default theme: */
   {
-    DEBUGCODE { fprintf(stderr, "User specific wordlist path found: %s\n", wordPath); }
-  }
-  /* If nothing found, check under theme directory, if theme selected: */
-  else
-  {
     if (!settings.use_english)  /* Using theme: */
     {
       sprintf(wordPath,"%s/words", settings.theme_data_path);
@@ -1245,14 +1238,13 @@
   /* If we get to here, we know there is at least a wordlist directory */
   /* but not necessarily any valid files.                              */
 
-  DEBUGCODE { fprintf(stderr, "wordPath is: %s\n", wordPath); }
+  DEBUGCODE { fprintf(stderr, "bundled wordPath is: %s\n", wordPath); }
 
 
   /* FIXME looks like a place for scandir() - or our own w32_scandir() */
+  /* FIXME we also have this block of code repeated three times - DSB  */
   /* create a list of all the .txt files */
-
   wordsDir = opendir( wordPath );	
-
   do
   {
     wordsFile = readdir(wordsDir);
@@ -1287,85 +1279,124 @@
 
     fclose(tempFile);
   } while (1); /* Loop continues until break occurs */
+  closedir(wordsDir);	
 
-  closedir(wordsDir);	
+  
+  
+  /* Adding global custom wordlists ------------------------------------ */
  
+  sprintf(wordPath,"%s/words", settings.var_data_path);
+  if (!CheckFile(wordPath))
+  {
+    fprintf(stderr, "chooseWordList() -  path \"%s\" not found\n", settings.var_data_path);
+  }
+  else
+  {
+    /* If we get to here, we know there is at least a wordlist directory */
+    /* but not necessarily any valid files.                              */
 
-/* Adding custom wordlists */
- 
-    sprintf(wordPath,"%s/words", settings.var_data_path);
-    if (!CheckFile(wordPath))
+    DEBUGCODE { fprintf(stderr, "global custom wordPath is: %s\n", wordPath); }
+
+    /* FIXME looks like a place for scandir() - or our own w32_scandir() */
+    /* create a list of all the .txt files */
+    wordsDir = opendir( wordPath );	
+    do
     {
-      fprintf(stderr, "chooseWordList() - data path contains no wordlist dir \n");
-      return 0;
-    }
- 
+      wordsFile = readdir(wordsDir);
+      if (!wordsFile)
+        break; /* Loop continues until break occurs */
 
-  /* If we get to here, we know there is at least a wordlist directory */
-  /* but not necessarily any valid files.                              */
+      DEBUGCODE { fprintf(stderr, "wordsFile name is: %s\n", wordsFile->d_name); }
 
-  DEBUGCODE { fprintf(stderr, "wordPath is: %s\n", wordPath); }
+      /* must have at least .txt at the end */
+      if (strlen(wordsFile->d_name) < 5)
+        continue;
 
+      if (strcmp(&wordsFile->d_name[strlen(wordsFile->d_name) -4 ],".txt"))
+        continue;
 
-  /* FIXME looks like a place for scandir() - or our own w32_scandir() */
-  /* create a list of all the .txt files */
+      sprintf(wordlistFile[lists], "%s/%s", wordPath, wordsFile->d_name);
 
-  wordsDir = opendir( wordPath );	
+      /* load the name for the wordlist from the file ... (1st line) */
+      tempFile = fopen( wordlistFile[lists], "r" );
+      if (!tempFile)
+        continue;
 
-  do
-  {
-    wordsFile = readdir(wordsDir);
-    if (!wordsFile)
-      break; /* Loop continues until break occurs */
+      result = fscanf(tempFile, "%[^\n]\n", wordlistName[lists]);
+      if (result == EOF)
+        continue;
 
-    DEBUGCODE { fprintf(stderr, "wordsFile name is: %s\n", wordsFile->d_name); }
+      /* check to see if it has a \r at the end of it (dos format!) */
+      if (wordlistName[lists][strlen(wordlistName[lists]) - 1] == '\r')
+        wordlistName[lists][strlen(wordlistName[lists]) - 1] = '\0';
 
-    /* must have at least .txt at the end */
-    if (strlen(wordsFile->d_name) < 5)
-      continue;
+      lists++;
 
-    if (strcmp(&wordsFile->d_name[strlen(wordsFile->d_name) -4 ],".txt"))
-      continue;
+      fclose(tempFile);
+    } while (1); /* Loop continues until break occurs */
+    closedir(wordsDir);
+  }
+  
+  /* Now add any lists in the user's personal settings path: ------------ */
 
-    sprintf(wordlistFile[lists], "%s/%s", wordPath, wordsFile->d_name);
+  sprintf(wordPath,"%s/words", settings.user_settings_path);
+  if (!CheckFile(wordPath))
+  {
+    fprintf(stderr, "chooseWordList() -  path \"%s\" not found\n", settings.user_settings_path);
+  }
+  else
+  {
+    /* If we get to here, we know there is at least a wordlist directory */
+    /* but not necessarily any valid files.                              */
 
-    /* load the name for the wordlist from the file ... (1st line) */
-    tempFile = fopen( wordlistFile[lists], "r" );
-    if (!tempFile)
-      continue;
+    DEBUGCODE { fprintf(stderr, "user-specific wordPath is: %s\n", wordPath); }
 
-    result = fscanf(tempFile, "%[^\n]\n", wordlistName[lists]);
-    if (result == EOF)
-      continue;
+    /* FIXME looks like a place for scandir() - or our own w32_scandir() */
+    /* create a list of all the .txt files */
+    wordsDir = opendir( wordPath );	
+    do
+    {
+      wordsFile = readdir(wordsDir);
+      if (!wordsFile)
+        break; /* Loop continues until break occurs */
 
-    /* check to see if it has a \r at the end of it (dos format!) */
-    if (wordlistName[lists][strlen(wordlistName[lists]) - 1] == '\r')
-      wordlistName[lists][strlen(wordlistName[lists]) - 1] = '\0';
+      DEBUGCODE { fprintf(stderr, "wordsFile name is: %s\n", wordsFile->d_name); }
 
-    lists++;
+      /* must have at least .txt at the end */
+      if (strlen(wordsFile->d_name) < 5)
+        continue;
 
-    fclose(tempFile);
-  } while (1); /* Loop continues until break occurs */
+      if (strcmp(&wordsFile->d_name[strlen(wordsFile->d_name) -4 ],".txt"))
+        continue;
 
-  closedir(wordsDir);
+      sprintf(wordlistFile[lists], "%s/%s", wordPath, wordsFile->d_name);
 
+      /* load the name for the wordlist from the file ... (1st line) */
+      tempFile = fopen( wordlistFile[lists], "r" );
+      if (!tempFile)
+        continue;
 
+      result = fscanf(tempFile, "%[^\n]\n", wordlistName[lists]);
+      if (result == EOF)
+        continue;
 
+      /* check to see if it has a \r at the end of it (dos format!) */
+      if (wordlistName[lists][strlen(wordlistName[lists]) - 1] == '\r')
+        wordlistName[lists][strlen(wordlistName[lists]) - 1] = '\0';
 
- DEBUGCODE { fprintf(stderr, "Found %d .txt file(s) in words dir\n", lists); }
+      lists++;
 
+      fclose(tempFile);
+    } while (1); /* Loop continues until break occurs */
+    closedir(wordsDir);
+  }
 
+  DEBUGCODE { fprintf(stderr, "Found %d .txt file(s) in words dir\n", lists); }
 
-  /* let the user pick the list */
+  
+  /* Done scanning for word lists, now display them for user selection: */
 
   /* Render SDL_Surfaces for list entries: */
-//  titles[0] = BlackOutline( _("Alphabet"), font, &white );
-//  select[0] = BlackOutline( _("Alphabet"), font, &yellow);
-
-  /* NOTE - no longer hard-coding titles[0] to be alphabet - themes  */
-  /* should include a regular word list file called "alphabet.txt"   */
-  /* Should sort the list and always put the alphabet file first, if */
-  /* present.                                                        */
   for (i = 0; i < lists; i++)
   {
     titles[i] = BlackOutline( wordlistName[i], DEFAULT_MENU_FONT_SIZE, &white );




More information about the Tux4kids-commits mailing list