[Tux4kids-commits] r218 - tuxmath/trunk/src
tholy-guest at alioth.debian.org
tholy-guest at alioth.debian.org
Thu Aug 30 04:17:54 UTC 2007
Author: tholy-guest
Date: 2007-08-30 04:17:54 +0000 (Thu, 30 Aug 2007)
New Revision: 218
Modified:
tuxmath/trunk/src/Makefile.in
tuxmath/trunk/src/fileops.c
tuxmath/trunk/src/fileops.h
tuxmath/trunk/src/setup.c
tuxmath/trunk/src/titlescreen.c
tuxmath/trunk/src/titlescreen.h
tuxmath/trunk/src/tuxmath.h
Log:
Cleanup: move lessons directory parsing to fileops.c, and call it only once upon setup (so it's done only once).
Modified: tuxmath/trunk/src/Makefile.in
===================================================================
--- tuxmath/trunk/src/Makefile.in 2007-08-29 02:17:14 UTC (rev 217)
+++ tuxmath/trunk/src/Makefile.in 2007-08-30 04:17:54 UTC (rev 218)
@@ -1,8 +1,8 @@
-# Makefile.in generated by automake 1.10 from Makefile.am.
+# Makefile.in generated by automake 1.9.6 from Makefile.am.
# @configure_input@
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
-# 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+# 2003, 2004, 2005 Free Software Foundation, Inc.
# This Makefile.in is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
@@ -15,11 +15,15 @@
@SET_MAKE@
+srcdir = @srcdir@
+top_srcdir = @top_srcdir@
VPATH = @srcdir@
pkgdatadir = $(datadir)/@PACKAGE@
pkglibdir = $(libdir)/@PACKAGE@
pkgincludedir = $(includedir)/@PACKAGE@
+top_builddir = ..
am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
+INSTALL = @INSTALL@
install_sh_DATA = $(install_sh) -c -m 644
install_sh_PROGRAM = $(install_sh) -c
install_sh_SCRIPT = $(install_sh) -c
@@ -66,7 +70,7 @@
fileops.$(OBJEXT)
tuxmath_OBJECTS = $(am_tuxmath_OBJECTS)
tuxmath_LDADD = $(LDADD)
-DEFAULT_INCLUDES = -I. -I$(top_builddir)@am__isrc@
+DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)
depcomp = $(SHELL) $(top_srcdir)/depcomp
am__depfiles_maybe = depfiles
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
@@ -77,24 +81,25 @@
DIST_SOURCES = $(TuxMath_SOURCES) $(tuxmath_SOURCES)
RECURSIVE_TARGETS = all-recursive check-recursive dvi-recursive \
html-recursive info-recursive install-data-recursive \
- install-dvi-recursive install-exec-recursive \
- install-html-recursive install-info-recursive \
- install-pdf-recursive install-ps-recursive install-recursive \
- installcheck-recursive installdirs-recursive pdf-recursive \
- ps-recursive uninstall-recursive
+ install-exec-recursive install-info-recursive \
+ install-recursive installcheck-recursive installdirs-recursive \
+ pdf-recursive ps-recursive uninstall-info-recursive \
+ uninstall-recursive
HEADERS = $(noinst_HEADERS)
-RECURSIVE_CLEAN_TARGETS = mostlyclean-recursive clean-recursive \
- distclean-recursive maintainer-clean-recursive
ETAGS = etags
CTAGS = ctags
DIST_SUBDIRS = $(SUBDIRS)
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
ACLOCAL = @ACLOCAL@
+AMDEP_FALSE = @AMDEP_FALSE@
+AMDEP_TRUE = @AMDEP_TRUE@
AMTAR = @AMTAR@
AUTOCONF = @AUTOCONF@
AUTOHEADER = @AUTOHEADER@
AUTOMAKE = @AUTOMAKE@
AWK = @AWK@
+BUILD_MINGW32_FALSE = @BUILD_MINGW32_FALSE@
+BUILD_MINGW32_TRUE = @BUILD_MINGW32_TRUE@
CC = @CC@
CCDEPMODE = @CCDEPMODE@
CFLAGS = @CFLAGS@
@@ -109,7 +114,6 @@
EGREP = @EGREP@
EXEEXT = @EXEEXT@
GREP = @GREP@
-INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
@@ -119,13 +123,18 @@
LIBS = @LIBS@
LTLIBOBJS = @LTLIBOBJS@
MAINT = @MAINT@
+MAINTAINER_MODE_FALSE = @MAINTAINER_MODE_FALSE@
+MAINTAINER_MODE_TRUE = @MAINTAINER_MODE_TRUE@
MAKEINFO = @MAKEINFO@
-MKDIR_P = @MKDIR_P@
NAME_VERSION = @NAME_VERSION@
NSIS = @NSIS@
+NSI_BUILD_FALSE = @NSI_BUILD_FALSE@
+NSI_BUILD_TRUE = @NSI_BUILD_TRUE@
NSI_DLL_DIR = @NSI_DLL_DIR@
NSI_INSTALL_DIR = @NSI_INSTALL_DIR@
NSI_TUXMATH_CONF_DIR = @NSI_TUXMATH_CONF_DIR@
+NSI_USE_TUXMATH_CONF_FALSE = @NSI_USE_TUXMATH_CONF_FALSE@
+NSI_USE_TUXMATH_CONF_TRUE = @NSI_USE_TUXMATH_CONF_TRUE@
OBJEXT = @OBJEXT@
PACKAGE = @PACKAGE@
PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
@@ -146,11 +155,9 @@
# SUBDIRS = po
WINDRES = @WINDRES@
-abs_builddir = @abs_builddir@
-abs_srcdir = @abs_srcdir@
-abs_top_builddir = @abs_top_builddir@
-abs_top_srcdir = @abs_top_srcdir@
ac_ct_CC = @ac_ct_CC@
+am__fastdepCC_FALSE = @am__fastdepCC_FALSE@
+am__fastdepCC_TRUE = @am__fastdepCC_TRUE@
am__include = @am__include@
am__leading_dot = @am__leading_dot@
am__quote = @am__quote@
@@ -162,7 +169,6 @@
build_cpu = @build_cpu@
build_os = @build_os@
build_vendor = @build_vendor@
-builddir = @builddir@
datadir = @datadir@
datarootdir = @datarootdir@
docdir = @docdir@
@@ -190,15 +196,12 @@
psdir = @psdir@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
-srcdir = @srcdir@
sysconfdir = @sysconfdir@
target = @target@
target_alias = @target_alias@
target_cpu = @target_cpu@
target_os = @target_os@
target_vendor = @target_vendor@
-top_builddir = @top_builddir@
-top_srcdir = @top_srcdir@
SUBDIRS = po
@BUILD_MINGW32_FALSE at TUXMATHRC =
TuxMath_SOURCES = tuxmath.c setup.c titlescreen.c game.c \
@@ -263,7 +266,7 @@
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
install-binPROGRAMS: $(bin_PROGRAMS)
@$(NORMAL_INSTALL)
- test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)"
+ test -z "$(bindir)" || $(mkdir_p) "$(DESTDIR)$(bindir)"
@list='$(bin_PROGRAMS)'; for p in $$list; do \
p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \
if test -f $$p \
@@ -286,10 +289,10 @@
-test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS)
TuxMath$(EXEEXT): $(TuxMath_OBJECTS) $(TuxMath_DEPENDENCIES)
@rm -f TuxMath$(EXEEXT)
- $(LINK) $(TuxMath_OBJECTS) $(TuxMath_LDADD) $(LIBS)
+ $(LINK) $(TuxMath_LDFLAGS) $(TuxMath_OBJECTS) $(TuxMath_LDADD) $(LIBS)
tuxmath$(EXEEXT): $(tuxmath_OBJECTS) $(tuxmath_DEPENDENCIES)
@rm -f tuxmath$(EXEEXT)
- $(LINK) $(tuxmath_OBJECTS) $(tuxmath_LDADD) $(LIBS)
+ $(LINK) $(tuxmath_LDFLAGS) $(tuxmath_OBJECTS) $(tuxmath_LDADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
@@ -314,18 +317,19 @@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/tuxmath.Po at am__quote@
.c.o:
- at am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
- at am__fastdepCC_TRUE@ mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+ at am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \
+ at am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi
@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(COMPILE) -c $<
.c.obj:
- at am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
- at am__fastdepCC_TRUE@ mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+ at am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ `$(CYGPATH_W) '$<'`; \
+ at am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi
@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'`
+uninstall-info-am:
# This directory's subdirectories are mostly independent; you can cd
# into them and run `make' without going through this Makefile.
@@ -358,7 +362,8 @@
$(MAKE) $(AM_MAKEFLAGS) "$$target-am" || exit 1; \
fi; test -z "$$fail"
-$(RECURSIVE_CLEAN_TARGETS):
+mostlyclean-recursive clean-recursive distclean-recursive \
+maintainer-clean-recursive:
@failcom='exit 1'; \
for f in x $$MAKEFLAGS; do \
case $$f in \
@@ -459,21 +464,22 @@
-rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
distdir: $(DISTFILES)
- @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
- topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
- list='$(DISTFILES)'; \
- dist_files=`for file in $$list; do echo $$file; done | \
- sed -e "s|^$$srcdirstrip/||;t" \
- -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \
- case $$dist_files in \
- */*) $(MKDIR_P) `echo "$$dist_files" | \
- sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \
- sort -u` ;; \
- esac; \
- for file in $$dist_files; do \
+ @srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \
+ topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \
+ list='$(DISTFILES)'; for file in $$list; do \
+ case $$file in \
+ $(srcdir)/*) file=`echo "$$file" | sed "s|^$$srcdirstrip/||"`;; \
+ $(top_srcdir)/*) file=`echo "$$file" | sed "s|^$$topsrcdirstrip/|$(top_builddir)/|"`;; \
+ esac; \
if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
+ dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \
+ if test "$$dir" != "$$file" && test "$$dir" != "."; then \
+ dir="/$$dir"; \
+ $(mkdir_p) "$(distdir)$$dir"; \
+ else \
+ dir=''; \
+ fi; \
if test -d $$d/$$file; then \
- dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \
fi; \
@@ -487,7 +493,7 @@
list='$(DIST_SUBDIRS)'; for subdir in $$list; do \
if test "$$subdir" = .; then :; else \
test -d "$(distdir)/$$subdir" \
- || $(MKDIR_P) "$(distdir)/$$subdir" \
+ || $(mkdir_p) "$(distdir)/$$subdir" \
|| exit 1; \
distdir=`$(am__cd) $(distdir) && pwd`; \
top_distdir=`$(am__cd) $(top_distdir) && pwd`; \
@@ -495,8 +501,6 @@
$(MAKE) $(AM_MAKEFLAGS) \
top_distdir="$$top_distdir" \
distdir="$$distdir/$$subdir" \
- am__remove_distdir=: \
- am__skip_length_check=: \
distdir) \
|| exit 1; \
fi; \
@@ -507,7 +511,7 @@
installdirs: installdirs-recursive
installdirs-am:
for dir in "$(DESTDIR)$(bindir)"; do \
- test -z "$$dir" || $(MKDIR_P) "$$dir"; \
+ test -z "$$dir" || $(mkdir_p) "$$dir"; \
done
install: install-recursive
install-exec: install-exec-recursive
@@ -555,20 +559,12 @@
install-data-am:
-install-dvi: install-dvi-recursive
-
install-exec-am: install-binPROGRAMS
-install-html: install-html-recursive
-
install-info: install-info-recursive
install-man:
-install-pdf: install-pdf-recursive
-
-install-ps: install-ps-recursive
-
installcheck-am:
maintainer-clean: maintainer-clean-recursive
@@ -588,25 +584,23 @@
ps-am:
-uninstall-am: uninstall-binPROGRAMS
+uninstall-am: uninstall-binPROGRAMS uninstall-info-am
-.MAKE: $(RECURSIVE_CLEAN_TARGETS) $(RECURSIVE_TARGETS) install-am \
- install-strip
+uninstall-info: uninstall-info-recursive
-.PHONY: $(RECURSIVE_CLEAN_TARGETS) $(RECURSIVE_TARGETS) CTAGS GTAGS \
- all all-am check check-am clean clean-binPROGRAMS \
- clean-generic ctags ctags-recursive distclean \
- distclean-compile distclean-generic distclean-tags distdir dvi \
- dvi-am html html-am info info-am install install-am \
- install-binPROGRAMS install-data install-data-am install-dvi \
- install-dvi-am install-exec install-exec-am install-html \
- install-html-am install-info install-info-am install-man \
- install-pdf install-pdf-am install-ps install-ps-am \
- install-strip installcheck installcheck-am installdirs \
- installdirs-am maintainer-clean maintainer-clean-generic \
- mostlyclean mostlyclean-compile mostlyclean-generic pdf pdf-am \
- ps ps-am tags tags-recursive uninstall uninstall-am \
- uninstall-binPROGRAMS
+.PHONY: $(RECURSIVE_TARGETS) CTAGS GTAGS all all-am check check-am \
+ clean clean-binPROGRAMS clean-generic clean-recursive ctags \
+ ctags-recursive distclean distclean-compile distclean-generic \
+ distclean-recursive distclean-tags distdir dvi dvi-am html \
+ html-am info info-am install install-am install-binPROGRAMS \
+ install-data install-data-am install-exec install-exec-am \
+ install-info install-info-am install-man install-strip \
+ installcheck installcheck-am installdirs installdirs-am \
+ maintainer-clean maintainer-clean-generic \
+ maintainer-clean-recursive mostlyclean mostlyclean-compile \
+ mostlyclean-generic mostlyclean-recursive pdf pdf-am ps ps-am \
+ tags tags-recursive uninstall uninstall-am \
+ uninstall-binPROGRAMS uninstall-info-am
# How to make an RC file
Modified: tuxmath/trunk/src/fileops.c
===================================================================
--- tuxmath/trunk/src/fileops.c 2007-08-29 02:17:14 UTC (rev 217)
+++ tuxmath/trunk/src/fileops.c 2007-08-30 04:17:54 UTC (rev 218)
@@ -478,6 +478,108 @@
return 0;
}
+int is_lesson_file(const struct dirent *lfdirent)
+{
+ return (0 == strncasecmp(&(lfdirent->d_name), "lesson", 6));
+ /* FIXME Should somehow test each file to see if it is a tuxmath config file */
+}
+
+int parse_lesson_file_directory(void)
+{
+ unsigned char lesson_path[PATH_MAX]; //Path to lesson directory
+ char* fgets_return_val;
+ unsigned char name_buf[NAME_BUF_SIZE];
+
+ struct dirent **lesson_list_dirents;
+ FILE* tempFile = NULL;
+
+ int i = 0;
+ int lessonIterator = 0;
+ int length = 0;
+ int lessons = 0;
+
+ /* find the directory containing the lesson files: */
+ sprintf(lesson_path, "%s/missions/lessons", DATA_PREFIX);
+
+#ifdef TUXMATH_DEBUG
+ fprintf(stderr, "lesson_path is: %s\n", lesson_path);
+#endif
+
+ num_lessons = scandir(lesson_path,&lesson_list_dirents,is_lesson_file,alphasort);
+ if (num_lessons < 0) {
+ perror("scanning lesson directory");
+ num_lessons = 0;
+ return 0;
+ }
+
+ /* Allocate storage for lessons */
+ lesson_list = (lesson_entry *) malloc(num_lessons * sizeof(lesson_entry));
+ if (lesson_list == NULL) {
+ perror("allocating memory for lesson list");
+ return 0;
+ }
+
+ /* lessonIterator indexes the direntries, lessons indexes the correctly-parsed files. If successful in parsing, lessons gets incremented */
+ for (lessonIterator = 0, lessons = 0; lessonIterator < num_lessons; lessonIterator++) {
+ /* Copy over the filename */
+ sprintf(lesson_list[lessons].filename, "%s/%s", lesson_path, lesson_list_dirents[lessonIterator]->d_name);
+
+#ifdef TUXMATH_DEBUG
+ fprintf(stderr, "Found lesson file %d:\t%s\n", lessons, lesson_list[lessons].filename);
+#endif
+
+ /* load the name for the lesson from the file ... (1st line) */
+ tempFile = fopen(lesson_list[lessons].filename, "r");
+
+ if (tempFile==NULL)
+ {
+ /* By leaving the current iteration without incrementing 'lessons', */
+ /* the bad file name will get clobbered next time through: */
+ continue;
+ }
+
+ fgets_return_val = fgets(name_buf, NAME_BUF_SIZE, tempFile);
+ if (fgets_return_val == NULL) {
+ continue;
+ }
+
+
+ /* check to see if it has a \r at the end of it (dos format!) */
+ length = strlen(name_buf);
+ while (length>0 && (name_buf[length - 1] == '\r' || name_buf[length - 1] == '\n')) {
+ name_buf[length - 1] = '\0';
+ length--;
+ }
+
+ /* Go past leading '#', ';', or whitespace: */
+ /* NOTE getting i to the correct value on exit is the main goal of the loop */
+ for ( i = 0;
+ ((name_buf[i] == '#') ||
+ (name_buf[i] == ';') ||
+ isspace(name_buf[i])) &&
+ (i < NAME_BUF_SIZE);
+ i++ )
+ {
+ length--;
+ }
+ /* Now copy the rest of the first line into the list: */
+ /* Note that "length + 1" is needed so that the final \0 is copied! */
+ memmove(&lesson_list[lessons].display_name, &name_buf[i], length + 1);
+ fclose(tempFile);
+
+ free(lesson_list_dirents[lessonIterator]);
+
+ /* Increment the iterator for correctly-parsed lesson files */
+ lessons++;
+ }
+ /* In case we didn't keep all of them, revise our estimate of how many there are */
+ num_lessons = lessons;
+
+ free(lesson_list_dirents);
+
+ return 1;
+}
+
/* Look for a high score table file in the user's homedir */
/* and if found, pass the FILE* to read_high_scores_() in */
/* highscore.c to actually read in scores. (A "global" */
Modified: tuxmath/trunk/src/fileops.h
===================================================================
--- tuxmath/trunk/src/fileops.h 2007-08-29 02:17:14 UTC (rev 217)
+++ tuxmath/trunk/src/fileops.h 2007-08-30 04:17:54 UTC (rev 218)
@@ -230,6 +230,7 @@
/* These functions used by setup() to read in settings: */
int read_global_config_file(void);
int read_user_config_file(void);
+int parse_lesson_file_directory(void);
int read_named_config_file(const char* filename);
int write_user_config_file(void);
int read_high_scores(void);
Modified: tuxmath/trunk/src/setup.c
===================================================================
--- tuxmath/trunk/src/setup.c 2007-08-29 02:17:14 UTC (rev 217)
+++ tuxmath/trunk/src/setup.c 2007-08-30 04:17:54 UTC (rev 218)
@@ -152,6 +152,11 @@
}
}
+ /* Read the lessons directory to determine which lesson */
+ /* files are available. */
+ if (!parse_lesson_file_directory())
+ fprintf(stderr,"\nCould not parse the lesson file directory.\n");
+
/* Now set up high score tables: */
initialize_scores();
if (!read_high_scores())
@@ -627,6 +632,9 @@
musics[i] = NULL;
}
+ free(lesson_list);
+ lesson_list = NULL;
+
// Close the audio mixer. We have to do this at least as many times
// as it was opened.
n_timesopened = Mix_QuerySpec(&frequency,&format,&channels);
Modified: tuxmath/trunk/src/titlescreen.c
===================================================================
--- tuxmath/trunk/src/titlescreen.c 2007-08-29 02:17:14 UTC (rev 217)
+++ tuxmath/trunk/src/titlescreen.c 2007-08-30 04:17:54 UTC (rev 218)
@@ -50,13 +50,10 @@
unsigned char type;
} blits[MAX_UPDATES];
-typedef struct lesson_entry {
- unsigned char filename[NAME_BUF_SIZE]; //List of lesson file names
- unsigned char display_name[NAME_BUF_SIZE]; //List of lesson names for display
-} lesson_entry;
+// Lessons available for play
+lesson_entry *lesson_list = NULL;
+int num_lessons = 0;
-lesson_entry lesson_list[MAX_LESSONS];
-
// globals from tuxtype's globals.h defined outside of titlescreen.c (in tuxtype):
//int show_tux4kids;
int debugOn; //FIXME switch to TUXMATH_DEBUG
@@ -1663,15 +1660,14 @@
/* choose_config_file() - adapted from chooseWordlist() from tuxtype. */
/* Display a list of tuxmath config files in the missions directory */
/* and allow the player to pick one (AKA "Lessons"). */
-/* FIXME the directory search and list generation belongs in fileops.c */
/* returns 0 if user pressed escape (or if dir not found)
* 1 if config was set correctly
*/
int choose_config_file(void)
{
- SDL_Surface* titles[MAX_LESSONS];
- SDL_Surface* select[MAX_LESSONS];
+ SDL_Surface **titles;
+ SDL_Surface **select;
SDL_Rect leftRect, rightRect;
SDL_Rect titleRects[8]; //8 lessons displayed per page
@@ -1682,117 +1678,27 @@
int stop = 0;
int loc = 0; //The currently selected lesson file
int old_loc = 1;
- int lessons = 0; //Number of lesson files found in dir
int redraw = 0;
int i = 0;
- int length = 0;
int tux_frame = 0;
int click_flag = 1;
- /* FIXME:Move file stuff into fileops.c.*/
- /* Todo?: switch from readdir() to scandir() and use dynamic memory allocation? */
- unsigned char lesson_path[PATH_MAX]; //Path to lesson directory
- char* fgets_return_val;
- unsigned char name_buf[NAME_BUF_SIZE];
- DIR* lesson_dir = NULL;
- struct dirent* lesson_file = NULL;
- FILE* tempFile = NULL;
-
- /* All pointers get explicitly set to NULL until used:*/
- for (i = 0; i < MAX_LESSONS; i++)
- {
- titles[i] = NULL;
- select[i] = NULL;
- }
-
-
#ifdef TUXMATH_DEBUG
fprintf(stderr, "Entering choose_config_file():\n");
#endif
- /* find the directory containing the lesson files: */
- sprintf(lesson_path, "%s/missions/lessons", DATA_PREFIX);
-
-#ifdef TUXMATH_DEBUG
- fprintf(stderr, "lesson_path is: %s\n", lesson_path);
-#endif
-
- /* create a list of all the lesson files */
- lesson_dir = opendir(lesson_path);
-
- do
- {
- /* readdir() returns ptr to next file in dir AND resets ptr to following file: */
- lesson_file = readdir(lesson_dir);
- /* Get out when no more files: */
- if (!lesson_file)
- {
- break;
- }
-
- /* file names must begin with 'lesson' (case-insensitive) */
- if (0 != strncasecmp(&lesson_file->d_name, "lesson", 6))
- {
- continue;
- }
-
- /* FIXME Should somehow test each file to see if it is a tuxmath config file */
- /* Put file name into array of names found in lesson directory */
- sprintf(lesson_list[lessons].filename, "%s/%s", lesson_path, lesson_file->d_name);
-
-#ifdef TUXMATH_DEBUG
- fprintf(stderr, "Found lesson file %d:\t%s\n", lessons, lesson_list[lessons].filename);
-#endif
-
- /* load the name for the lesson from the file ... (1st line) */
- tempFile = fopen(lesson_list[lessons].filename, "r");
-
- if (tempFile==NULL)
- {
- /* By leaving the current iteration without incrementing 'lessons', */
- /* the bad file name will get clobbered next time through: */
- continue;
- }
-
- fgets_return_val = fgets(name_buf, NAME_BUF_SIZE, tempFile);
- if (fgets_return_val == NULL) {
- continue;
- }
-
-
- /* check to see if it has a \r at the end of it (dos format!) */
- length = strlen(name_buf);
- while (length>0 && (name_buf[length - 1] == '\r' || name_buf[length - 1] == '\n')) {
- name_buf[length - 1] = '\0';
- length--;
- }
-
- /* Go past leading '#', ';', or whitespace: */
- /* NOTE getting i to the correct value on exit is the main goal of the loop */
- for ( i = 0;
- ((name_buf[i] == '#') ||
- (name_buf[i] == ';') ||
- isspace(name_buf[i])) &&
- (i < NAME_BUF_SIZE);
- i++ )
- {
- length--;
- }
- /* Now copy the rest of the first line into the list: */
- /* Note that "length + 1" is needed so that the final \0 is copied! */
- memmove(&lesson_list[lessons].display_name, &name_buf[i], length + 1);
- lessons++;
- fclose(tempFile);
- } while (lessons < MAX_LESSONS); // Loop will end when 'break' encountered
-
- closedir(lesson_dir);
-
- /* FIXME The lesson list does not necessarily come out in alphabetical order. */
- /* Sort the list into proper order: */
- qsort(lesson_list, lessons, sizeof(struct lesson_entry), compare_lesson_entries);
/* Display the list of lessons for the player to select: */
- for (i = 0; i < lessons; i++)
+ titles = NULL;
+ select = NULL;
+ titles = (SDL_Surface *) malloc(num_lessons*sizeof(SDL_Surface *));
+ select = (SDL_Surface *) malloc(num_lessons*sizeof(SDL_Surface *));
+ if (titles == NULL || select == NULL) {
+ free(titles);
+ free(select);
+ return 0;
+ }
+ for (i = 0; i < num_lessons; i++)
{
titles[i] = black_outline( _(lesson_list[i].display_name), default_font, &white );
select[i] = black_outline( _(lesson_list[i].display_name), default_font, &yellow);
@@ -1872,7 +1778,7 @@
case SDL_MOUSEMOTION:
{
- for (i = 0; (i < 8) && (loc -(loc % 8) + i < lessons); i++)
+ for (i = 0; (i < 8) && (loc -(loc % 8) + i < num_lessons); i++)
{
if (inRect(lesson_menu_button[i], event.motion.x, event.motion.y))
{
@@ -1903,7 +1809,7 @@
/* "Right" button - go to next page: */
else if (inRect( rightRect, event.motion.x, event.motion.y ))
{
- if (loc - (loc % 8) + 8 < lessons)
+ if (loc - (loc % 8) + 8 < num_lessons)
{
if (Opts_MenuSound() && click_flag)
{
@@ -1923,7 +1829,7 @@
case SDL_MOUSEBUTTONDOWN:
{
/* Lesson buttons - play game with corresponding lesson file: */
- for (i = 0; (i < 8) && (loc - (loc % 8) + i < lessons); i++)
+ for (i = 0; (i < 8) && (loc - (loc % 8) + i < num_lessons); i++)
{
if (inRect(lesson_menu_button[i], event.button.x, event.button.y))
{
@@ -1980,7 +1886,7 @@
/* "Right" button - go to next page: */
if (inRect( rightRect, event.button.x, event.button.y ))
{
- if (loc - (loc % 8) + 8 < lessons)
+ if (loc - (loc % 8) + 8 < num_lessons)
{
loc = loc - (loc % 8) + 8;
if (Opts_MenuSound())
@@ -2062,7 +1968,7 @@
{
if (Opts_MenuSound())
{tuxtype_playsound(sounds[SND_TOCK]);}
- if (loc - (loc % 8) + 8 < lessons)
+ if (loc - (loc % 8) + 8 < num_lessons)
{loc = (loc - (loc % 8) + 8);}
break;
}
@@ -2083,7 +1989,7 @@
{
if (Opts_MenuSound())
{tuxtype_playsound(sounds[SND_TOCK]);}
- if (loc + 1 < lessons)
+ if (loc + 1 < num_lessons)
{loc++;}
break;
}
@@ -2140,7 +2046,7 @@
SDL_BlitSurface(Tux->frame[0], NULL, screen, &Tuxdest);
/* FIXME get rid of "evil" macro ;) */
- for (i = start; i < MIN(start+8,lessons); i++)
+ for (i = start; i < MIN(start+8,num_lessons); i++)
{
titleRects[i % 8].x = 240; //Like main menu
titleRects[i % 8].w = titles[i]->w;
@@ -2174,7 +2080,7 @@
SDL_BlitSurface(images[IMG_LEFT_GRAY], NULL, screen, &leftRect);
}
- if (start + 8 < lessons) // not on last page
+ if (start + 8 < num_lessons) // not on last page
{
SDL_BlitSurface(images[IMG_RIGHT], NULL, screen, &rightRect);
}
@@ -2217,11 +2123,13 @@
} // End !stop while loop
/* --- clear graphics before leaving function --- */
- for (i = 0; i < lessons; i++)
+ for (i = 0; i < num_lessons; i++)
{
SDL_FreeSurface(titles[i]);
SDL_FreeSurface(select[i]);
}
+ free(titles);
+ free(select);
#ifdef TUXMATH_DEBUG
Modified: tuxmath/trunk/src/titlescreen.h
===================================================================
--- tuxmath/trunk/src/titlescreen.h 2007-08-29 02:17:14 UTC (rev 217)
+++ tuxmath/trunk/src/titlescreen.h 2007-08-30 04:17:54 UTC (rev 218)
@@ -117,7 +117,6 @@
#define MAX_LESSONS 100
#define MAX_NUM_WORDS 500
#define MAX_WORD_SIZE 8
-#define NAME_BUF_SIZE 200
//MAX_UPDATES needed for TransWipe() and friends:
#define MAX_UPDATES 180
Modified: tuxmath/trunk/src/tuxmath.h
===================================================================
--- tuxmath/trunk/src/tuxmath.h 2007-08-29 02:17:14 UTC (rev 217)
+++ tuxmath/trunk/src/tuxmath.h 2007-08-30 04:17:54 UTC (rev 218)
@@ -163,6 +163,14 @@
extern range_type ranges[NUM_Q_RANGES];
extern int opers[NUM_OPERS], range_enabled[NUM_Q_RANGES];
+#define NAME_BUF_SIZE 200
+typedef struct lesson_entry {
+ char filename[NAME_BUF_SIZE]; //List of lesson file names
+ char display_name[NAME_BUF_SIZE]; //List of lesson names for display
+} lesson_entry;
+extern lesson_entry *lesson_list;
+extern int num_lessons;
+
/* NOTE: default values for math options are now in mathcards.h */
#endif
More information about the Tux4kids-commits
mailing list