Changed freqman file loading to be a bit less stupid

Capped freqman file entries to 50 due to RAM overflow (?)
This commit is contained in:
furrtek 2017-10-05 03:42:46 +01:00
parent 73d47cd77d
commit 34b99042ef
5 changed files with 84 additions and 55 deletions

View File

@ -23,9 +23,6 @@
#include "freqman.hpp" #include "freqman.hpp"
#include <algorithm> #include <algorithm>
#define FREQMAN_DESC_MAX_LEN 30
#define FREQMAN_MAX_PER_FILE 256
std::vector<std::string> get_freqman_files() { std::vector<std::string> get_freqman_files() {
std::vector<std::string> file_list; std::vector<std::string> file_list;
@ -40,9 +37,9 @@ std::vector<std::string> get_freqman_files() {
bool load_freqman_file(std::string& file_stem, freqman_db &db) { bool load_freqman_file(std::string& file_stem, freqman_db &db) {
File freqman_file; File freqman_file;
size_t length, span_end, n = 0; size_t length, n = 0, file_position = 0;
uint64_t seek_pos = 0;
char * pos; char * pos;
char * line_start;
char * line_end; char * line_end;
std::string description; std::string description;
rf::Frequency value; rf::Frequency value;
@ -54,43 +51,52 @@ bool load_freqman_file(std::string& file_stem, freqman_db &db) {
if (result.is_valid()) if (result.is_valid())
return false; return false;
freqman_file.read(file_data, 256); while (1) {
freqman_file.seek(file_position);
while ((pos = strstr(file_data, "f=")) && (n < FREQMAN_MAX_PER_FILE)) { memset(file_data, 0, 256);
auto read_size = freqman_file.read(file_data, 256);
if (read_size.is_error())
return false; // Read error
// Trim buffer at end of last complete line file_position += sizeof(file_data);
line_end = file_data;
span_end = strcspn(line_end, "\x0A");
if (span_end) {
if (span_end == strlen(line_end))
return true;
}
line_end += (span_end + 1);
*line_end = (char)0;
line_start = file_data;
pos = strstr(file_data, "f=");
if (!pos) break;
// Look for complete lines in buffer
while ((line_end = strstr(line_start, "\x0A"))) {
// Read frequency // Read frequency
pos = strstr(line_start, "f=");
if (pos) {
pos += 2; pos += 2;
value = strtoll(pos, nullptr, 10); value = strtoll(pos, nullptr, 10);
} else
value = 0;
// Read description until , or LF // Read description until , or LF
pos = strstr(file_data, "d="); pos = strstr(line_start, "d=");
if (pos) { if (pos) {
pos += 2; pos += 2;
length = std::min(strcspn(pos, ",\x0A"), (size_t)FREQMAN_DESC_MAX_LEN); length = std::min(strcspn(pos, ",\x0A"), (size_t)FREQMAN_DESC_MAX_LEN);
description = string(pos, length); description = string(pos, length);
} else { } else
description = "-"; description = "-";
}
db.entries.push_back({ value, "", description }); db.entries.push_back({ value, "", description });
n++; n++;
seek_pos += (line_end - file_data); if (n >= FREQMAN_MAX_PER_FILE) return true;
if (freqman_file.seek(seek_pos).value() == seek_pos) line_start = line_end + 1;
break; }
else
freqman_file.read(file_data, 256); if (read_size.value() != sizeof(file_data))
return true; // End of file
file_position -= (file_data + sizeof(file_data) - line_start);
} }
return true; return true;

View File

@ -29,6 +29,10 @@
#ifndef __FREQMAN_H__ #ifndef __FREQMAN_H__
#define __FREQMAN_H__ #define __FREQMAN_H__
#define FREQMAN_DESC_MAX_LEN 30
#define FREQMAN_MAX_PER_FILE 50
#define FREQMAN_MAX_PER_FILE_STR "50"
using namespace ui; using namespace ui;
using namespace std; using namespace std;
@ -40,9 +44,9 @@ enum freqman_error {
}; };
struct freqman_entry { struct freqman_entry {
rf::Frequency value; rf::Frequency value { 0 };
std::string frequency_str; std::string frequency_str { };
std::string description; std::string description { };
}; };
struct freqman_db { struct freqman_db {

View File

@ -35,9 +35,6 @@ FreqManBaseView::FreqManBaseView(
{ {
file_list = get_freqman_files(); file_list = get_freqman_files();
if (!file_list.size())
error_ = ERROR_NOFILES;
add_children({ add_children({
&label_category, &label_category,
&button_exit &button_exit
@ -46,7 +43,8 @@ FreqManBaseView::FreqManBaseView(
if (file_list.size()) { if (file_list.size()) {
add_child(&options_category); add_child(&options_category);
populate_categories(); populate_categories();
} } else
error_ = ERROR_NOFILES;
// Default function // Default function
on_change_category = [this](int32_t category_id) { on_change_category = [this](int32_t category_id) {
@ -64,18 +62,23 @@ void FreqManBaseView::focus() {
if (error_ == ERROR_ACCESS) { if (error_ == ERROR_ACCESS) {
nav_.display_modal("Error", "File acces error", ABORT, nullptr); nav_.display_modal("Error", "File acces error", ABORT, nullptr);
} else if (error_ == ERROR_NOFILES) { } else if (error_ == ERROR_NOFILES) {
nav_.display_modal("Error", "No database files", ABORT, nullptr); nav_.display_modal("Error", "No database files\nin /freqman", ABORT, nullptr);
} else { } else {
options_category.focus(); options_category.focus();
} }
} }
bool FreqManBaseView::populate_categories() { void FreqManBaseView::populate_categories() {
categories.clear(); categories.clear();
for (size_t n = 0; n < file_list.size(); n++) for (size_t n = 0; n < file_list.size(); n++)
categories.emplace_back(std::make_pair(file_list[n], n)); categories.emplace_back(std::make_pair(file_list[n], n));
// Alphabetical sort
std::sort(categories.begin(), categories.end(), [](auto &left, auto &right) {
return left.first < right.first;
});
options_category.set_options(categories); options_category.set_options(categories);
options_category.set_selected_index(0); options_category.set_selected_index(0);
@ -83,8 +86,6 @@ bool FreqManBaseView::populate_categories() {
if (on_change_category) if (on_change_category)
on_change_category(category_id); on_change_category(category_id);
}; };
return true;
} }
void FreqManBaseView::change_category(int32_t category_id) { void FreqManBaseView::change_category(int32_t category_id) {
@ -125,18 +126,35 @@ void FreqManBaseView::refresh_list() {
} }
} }
void FrequencySaveView::save_current_file() {
if (database.entries.size() > FREQMAN_MAX_PER_FILE) {
nav_.display_modal(
"Error", "Too many entries, maximum is\n" FREQMAN_MAX_PER_FILE_STR ". Trim list ?",
YESNO,
[this](bool choice) {
if (choice) {
database.entries.resize(FREQMAN_MAX_PER_FILE);
save_freqman_file(file_list[current_category_id], database);
}
nav_.pop();
}
);
} else {
save_freqman_file(file_list[current_category_id], database);
nav_.pop();
}
}
void FrequencySaveView::on_save_name() { void FrequencySaveView::on_save_name() {
text_prompt(nav_, &desc_buffer, 28, [this](std::string * buffer) { text_prompt(nav_, &desc_buffer, 28, [this](std::string * buffer) {
database.entries.push_back({ value_, "", *buffer }); database.entries.push_back({ value_, "", *buffer });
save_freqman_file(file_list[current_category_id], database); save_current_file();
nav_.pop();
}); });
} }
void FrequencySaveView::on_save_timestamp() { void FrequencySaveView::on_save_timestamp() {
database.entries.push_back({ value_, "", live_timestamp.string() }); database.entries.push_back({ value_, "", live_timestamp.string() });
save_freqman_file(file_list[current_category_id], database); save_current_file();
nav_.pop();
} }
FrequencySaveView::FrequencySaveView( FrequencySaveView::FrequencySaveView(

View File

@ -54,7 +54,7 @@ protected:
std::vector<std::string> file_list { }; std::vector<std::string> file_list { };
int32_t current_category_id { 0 }; int32_t current_category_id { 0 };
bool populate_categories(); void populate_categories();
void change_category(int32_t category_id); void change_category(int32_t category_id);
void refresh_list(); void refresh_list();
@ -95,6 +95,7 @@ private:
void on_save_name(); void on_save_name();
void on_save_timestamp(); void on_save_timestamp();
void save_current_file();
BigFrequency big_display { BigFrequency big_display {
{ 4, 2 * 16, 28 * 8, 32 }, { 4, 2 * 16, 28 * 8, 32 },

Binary file not shown.