summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Arnold <amiconn@rockbox.org>2005-09-02 01:15:35 +0000
committerJens Arnold <amiconn@rockbox.org>2005-09-02 01:15:35 +0000
commit58e9412bff9947e4514c0d55152bfad91049a60c (patch)
treedc4cc3d7812388805c07c08d4cd9271eecab938e
parentc7240cf844d7c2a28031aa5ec984f0e007de242d (diff)
downloadrockbox-58e9412bff9947e4514c0d55152bfad91049a60c.tar.gz
rockbox-58e9412bff9947e4514c0d55152bfad91049a60c.zip
Added universal functions for creation of numbered filenames and date+time filenames (units with RTC only). Some size optimisations within code using these new functions. Everything should behave as before, except config saving will always find the highest file number + 1 even if the sequence is non-contiguous.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7449 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/misc.c113
-rw-r--r--apps/misc.h8
-rw-r--r--apps/recorder/recording.c47
-rw-r--r--apps/settings.c58
4 files changed, 104 insertions, 122 deletions
diff --git a/apps/misc.c b/apps/misc.c
index f27263b6b7..6751da9d69 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -89,7 +89,7 @@ char *output_dyn_value(char *buf, int buf_size, int value,
89 tbuf[i] = '\0'; 89 tbuf[i] = '\0';
90 90
91 talk_number(value, true); 91 talk_number(value, true);
92 if (strlen(tbuf)) 92 if (tbuf[0] != 0)
93 { 93 {
94 talk_id(LANG_POINT, true); 94 talk_id(LANG_POINT, true);
95 talk_spell(tbuf, true); 95 talk_spell(tbuf, true);
@@ -99,10 +99,78 @@ char *output_dyn_value(char *buf, int buf_size, int value,
99 return buf; 99 return buf;
100} 100}
101 101
102/* Create a filename with a number part in a way that the number is 1
103 higher than the highest numbered file matching the same pattern.
104 It is allowed that buffer and path point to the same memory location,
105 saving a strcpy(). Path must always be given without trailing slash,. */
106char *create_numbered_filename(char *buffer, const char *path,
107 const char *prefix, const char *suffix,
108 int numberlen)
109{
110 DIR *dir;
111 struct dirent *entry;
112 int max_num = 0;
113 int pathlen;
114 int prefixlen = strlen(prefix);
115 char fmtstring[12];
116
117 if (buffer != path)
118 strncpy(buffer, path, MAX_PATH);
119
120 pathlen = strlen(buffer);
121
122 dir = opendir(pathlen ? buffer : "/");
123 if (!dir)
124 return NULL;
125
126 while ((entry = readdir(dir)))
127 {
128 int curr_num;
129
130 if (strncasecmp(entry->d_name, prefix, prefixlen)
131 || strcasecmp(entry->d_name + prefixlen + numberlen, suffix))
132 continue;
133
134 curr_num = atoi(entry->d_name + prefixlen);
135 if (curr_num > max_num)
136 max_num = curr_num;
137 }
138 closedir(dir);
139
140 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
141 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
142 max_num + 1, suffix);
143
144 return buffer;
145}
146
147#ifdef HAVE_RTC
148/* Create a filename with a date+time part.
149 It is allowed that buffer and path point to the same memory location,
150 saving a strcpy(). Path must always be given without trailing slash. */
151char *create_datetime_filename(char *buffer, const char *path,
152 const char *prefix, const char *suffix)
153{
154 struct tm *tm = get_time();
155 int pathlen;
156
157 if (buffer != path)
158 strncpy(buffer, path, MAX_PATH);
159
160 pathlen = strlen(buffer);
161 snprintf(buffer + pathlen, MAX_PATH - pathlen,
162 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix,
163 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
164 tm->tm_hour, tm->tm_min, tm->tm_sec, suffix);
165
166 return buffer;
167}
168#endif /* HAVE_RTC */
169
102/* Read (up to) a line of text from fd into buffer and return number of bytes 170/* Read (up to) a line of text from fd into buffer and return number of bytes
103 * read (which may be larger than the number of bytes stored in buffer). If 171 * read (which may be larger than the number of bytes stored in buffer). If
104 * an error occurs, -1 is returned (and buffer contains whatever could be 172 * an error occurs, -1 is returned (and buffer contains whatever could be
105 * read). A line is terminated by a LF char. Neither LF nor CR chars are 173 * read). A line is terminated by a LF char. Neither LF nor CR chars are
106 * stored in buffer. 174 * stored in buffer.
107 */ 175 */
108int read_line(int fd, char* buffer, int buffer_size) 176int read_line(int fd, char* buffer, int buffer_size)
@@ -212,44 +280,9 @@ void screen_dump(void)
212#endif 280#endif
213 281
214#ifdef HAVE_RTC 282#ifdef HAVE_RTC
215 struct tm *tm = get_time(); 283 create_datetime_filename(filename, "", "dump ", ".bmp");
216
217 snprintf(filename, MAX_PATH, "/dump %04d-%02d-%02d %02d-%02d-%02d.bmp",
218 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
219 tm->tm_hour, tm->tm_min, tm->tm_sec);
220#else 284#else
221 { 285 create_numbered_filename(filename, "", "dump_", ".bmp", 4);
222 DIR* dir;
223 int max_dump_file = 1; /* default to dump_0001.bmp */
224 dir = opendir("/");
225 if (dir) /* found */
226 {
227 /* Search for the highest screendump filename present,
228 increment behind that. So even with "holes"
229 (deleted files), the newest will always have the
230 highest number. */
231 while(true)
232 {
233 struct dirent* entry;
234 int curr_dump_file;
235 /* walk through the directory content */
236 entry = readdir(dir);
237 if (!entry)
238 {
239 closedir(dir);
240 break; /* end of dir */
241 }
242 if (strncasecmp(entry->d_name, "dump_", 5))
243 continue; /* no screendump file */
244 curr_dump_file = atoi(&entry->d_name[5]);
245 if (curr_dump_file >= max_dump_file)
246 max_dump_file = curr_dump_file + 1;
247 }
248 }
249 snprintf(filename, MAX_PATH,
250 "/dump_%04d.bmp", max_dump_file);
251 }
252
253#endif 286#endif
254 287
255 fh = creat(filename, O_WRONLY); 288 fh = creat(filename, O_WRONLY);
diff --git a/apps/misc.h b/apps/misc.h
index 757eee7377..5e7450a7b2 100644
--- a/apps/misc.h
+++ b/apps/misc.h
@@ -27,6 +27,14 @@
27void output_dyn_value(char *buf, int buf_size, int value, 27void output_dyn_value(char *buf, int buf_size, int value,
28 const unsigned char **units, bool bin_scale); 28 const unsigned char **units, bool bin_scale);
29 29
30char *create_numbered_filename(char *buffer, const char *path,
31 const char *prefix, const char *suffix,
32 int numberlen);
33#ifdef HAVE_RTC
34char *create_datetime_filename(char *buffer, const char *path,
35 const char *prefix, const char *suffix);
36#endif
37
30/* Read (up to) a line of text from fd into buffer and return number of bytes 38/* Read (up to) a line of text from fd into buffer and return number of bytes
31 * read (which may be larger than the number of bytes stored in buffer). If 39 * read (which may be larger than the number of bytes stored in buffer). If
32 * an error occurs, -1 is returned (and buffer contains whatever could be 40 * an error occurs, -1 is returned (and buffer contains whatever could be
diff --git a/apps/recorder/recording.c b/apps/recorder/recording.c
index ea71538322..3fcf97b177 100644
--- a/apps/recorder/recording.c
+++ b/apps/recorder/recording.c
@@ -164,56 +164,15 @@ void adjust_cursor(void)
164 164
165char *rec_create_filename(char *buffer) 165char *rec_create_filename(char *buffer)
166{ 166{
167 int fpos;
168
169 if(global_settings.rec_directory) 167 if(global_settings.rec_directory)
170 getcwd(buffer, MAX_PATH); 168 getcwd(buffer, MAX_PATH);
171 else 169 else
172 strncpy(buffer, rec_base_directory, MAX_PATH); 170 strncpy(buffer, rec_base_directory, MAX_PATH);
173 171
174 fpos = strlen(buffer); 172#ifdef HAVE_RTC
175#ifdef HAVE_RTC 173 create_datetime_filename(buffer, buffer, "R", ".mp3");
176 {
177 struct tm *tm = get_time();
178
179 /* Append filename to path: RYYMMDD-HH.MM.SS.mp3 */
180 snprintf(&buffer[fpos], MAX_PATH-fpos,
181 "/R%02d%02d%02d-%02d%02d%02d.mp3",
182 tm->tm_year%100, tm->tm_mon+1, tm->tm_mday,
183 tm->tm_hour, tm->tm_min, tm->tm_sec);
184 }
185#else 174#else
186 { 175 create_numbered_filename(buffer, buffer, "rec_", ".mp3", 4);
187 DIR* dir;
188 int max_rec_file = 1; /* default to rec_0001.mp3 */
189 dir = opendir(buffer);
190 if (dir) /* found */
191 {
192 /* Search for the highest recording filename present,
193 increment behind that. So even with "holes"
194 (deleted recordings), the newest will always have the
195 highest number. */
196 while(true)
197 {
198 struct dirent* entry;
199 int curr_rec_file;
200 /* walk through the directory content */
201 entry = readdir(dir);
202 if (!entry)
203 {
204 closedir(dir);
205 break; /* end of dir */
206 }
207 if (strncasecmp(entry->d_name, "rec_", 4))
208 continue; /* no recording file */
209 curr_rec_file = atoi(&entry->d_name[4]);
210 if (curr_rec_file >= max_rec_file)
211 max_rec_file = curr_rec_file + 1;
212 }
213 }
214 snprintf(&buffer[fpos], MAX_PATH-fpos,
215 "/rec_%04d.mp3", max_rec_file);
216 }
217#endif 176#endif
218 return buffer; 177 return buffer;
219} 178}
diff --git a/apps/settings.c b/apps/settings.c
index 6649ce3f89..00ca6c82c3 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -1215,61 +1215,45 @@ static void save_cfg_table(const struct bit_entry* p_table, int count, int fd)
1215 1215
1216bool settings_save_config(void) 1216bool settings_save_config(void)
1217{ 1217{
1218 bool done = false; 1218 int fd;
1219 int fd, i;
1220 char filename[MAX_PATH]; 1219 char filename[MAX_PATH];
1221 1220
1222 /* find unused filename */ 1221 create_numbered_filename(filename, ROCKBOX_DIR, "config", ".cfg", 2);
1223 for (i=0; ; i++) {
1224 snprintf(filename, sizeof filename, ROCKBOX_DIR "/config%02d.cfg", i);
1225 fd = open(filename, O_RDONLY);
1226 if (fd < 0)
1227 break;
1228 close(fd);
1229 }
1230 1222
1231 /* allow user to modify filename */ 1223 /* allow user to modify filename */
1232 while (!done) { 1224 while (true) {
1233 if (!kbd_input(filename, sizeof filename)) { 1225 if (!kbd_input(filename, sizeof filename)) {
1234 fd = creat(filename,0); 1226 fd = creat(filename,0);
1235 if (fd < 0) { 1227 if (fd < 0) {
1236 lcd_clear_display(); 1228 lcd_clear_display();
1237 lcd_puts(0,0,str(LANG_FAILED)); 1229 splash(HZ, true, str(LANG_FAILED));
1238 lcd_update();
1239 sleep(HZ);
1240 } 1230 }
1241 else 1231 else
1242 done = true; 1232 break;
1233 }
1234 else {
1235 lcd_clear_display();
1236 splash(HZ, true, str(LANG_RESET_DONE_CANCEL));
1237 return false;
1243 } 1238 }
1244 else
1245 break;
1246 }
1247
1248 /* abort if file couldn't be created */
1249 if (!done) {
1250 lcd_clear_display();
1251 lcd_puts(0,0,str(LANG_RESET_DONE_CANCEL));
1252 lcd_update();
1253 sleep(HZ);
1254 return false;
1255 } 1239 }
1256 1240
1257 fdprintf(fd, "# .cfg file created by rockbox %s - ", appsversion); 1241 fdprintf(fd, "# .cfg file created by rockbox %s - "
1258 fdprintf(fd, "http://www.rockbox.org\r\n#\r\n"); 1242 "http://www.rockbox.org\r\n#\r\n"
1259 fdprintf(fd, "#\r\n# wps / language / font \r\n#\r\n"); 1243 "#\r\n# wps / language / font \r\n#\r\n", appsversion);
1260 1244
1261 if (global_settings.wps_file[0] != 0) 1245 if (global_settings.wps_file[0] != 0)
1262 fdprintf(fd, "wps: %s/%s.wps\r\n", ROCKBOX_DIR, 1246 fdprintf(fd, "wps: %s/%s.wps\r\n", ROCKBOX_DIR,
1263 global_settings.wps_file); 1247 global_settings.wps_file);
1264 1248
1265 if (global_settings.lang_file[0] != 0) 1249 if (global_settings.lang_file[0] != 0)
1266 fdprintf(fd, "lang: %s/%s.lng\r\n", ROCKBOX_DIR LANG_DIR, 1250 fdprintf(fd, "lang: %s/%s.lng\r\n", ROCKBOX_DIR LANG_DIR,
1267 global_settings.lang_file); 1251 global_settings.lang_file);
1268 1252
1269#ifdef HAVE_LCD_BITMAP 1253#ifdef HAVE_LCD_BITMAP
1270 if (global_settings.font_file[0] != 0) 1254 if (global_settings.font_file[0] != 0)
1271 fdprintf(fd, "font: %s/%s.fnt\r\n", ROCKBOX_DIR FONT_DIR, 1255 fdprintf(fd, "font: %s/%s.fnt\r\n", ROCKBOX_DIR FONT_DIR,
1272 global_settings.font_file); 1256 global_settings.font_file);
1273#endif 1257#endif
1274 1258
1275 /* here's the action: write values to file, specified via table */ 1259 /* here's the action: write values to file, specified via table */
@@ -1279,10 +1263,8 @@ bool settings_save_config(void)
1279 close(fd); 1263 close(fd);
1280 1264
1281 lcd_clear_display(); 1265 lcd_clear_display();
1282 lcd_puts(0,0,str(LANG_SETTINGS_SAVED1)); 1266 splash(HZ, true, "%s %s", str(LANG_SETTINGS_SAVED1),
1283 lcd_puts(0,1,str(LANG_SETTINGS_SAVED2)); 1267 str(LANG_SETTINGS_SAVED2));
1284 lcd_update();
1285 sleep(HZ);
1286 return true; 1268 return true;
1287} 1269}
1288 1270