Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/platform/sdl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ int main(int argc, char* argv[]) {
char *fontFamily = NULL;
char *runFile = NULL;
bool debug = false;
bool restoreDir = true;
int fontScale;
int ide_option = -1;
SDL_Rect rect;
Expand All @@ -280,7 +281,9 @@ int main(int argc, char* argv[]) {
&& ((strcasecmp(s + len - 4, ".bas") == 0 && access(s, 0) == 0)
|| (strstr(s, "://") != NULL))) {
runFile = strdup(s);
} else if (chdir(s) != 0) {
} else if (chdir(s) == 0) {
restoreDir = false;
} else {
strcpy(opt_command, s);
}
}
Expand Down Expand Up @@ -344,7 +347,7 @@ int main(int argc, char* argv[]) {
}
}

restoreSettings(rect, fontScale, debug);
restoreSettings(rect, fontScale, debug, restoreDir);
if (ide_option != -1) {
opt_ide = ide_option;
}
Expand Down
43 changes: 38 additions & 5 deletions src/platform/sdl/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ static const char *ENV_VARS[] = {
"APPDATA", "HOME", "TMP", "TEMP", "TMPDIR"
};

#if !defined(PATH_MAX)
#define PATH_MAX 256
#if !defined(FILENAME_MAX)
#define FILENAME_MAX 256
#endif

#define DEFAULT_WIDTH 640
Expand All @@ -38,7 +38,7 @@ static const char *ENV_VARS[] = {

FILE *openConfig(const char *flags, bool debug) {
FILE *result = NULL;
char path[PATH_MAX];
char path[FILENAME_MAX];
int vars_len = sizeof(ENV_VARS) / sizeof(ENV_VARS[0]);

path[0] = 0;
Expand Down Expand Up @@ -93,10 +93,27 @@ int nextHex(FILE *fp, int def) {
return result;
}

//
// sets the next string in the file as the current working directory
//
void restorePath(FILE *fp) {
int pos = ftell(fp);
int len = 0;
for (int c = fgetc(fp); c != EOF && c != ',' && c != '\n'; c = fgetc(fp)) {
len++;
}
fseek(fp, pos, SEEK_SET);
if (len > 0) {
String path;
path.append(fp, len);
chdir(path.c_str());
}
}

//
// restore window position
//
void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug) {
void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug, bool restoreDir) {
FILE *fp = openConfig("r", debug);
if (fp) {
rect.x = nextInteger(fp, SDL_WINDOWPOS_UNDEFINED);
Expand All @@ -110,6 +127,9 @@ void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug) {
for (int i = 0; i < THEME_COLOURS; i++) {
g_user_theme[i] = nextHex(fp, g_user_theme[i]);
}
if (restoreDir) {
restorePath(fp);
}
fclose(fp);
} else {
rect.x = SDL_WINDOWPOS_UNDEFINED;
Expand Down Expand Up @@ -138,7 +158,20 @@ void saveSettings(SDL_Window *window, int fontScale, bool debug) {
for (int i = 0; i < THEME_COLOURS; i++) {
fprintf(fp, (i + 1 < THEME_COLOURS ? "%06x," : "%06x"), g_user_theme[i]);
}
fprintf(fp, "\n");

// save the current working directory
char path[FILENAME_MAX + 1];
getcwd(path, FILENAME_MAX);
if (path[1] == ':' && path[2] == '\\') {
for (int i = 2; path[i] != '\0'; i++) {
if (path[i] == '\\') {
path[i] = '/';
}
}
fprintf(fp, "\n%s\n", path);
} else {
fprintf(fp, "\n%s\n", path);
}
fclose(fp);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/sdl/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <SDL.h>

void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug);
void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug, bool restoreDir);
void saveSettings(SDL_Window *window, int fontScale, bool debug);

#endif
Expand Down