1313#include < ctype.h>
1414#include < sys/stat.h>
1515
16- #include " platform/sdl/settings.h"
1716#include " ui/utils.h"
1817#include " ui/textedit.h"
1918#include " common/smbas.h"
19+ #include " platform/sdl/settings.h"
2020
2121static const char *ENV_VARS[] = {
2222 " APPDATA" , " HOME" , " TMP" , " TEMP" , " TMPDIR"
@@ -29,13 +29,17 @@ static const char *ENV_VARS[] = {
2929#define DEFAULT_WIDTH 640
3030#define DEFAULT_HEIGHT 480
3131#define DEFAULT_SCALE 100
32+ #define NUM_RECENT_ITEMS 9
3233
3334#if defined(__MINGW32__)
3435#define makedir (f ) mkdir(f)
3536#else
3637#define makedir (f ) mkdir(f, 0700 )
3738#endif
3839
40+ strlib::String recentPath[NUM_RECENT_ITEMS];
41+ int recentPosition[NUM_RECENT_ITEMS];
42+
3943FILE *openConfig (const char *flags, bool debug) {
4044 FILE *result = NULL ;
4145 char path[FILENAME_MAX];
@@ -94,19 +98,39 @@ int nextHex(FILE *fp, int def) {
9498}
9599
96100//
97- // sets the next string in the file as the current working directory
101+ // returns the length of the next string
98102//
99- void restorePath (FILE *fp) {
103+ int nextString (FILE *fp) {
100104 int pos = ftell (fp);
101105 int len = 0 ;
102- for (int c = fgetc (fp); c != EOF && c != ' ,' && c != ' \n ' ; c = fgetc (fp)) {
106+ for (int c = fgetc (fp); c != EOF; c = fgetc (fp)) {
107+ if (c == ' \n ' ) {
108+ if (len > 0 ) {
109+ // string terminator
110+ break ;
111+ } else {
112+ // skip previous terminator
113+ pos++;
114+ continue ;
115+ }
116+ }
103117 len++;
104118 }
105119 fseek (fp, pos, SEEK_SET);
120+ return len;
121+ }
122+
123+ //
124+ // sets the next string in the file as the current working directory
125+ //
126+ void restorePath (FILE *fp, bool restoreDir) {
127+ int len = nextString (fp);
106128 if (len > 0 ) {
107129 String path;
108130 path.append (fp, len);
109- chdir (path.c_str ());
131+ if (restoreDir) {
132+ chdir (path.c_str ());
133+ }
110134 }
111135}
112136
@@ -127,8 +151,17 @@ void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug, bool restoreDir
127151 for (int i = 0 ; i < THEME_COLOURS; i++) {
128152 g_user_theme[i] = nextHex (fp, g_user_theme[i]);
129153 }
130- if (restoreDir) {
131- restorePath (fp);
154+ restorePath (fp, restoreDir);
155+
156+ // restore recent paths
157+ for (int i = 0 ; i < NUM_RECENT_ITEMS; i++) {
158+ int len = nextString (fp);
159+ if (len > 1 ) {
160+ recentPath[i].empty ();
161+ recentPath[i].append (fp, len);
162+ } else {
163+ break ;
164+ }
132165 }
133166 fclose (fp);
134167 } else {
@@ -158,6 +191,7 @@ void saveSettings(SDL_Window *window, int fontScale, bool debug) {
158191 for (int i = 0 ; i < THEME_COLOURS; i++) {
159192 fprintf (fp, (i + 1 < THEME_COLOURS ? " %06x," : " %06x" ), g_user_theme[i]);
160193 }
194+ fprintf (fp, " \n " );
161195
162196 // save the current working directory
163197 char path[FILENAME_MAX + 1 ];
@@ -168,11 +202,47 @@ void saveSettings(SDL_Window *window, int fontScale, bool debug) {
168202 path[i] = ' /' ;
169203 }
170204 }
171- fprintf (fp, " \n %s\n " , path);
172- } else {
173- fprintf (fp, " \n %s\n " , path);
174205 }
206+ fprintf (fp, " %s\n " , path);
207+
208+ // save the recent editor paths
209+ for (int i = 0 ; i < NUM_RECENT_ITEMS; i++) {
210+ if (recentPath[i].length () > 0 ) {
211+ fprintf (fp, " %s\n " , recentPath[i].c_str ());
212+ }
213+ }
214+
175215 fclose (fp);
176216 }
177217}
178218
219+ bool getRecentFile (strlib::String &path, unsigned position) {
220+ bool result = false ;
221+ if (position < NUM_RECENT_ITEMS && recentPath[position].length () > 0 &&
222+ !recentPath[position].equals (path)) {
223+ path.empty ();
224+ path.append (recentPath[position]);
225+ result = true ;
226+ }
227+ return result;
228+ }
229+
230+ void setRecentFile (const char *filename) {
231+ bool found = false ;
232+ for (int i = 0 ; i < NUM_RECENT_ITEMS && !found; i++) {
233+ if (recentPath[i].equals (filename, false )) {
234+ found = true ;
235+ }
236+ }
237+ if (!found) {
238+ // shift items downwards
239+ for (int i = NUM_RECENT_ITEMS - 1 ; i > 0 ; i--) {
240+ recentPath[i].empty ();
241+ recentPath[i].append (recentPath[i - 1 ]);
242+ }
243+
244+ // create new item in first position
245+ recentPath[0 ].empty ();
246+ recentPath[0 ].append (filename);
247+ }
248+ }
0 commit comments