@@ -22,8 +22,8 @@ static const char *ENV_VARS[] = {
2222 " APPDATA" , " HOME" , " TMP" , " TEMP" , " TMPDIR"
2323};
2424
25- #if !defined(PATH_MAX )
26- #define PATH_MAX 256
25+ #if !defined(FILENAME_MAX )
26+ #define FILENAME_MAX 256
2727#endif
2828
2929#define DEFAULT_WIDTH 640
@@ -38,7 +38,7 @@ static const char *ENV_VARS[] = {
3838
3939FILE *openConfig (const char *flags, bool debug) {
4040 FILE *result = NULL ;
41- char path[PATH_MAX ];
41+ char path[FILENAME_MAX ];
4242 int vars_len = sizeof (ENV_VARS) / sizeof (ENV_VARS[0 ]);
4343
4444 path[0 ] = 0 ;
@@ -93,10 +93,27 @@ int nextHex(FILE *fp, int def) {
9393 return result;
9494}
9595
96+ //
97+ // sets the next string in the file as the current working directory
98+ //
99+ void restorePath (FILE *fp) {
100+ int pos = ftell (fp);
101+ int len = 0 ;
102+ for (int c = fgetc (fp); c != EOF && c != ' ,' && c != ' \n ' ; c = fgetc (fp)) {
103+ len++;
104+ }
105+ fseek (fp, pos, SEEK_SET);
106+ if (len > 0 ) {
107+ String path;
108+ path.append (fp, len);
109+ chdir (path.c_str ());
110+ }
111+ }
112+
96113//
97114// restore window position
98115//
99- void restoreSettings (SDL_Rect &rect, int &fontScale, bool debug) {
116+ void restoreSettings (SDL_Rect &rect, int &fontScale, bool debug, bool restoreDir ) {
100117 FILE *fp = openConfig (" r" , debug);
101118 if (fp) {
102119 rect.x = nextInteger (fp, SDL_WINDOWPOS_UNDEFINED);
@@ -110,6 +127,9 @@ void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug) {
110127 for (int i = 0 ; i < THEME_COLOURS; i++) {
111128 g_user_theme[i] = nextHex (fp, g_user_theme[i]);
112129 }
130+ if (restoreDir) {
131+ restorePath (fp);
132+ }
113133 fclose (fp);
114134 } else {
115135 rect.x = SDL_WINDOWPOS_UNDEFINED;
@@ -138,7 +158,20 @@ void saveSettings(SDL_Window *window, int fontScale, bool debug) {
138158 for (int i = 0 ; i < THEME_COLOURS; i++) {
139159 fprintf (fp, (i + 1 < THEME_COLOURS ? " %06x," : " %06x" ), g_user_theme[i]);
140160 }
141- fprintf (fp, " \n " );
161+
162+ // save the current working directory
163+ char path[FILENAME_MAX + 1 ];
164+ getcwd (path, FILENAME_MAX);
165+ if (path[1 ] == ' :' && path[2 ] == ' \\ ' ) {
166+ for (int i = 2 ; path[i] != ' \0 ' ; i++) {
167+ if (path[i] == ' \\ ' ) {
168+ path[i] = ' /' ;
169+ }
170+ }
171+ fprintf (fp, " \n %s\n " , path);
172+ } else {
173+ fprintf (fp, " \n %s\n " , path);
174+ }
142175 fclose (fp);
143176 }
144177}
0 commit comments