diff options
author | Robert Kukla <roolku@rockbox.org> | 2007-09-13 00:18:28 +0000 |
---|---|---|
committer | Robert Kukla <roolku@rockbox.org> | 2007-09-13 00:18:28 +0000 |
commit | c9a00ab3aca1898a16ce1704e52041023336e1b4 (patch) | |
tree | 29f57db8d09131a0d588789be47dece6b1050958 | |
parent | f265ee9cd5e4f4798fcff4e07c7e8efaff308eb0 (diff) | |
download | rockbox-c9a00ab3aca1898a16ce1704e52041023336e1b4.tar.gz rockbox-c9a00ab3aca1898a16ce1704e52041023336e1b4.zip |
fix two bugs related to no-existing settings/bookmark files and simplify the viewer_load_settings() function in the process.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14680 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/plugins/viewer.c | 109 |
1 files changed, 53 insertions, 56 deletions
diff --git a/apps/plugins/viewer.c b/apps/plugins/viewer.c index ab93200963..055ace08c5 100644 --- a/apps/plugins/viewer.c +++ b/apps/plugins/viewer.c | |||
@@ -1045,80 +1045,77 @@ static void viewer_reset_settings(void) | |||
1045 | 1045 | ||
1046 | static void viewer_load_settings(void) /* same name as global, but not the same file.. */ | 1046 | static void viewer_load_settings(void) /* same name as global, but not the same file.. */ |
1047 | { | 1047 | { |
1048 | int settings_fd; | 1048 | int settings_fd, i; |
1049 | 1049 | struct bookmark_file_data *data; | |
1050 | |||
1051 | /* read settings file */ | ||
1050 | settings_fd=rb->open(SETTINGS_FILE, O_RDONLY); | 1052 | settings_fd=rb->open(SETTINGS_FILE, O_RDONLY); |
1051 | if (settings_fd < 0) | 1053 | if ((settings_fd < 0) || (rb->filesize(settings_fd) != sizeof(struct preferences))) |
1052 | { | 1054 | { |
1053 | rb->splash(HZ*2, "No Settings File"); | 1055 | rb->splash(HZ*2, "No Valid Settings File"); |
1054 | return; | 1056 | } |
1055 | } | 1057 | else |
1056 | if (rb->filesize(settings_fd) != sizeof(struct preferences)) | ||
1057 | { | 1058 | { |
1058 | rb->splash(HZ*2, "Settings File Invalid"); | 1059 | rb->read(settings_fd, &prefs, sizeof(struct preferences)); |
1059 | return; | 1060 | rb->close(settings_fd); |
1060 | } | 1061 | } |
1061 | 1062 | ||
1062 | rb->read(settings_fd, &prefs, sizeof(struct preferences)); | 1063 | data = (struct bookmark_file_data*)buffer; /* grab the text buffer */ |
1063 | rb->close(settings_fd); | 1064 | data->bookmarked_files_count = 0; |
1065 | |||
1066 | /* read bookmarks if file exists */ | ||
1064 | settings_fd = rb->open(BOOKMARKS_FILE, O_RDONLY); | 1067 | settings_fd = rb->open(BOOKMARKS_FILE, O_RDONLY); |
1065 | if (settings_fd >= 0) | 1068 | if (settings_fd >= 0) |
1066 | { | 1069 | { |
1067 | struct bookmark_file_data *data = (struct bookmark_file_data*)buffer; /* grab the text buffer */ | 1070 | /* figure out how many items to read */ |
1068 | int i; | 1071 | rb->read(settings_fd, &data->bookmarked_files_count, sizeof(signed int)); |
1069 | data->bookmarked_files_count = 0; | ||
1070 | rb->read(settings_fd, &data->bookmarked_files_count, sizeof(signed int)); /* figure out how many items to read */ | ||
1071 | if (data->bookmarked_files_count > MAX_BOOKMARKED_FILES) | 1072 | if (data->bookmarked_files_count > MAX_BOOKMARKED_FILES) |
1072 | data->bookmarked_files_count = MAX_BOOKMARKED_FILES; /* dump the older files */ | 1073 | data->bookmarked_files_count = MAX_BOOKMARKED_FILES; |
1073 | rb->read(settings_fd, data->bookmarks, | 1074 | rb->read(settings_fd, data->bookmarks, |
1074 | sizeof(struct bookmarked_file_info) * data->bookmarked_files_count); | 1075 | sizeof(struct bookmarked_file_info) * data->bookmarked_files_count); |
1075 | rb->close(settings_fd); | 1076 | rb->close(settings_fd); |
1076 | for (i=0; i < data->bookmarked_files_count; i++) | 1077 | } |
1077 | { | 1078 | |
1078 | if (!rb->strcmp(file_name, data->bookmarks[i].filename)) | 1079 | /* check if current file is in list */ |
1079 | break; | 1080 | for (i=0; i < data->bookmarked_files_count; i++) |
1080 | } | 1081 | { |
1081 | if (i < data->bookmarked_files_count) | 1082 | if (!rb->strcmp(file_name, data->bookmarks[i].filename)) |
1082 | { | 1083 | { |
1083 | /* it is in the list, write everything back in the correct order, and reload the file correctly */ | ||
1084 | settings_fd = rb->creat(BOOKMARKS_FILE); | ||
1085 | if (settings_fd >=0 ) | ||
1086 | { | ||
1087 | if (data->bookmarked_files_count > MAX_BOOKMARKED_FILES) | ||
1088 | data->bookmarked_files_count = MAX_BOOKMARKED_FILES; /* dump the older files */ | ||
1089 | rb->write (settings_fd, &data->bookmarked_files_count, sizeof(signed int)); | ||
1090 | /* write this item, then all up to it, then all after it */ | ||
1091 | rb->write (settings_fd, &data->bookmarks[i], sizeof(struct bookmarked_file_info)); | ||
1092 | rb->write (settings_fd, data->bookmarks, sizeof(struct bookmarked_file_info)*i); | ||
1093 | rb->write (settings_fd, &data->bookmarks[i+1], | ||
1094 | sizeof(struct bookmarked_file_info)*(data->bookmarked_files_count-i-1)); | ||
1095 | rb->close(settings_fd); | ||
1096 | } | ||
1097 | file_pos = data->bookmarks[i].file_position; | 1084 | file_pos = data->bookmarks[i].file_position; |
1098 | screen_top_ptr = buffer + data->bookmarks[i].top_ptr_pos; | 1085 | screen_top_ptr = buffer + data->bookmarks[i].top_ptr_pos; |
1099 | } | 1086 | break; |
1100 | else /* not in list, write the list to the file */ | 1087 | } |
1101 | { | 1088 | } |
1102 | settings_fd = rb->creat(BOOKMARKS_FILE); | 1089 | |
1103 | if (settings_fd >=0 ) | 1090 | /* prevent potential slot overflow */ |
1104 | { | 1091 | if (i >= data->bookmarked_files_count) |
1105 | if ((data->bookmarked_files_count + 1) > MAX_BOOKMARKED_FILES) | 1092 | { |
1106 | data->bookmarked_files_count = MAX_BOOKMARKED_FILES; /* dump the older files */ | 1093 | if (i < MAX_BOOKMARKED_FILES) |
1107 | else data->bookmarked_files_count++; | 1094 | data->bookmarked_files_count++; |
1108 | rb->write (settings_fd, &data->bookmarked_files_count, sizeof(signed int)); | 1095 | else |
1109 | rb->PREFIX(lseek)(settings_fd,sizeof(struct bookmarked_file_info),SEEK_CUR); | 1096 | i = MAX_BOOKMARKED_FILES-1; |
1110 | // rb->memset(&dummy,0,sizeof(struct bookmarked_file_info)); /* the actual info will be written on exit */ | 1097 | } |
1111 | //rb->write (settings_fd, &dummy, sizeof(struct bookmarked_file_info)); | 1098 | |
1112 | rb->write (settings_fd, data->bookmarks, sizeof(struct bookmarked_file_info)*(data->bookmarked_files_count)); | 1099 | /* write bookmark file with spare slot in first position |
1113 | rb->close(settings_fd); | 1100 | to be filled in by viewer_save_settings */ |
1114 | } | 1101 | settings_fd = rb->open(BOOKMARKS_FILE, O_WRONLY|O_CREAT); |
1115 | } | 1102 | if (settings_fd >=0 ) |
1116 | } /* BOOKMARKS_FILE opened ok */ | 1103 | { |
1104 | /* write count and skip first slot */ | ||
1105 | rb->write (settings_fd, &data->bookmarked_files_count, sizeof(signed int)); | ||
1106 | rb->PREFIX(lseek)(settings_fd,sizeof(struct bookmarked_file_info),SEEK_CUR); | ||
1107 | |||
1108 | /* shuffle up bookmarks */ | ||
1109 | rb->write (settings_fd, data->bookmarks, sizeof(struct bookmarked_file_info)*i); | ||
1110 | rb->close(settings_fd); | ||
1111 | } | ||
1112 | |||
1117 | init_need_scrollbar(); | 1113 | init_need_scrollbar(); |
1118 | 1114 | ||
1119 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ | 1115 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ |
1120 | 1116 | ||
1121 | if (BUFFER_OOB(screen_top_ptr)) { | 1117 | if (BUFFER_OOB(screen_top_ptr)) |
1118 | { | ||
1122 | screen_top_ptr = buffer; | 1119 | screen_top_ptr = buffer; |
1123 | } | 1120 | } |
1124 | 1121 | ||