summaryrefslogtreecommitdiff
path: root/apps/plugins/text_viewer/tv_reader.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/text_viewer/tv_reader.c')
-rw-r--r--apps/plugins/text_viewer/tv_reader.c42
1 files changed, 27 insertions, 15 deletions
diff --git a/apps/plugins/text_viewer/tv_reader.c b/apps/plugins/text_viewer/tv_reader.c
index b94dc17f65..cdfb01d462 100644
--- a/apps/plugins/text_viewer/tv_reader.c
+++ b/apps/plugins/text_viewer/tv_reader.c
@@ -134,14 +134,7 @@ void tv_seek(off_t offset, int whence)
134 134
135static int tv_change_preferences(const struct tv_preferences *oldp) 135static int tv_change_preferences(const struct tv_preferences *oldp)
136{ 136{
137 unsigned char bom[BOM_SIZE]; 137 bool change_file = false;
138 int cur_start_file_pos = start_file_pos;
139 off_t cur_file_pos = file_pos + buf_pos;
140
141 file_pos = 0;
142 buf_pos = 0;
143 read_size = 0;
144 start_file_pos = 0;
145 138
146 /* open the new file */ 139 /* open the new file */
147 if (oldp == NULL || rb->strcmp(oldp->file_name, preferences->file_name)) 140 if (oldp == NULL || rb->strcmp(oldp->file_name, preferences->file_name))
@@ -152,22 +145,41 @@ static int tv_change_preferences(const struct tv_preferences *oldp)
152 fd = rb->open(preferences->file_name, O_RDONLY); 145 fd = rb->open(preferences->file_name, O_RDONLY);
153 if (fd < 0) 146 if (fd < 0)
154 return TV_CALLBACK_ERROR; 147 return TV_CALLBACK_ERROR;
148
149 file_size = rb->filesize(fd);
150 change_file = true;
155 } 151 }
156 152
157 /* 153 /*
158 * When a file is UTF-8 file with BOM, if encoding is UTF-8, 154 * When a file is UTF-8 file with BOM, if encoding is UTF-8,
159 * then file size decreases only BOM_SIZE. 155 * then file size decreases only BOM_SIZE.
160 */ 156 */
161 if (preferences->encoding == UTF_8) 157 if (change_file || oldp->encoding != preferences->encoding)
162 { 158 {
163 rb->lseek(fd, 0, SEEK_SET); 159 int old_start_file_pos = start_file_pos;
164 rb->read(fd, bom, BOM_SIZE); 160 int delta_start_file_pos;
165 if (rb->memcmp(bom, BOM, BOM_SIZE) == 0) 161 off_t cur_file_pos = file_pos + buf_pos;
166 start_file_pos = BOM_SIZE; 162
163 file_pos = 0;
164 buf_pos = 0;
165 read_size = 0;
166 start_file_pos = 0;
167
168 if (preferences->encoding == UTF_8)
169 {
170 unsigned char bom[BOM_SIZE];
171
172 rb->lseek(fd, 0, SEEK_SET);
173 rb->read(fd, bom, BOM_SIZE);
174 if (rb->memcmp(bom, BOM, BOM_SIZE) == 0)
175 start_file_pos = BOM_SIZE;
176 }
177
178 delta_start_file_pos = old_start_file_pos - start_file_pos;
179 file_size += delta_start_file_pos;
180 tv_seek(cur_file_pos + delta_start_file_pos, SEEK_SET);
167 } 181 }
168 182
169 file_size = rb->filesize(fd) - start_file_pos;
170 tv_seek(cur_file_pos + cur_start_file_pos - start_file_pos, SEEK_SET);
171 return TV_CALLBACK_OK; 183 return TV_CALLBACK_OK;
172} 184}
173 185