2016-12-25 19:31:38 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "freqman.hpp"
|
2017-02-03 10:10:27 -05:00
|
|
|
#include <algorithm>
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
#define FREQMAN_DESC_MAX_LEN 30
|
|
|
|
#define FREQMAN_MAX_PER_FILE 256
|
2017-02-03 10:10:27 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
std::vector<std::string> get_freqman_files() {
|
|
|
|
std::vector<std::string> file_list;
|
|
|
|
|
|
|
|
auto files = scan_root_files(u"FREQMAN", u"*.TXT");
|
|
|
|
|
|
|
|
for (auto file : files) {
|
|
|
|
file_list.emplace_back(file.stem().string());
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool load_freqman_file(std::string& file_stem, freqman_db &db) {
|
|
|
|
File freqman_file;
|
2017-02-03 10:10:27 -05:00
|
|
|
size_t length, span_end, n = 0;
|
|
|
|
uint64_t seek_pos = 0;
|
|
|
|
char * pos;
|
2017-06-22 04:08:37 -04:00
|
|
|
char * line_end;
|
2016-12-25 19:31:38 -05:00
|
|
|
std::string description;
|
|
|
|
rf::Frequency value;
|
2017-06-22 04:08:37 -04:00
|
|
|
char file_data[256];
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
db.entries.clear();
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
auto result = freqman_file.open("FREQMAN/" + file_stem + ".TXT");
|
|
|
|
if (result.is_valid())
|
|
|
|
return false;
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
freqman_file.read(file_data, 256);
|
2017-02-03 10:10:27 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
while ((pos = strstr(file_data, "f=")) && (n < FREQMAN_MAX_PER_FILE)) {
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
// 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);
|
2017-02-03 10:10:27 -05:00
|
|
|
*line_end = (char)0;
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
// Read frequency
|
2017-02-03 10:10:27 -05:00
|
|
|
pos += 2;
|
2017-06-22 19:13:13 -04:00
|
|
|
value = strtoll(pos, nullptr, 10);
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
// Read description until , or LF
|
|
|
|
pos = strstr(file_data, "d=");
|
2017-02-03 10:10:27 -05:00
|
|
|
if (pos) {
|
|
|
|
pos += 2;
|
2017-06-22 04:08:37 -04:00
|
|
|
length = std::min(strcspn(pos, ",\x0A"), (size_t)FREQMAN_DESC_MAX_LEN);
|
|
|
|
description = string(pos, length);
|
2016-12-25 19:31:38 -05:00
|
|
|
} else {
|
|
|
|
description = "-";
|
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
db.entries.push_back({ value, "", description });
|
2017-02-03 10:10:27 -05:00
|
|
|
n++;
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
seek_pos += (line_end - file_data);
|
2017-02-03 10:10:27 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
if (freqman_file.seek(seek_pos).value() == seek_pos)
|
2017-02-03 10:10:27 -05:00
|
|
|
break;
|
|
|
|
else
|
2017-06-22 04:08:37 -04:00
|
|
|
freqman_file.read(file_data, 256);
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
bool save_freqman_file(std::string& file_stem, freqman_db &db) {
|
|
|
|
File freqman_file;
|
2016-12-25 19:31:38 -05:00
|
|
|
std::string item_string;
|
2017-06-22 19:13:13 -04:00
|
|
|
rf::Frequency f;
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
if (!create_freqman_file(file_stem, freqman_file))
|
|
|
|
return false;
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
for (size_t n = 0; n < db.entries.size(); n++) {
|
2017-06-22 19:13:13 -04:00
|
|
|
f = db.entries[n].value;
|
|
|
|
item_string = "f=" + to_string_dec_uint(f / 1000) + to_string_dec_uint(f % 1000UL, 3, '0'); // Please forgive me
|
2017-02-03 10:10:27 -05:00
|
|
|
|
|
|
|
if (db.entries[n].description.size())
|
|
|
|
item_string += ",d=" + db.entries[n].description;
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
freqman_file.write_line(item_string);
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
bool create_freqman_file(std::string& file_stem, File& freqman_file) {
|
|
|
|
auto result = freqman_file.create("FREQMAN/" + file_stem + ".TXT");
|
2017-02-03 10:10:27 -05:00
|
|
|
if (result.is_valid())
|
|
|
|
return false;
|
2017-01-10 13:40:33 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
std::string freqman_item_string(freqman_entry &entry, size_t max_length) {
|
2016-12-25 19:31:38 -05:00
|
|
|
std::string item_string, frequency_str, description;
|
|
|
|
rf::Frequency value;
|
|
|
|
|
|
|
|
value = entry.value;
|
|
|
|
entry.frequency_str = to_string_dec_int(value / 1000000, 4) + "." +
|
|
|
|
to_string_dec_int((value / 100) % 10000, 4, '0');
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
item_string = entry.frequency_str + "M: " + entry.description;
|
2017-02-03 10:10:27 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
if (entry.description.size() > max_length)
|
|
|
|
return item_string.substr(0, max_length - 3) + "...";
|
2016-12-25 19:31:38 -05:00
|
|
|
|
|
|
|
return item_string;
|
|
|
|
}
|