Support for frequency manager categories (as files)

Base class for frequency manager views
Menuview clear/add bugfix
This commit is contained in:
furrtek 2017-06-22 09:08:37 +01:00
parent abd154b3c7
commit 08391bba4f
8 changed files with 351 additions and 410 deletions

View file

@ -23,129 +23,107 @@
#include "freqman.hpp"
#include <algorithm>
#define FREQMAN_MAX_PER_CAT 64
#define FREQMAN_CAT_MAX_LEN 8
#define FREQMAN_DESC_MAX_LEN 32
#define FREQMAN_DESC_MAX_LEN 30
#define FREQMAN_MAX_PER_FILE 256
bool load_freqman_file(freqman_db &db) {
File freqs_file;
size_t length, span_end, n = 0;
uint64_t seek_pos = 0;
char * line_end;
int32_t category_id;
char * pos;
char desc_buffer[FREQMAN_DESC_MAX_LEN + 1] = { 0 };
char category_buffer[FREQMAN_CAT_MAX_LEN + 1] = { 0 };
std::string description;
rf::Frequency value;
std::vector<std::string>::iterator category_find;
std::vector<std::string> get_freqman_files() {
std::vector<std::string> file_list;
char file_buffer[256];
auto files = scan_root_files(u"FREQMAN", u"*.TXT");
while (freqs_file.open("freqman.txt").is_valid()) {
auto result = freqs_file.create("freqman.txt");
if (result.is_valid())
return false;
for (auto file : files) {
file_list.emplace_back(file.stem().string());
}
freqs_file.read(file_buffer, 256);
return file_list;
};
bool load_freqman_file(std::string& file_stem, freqman_db &db) {
File freqman_file;
size_t length, span_end, n = 0;
uint64_t seek_pos = 0;
char * pos;
char * line_end;
std::string description;
rf::Frequency value;
char file_data[256];
while ((pos = strstr(file_buffer, "f=")) && (n < FREQMAN_MAX_PER_CAT)) {
db.entries.clear();
auto result = freqman_file.open("FREQMAN/" + file_stem + ".TXT");
if (result.is_valid())
return false;
freqman_file.read(file_data, 256);
while ((pos = strstr(file_data, "f=")) && (n < FREQMAN_MAX_PER_FILE)) {
// Cut buffer at end of line
line_end = file_buffer;
do {
span_end = strcspn(line_end, "\x0D\x0A");
line_end += (span_end + 1);
} while (span_end);
// Trim buffer at end of last complete line
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;
// Read frequency
pos += 2;
value = strtol(pos, nullptr, 10); // Read frequency
value = strtol(pos, nullptr, 10);
pos = strstr(file_buffer, "d=");
// Read description until , or LF
pos = strstr(file_data, "d=");
if (pos) {
pos += 2;
length = strcspn(pos, ",\x0D\x0A"); // Read description until , or CR LF
if (length > FREQMAN_DESC_MAX_LEN) length = FREQMAN_DESC_MAX_LEN;
memcpy(desc_buffer, pos, length);
desc_buffer[length] = (char)0;
description = desc_buffer;
length = std::min(strcspn(pos, ",\x0A"), (size_t)FREQMAN_DESC_MAX_LEN);
description = string(pos, length);
} else {
description = "-";
}
pos = strstr(file_buffer, "c=");
if (pos) {
pos += 2;
length = strcspn(pos, ",\x0D\x0A"); // Read category name until , or CR LF
if (length > FREQMAN_CAT_MAX_LEN) length = FREQMAN_CAT_MAX_LEN;
memcpy(category_buffer, pos, length);
category_buffer[length] = (char)0;
// See if we already know that category
category_find = find(db.categories.begin(), db.categories.end(), category_buffer);
if (category_find == db.categories.end()) {
// Not found: add to list
db.categories.push_back(category_buffer);
category_id = db.categories.size() - 1;
} else {
// Found
category_id = category_find - db.categories.begin();
}
} else {
category_id = -1; // Uncategorized
}
db.entries.push_back({ value, "", description, category_id });
db.entries.push_back({ value, "", description });
n++;
seek_pos += (line_end - file_buffer);
seek_pos += (line_end - file_data);
if (freqs_file.seek(seek_pos).value() == seek_pos)
if (freqman_file.seek(seek_pos).value() == seek_pos)
break;
else
freqs_file.read(file_buffer, 256);
freqman_file.read(file_data, 256);
}
//chHeapFree(file_buffer);
return true;
}
bool save_freqman_file(freqman_db &db) {
File freqs_file;
size_t n;
bool save_freqman_file(std::string& file_stem, freqman_db &db) {
File freqman_file;
std::string item_string;
int32_t category_id;
if (!create_freqman_file(freqs_file)) return false;
if (!create_freqman_file(file_stem, freqman_file))
return false;
for (n = 0; n < db.entries.size(); n++) {
for (size_t n = 0; n < db.entries.size(); n++) {
item_string = "f=" + to_string_dec_uint(db.entries[n].value);
if (db.entries[n].description.size())
item_string += ",d=" + db.entries[n].description;
category_id = db.entries[n].category_id;
if ((category_id >= 0) && (category_id < (int32_t)db.categories.size()))
item_string += ",c=" + db.categories[db.entries[n].category_id];
freqs_file.write_line(item_string);
freqman_file.write_line(item_string);
}
return true;
}
bool create_freqman_file(File &freqs_file) {
auto result = freqs_file.create("freqman.txt");
bool create_freqman_file(std::string& file_stem, File& freqman_file) {
auto result = freqman_file.create("FREQMAN/" + file_stem + ".TXT");
if (result.is_valid())
return false;
return true;
}
std::string freqman_item_string(freqman_entry &entry) {
std::string freqman_item_string(freqman_entry &entry, size_t max_length) {
std::string item_string, frequency_str, description;
rf::Frequency value;
@ -153,14 +131,10 @@ std::string freqman_item_string(freqman_entry &entry) {
entry.frequency_str = to_string_dec_int(value / 1000000, 4) + "." +
to_string_dec_int((value / 100) % 10000, 4, '0');
item_string = entry.frequency_str + "M: ";
item_string = entry.frequency_str + "M: " + entry.description;
if (entry.description.size() <= 19) {
item_string += entry.description;
} else {
// Cut if too long
item_string += entry.description.substr(0, 16) + "...";
}
if (entry.description.size() > max_length)
return item_string.substr(0, max_length - 3) + "...";
return item_string;
}