Skip to content

Commit aa44aa9

Browse files
committed
Merge pull request #33 from chrisws/0_11_20
SDL: restore recent path
2 parents 1bf3fe8 + 2895693 commit aa44aa9

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/platform/sdl/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ int main(int argc, char* argv[]) {
263263
char *fontFamily = NULL;
264264
char *runFile = NULL;
265265
bool debug = false;
266+
bool restoreDir = true;
266267
int fontScale;
267268
int ide_option = -1;
268269
SDL_Rect rect;
@@ -280,7 +281,9 @@ int main(int argc, char* argv[]) {
280281
&& ((strcasecmp(s + len - 4, ".bas") == 0 && access(s, 0) == 0)
281282
|| (strstr(s, "://") != NULL))) {
282283
runFile = strdup(s);
283-
} else if (chdir(s) != 0) {
284+
} else if (chdir(s) == 0) {
285+
restoreDir = false;
286+
} else {
284287
strcpy(opt_command, s);
285288
}
286289
}
@@ -344,7 +347,7 @@ int main(int argc, char* argv[]) {
344347
}
345348
}
346349

347-
restoreSettings(rect, fontScale, debug);
350+
restoreSettings(rect, fontScale, debug, restoreDir);
348351
if (ide_option != -1) {
349352
opt_ide = ide_option;
350353
}

src/platform/sdl/settings.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3939
FILE *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
}

src/platform/sdl/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <SDL.h>
1313

14-
void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug);
14+
void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug, bool restoreDir);
1515
void saveSettings(SDL_Window *window, int fontScale, bool debug);
1616

1717
#endif

0 commit comments

Comments
 (0)