summaryrefslogtreecommitdiffstats
path: root/convert-presets/convert-presets.cpp
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2022-05-30 19:40:31 +0900
committerSlávek Banko <slavek.banko@axis.cz>2022-05-30 21:55:12 +0200
commit9d7329284fd42187092c829ae6ed030561002776 (patch)
tree1d53503d17938eca725caee7ff783fccf52ca509 /convert-presets/convert-presets.cpp
parentfac82f62f156c76cb4b23c313111283b30a778b5 (diff)
downloadtderadio-9d732928.tar.gz
tderadio-9d732928.zip
Standardize folder structure.
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it> (cherry picked from commit d95a4fea540b371fa86493d069fdbd54f33c5b40)
Diffstat (limited to 'convert-presets/convert-presets.cpp')
-rw-r--r--convert-presets/convert-presets.cpp192
1 files changed, 192 insertions, 0 deletions
diff --git a/convert-presets/convert-presets.cpp b/convert-presets/convert-presets.cpp
new file mode 100644
index 0000000..c6ee08c
--- /dev/null
+++ b/convert-presets/convert-presets.cpp
@@ -0,0 +1,192 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <tdeapplication.h>
+#include <tqstring.h>
+#include <tqtextstream.h>
+#include <tqfile.h>
+#include <tdelocale.h>
+#include <kdebug.h>
+#include <tdeaboutdata.h>
+#include <tdecmdlineargs.h>
+#include <tqregexp.h>
+#include <time.h>
+#include <sys/fcntl.h>
+#include <unistd.h>
+
+#define dev_urandom "/dev/urandom"
+
+TQString createStationID()
+{
+ const int buffersize = 32;
+ unsigned char buffer[buffersize];
+
+ TQString stime, srandom = "";
+ stime.setNum(time(NULL));
+
+ int fd = open (dev_urandom, O_RDONLY);
+ read(fd, buffer, buffersize);
+ close(fd);
+ for (int i = 0; i < buffersize; ++i)
+ srandom += TQString().sprintf("%02X", (unsigned int)buffer[i]);
+
+// kdDebug() << i18n("generated StationID: ") << stime << srandom << endl;
+
+ return stime + srandom;
+}
+
+
+
+
+bool convertFile(const TQString &file)
+{
+ ////////////////////////////////////////////////////////////////////////
+ // read input
+ ////////////////////////////////////////////////////////////////////////
+
+ TQFile presetFile (file);
+
+ if (! presetFile.open(IO_ReadOnly)) {
+ kdDebug() << "convertFile: "
+ << i18n("error opening preset file")
+ << " " << file << " "
+ << i18n("for reading") << endl;
+ return false;
+ }
+
+ TQString xmlData;
+
+ // make sure that qtextstream is gone when we close presetFile
+ {
+ TQTextStream ins(&presetFile);
+ ins.setEncoding(TQTextStream::Locale);
+ xmlData = ins.read();
+ }
+
+ if (xmlData.find("<format>", 0, false) >= 0) {
+ kdDebug() << "file " << file << " already in new format" << endl;
+ // but add <?xml line at beginning if missing
+
+ {
+ presetFile.reset();
+ TQTextStream ins(&presetFile);
+ ins.setEncoding(TQTextStream::UnicodeUTF8);
+ xmlData = ins.read();
+ }
+
+ if (xmlData.find("<?xml", 0, false) < 0) {
+ xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlData;
+ }
+
+ } else {
+
+ ////////////////////////////////////////////////////////////////////////
+ // convert file
+ ////////////////////////////////////////////////////////////////////////
+
+ TQRegExp qselect("<quickselect>.*</quickselect>");
+ TQRegExp docking("<dockingmenu>.*</dockingmenu>");
+ TQRegExp station("<station>(.*)</station>");
+ TQRegExp stationlist("<stationlist>");
+ TQRegExp emptyLines("\\n\\s*\\n");
+
+ #define stationIDElement "stationID"
+
+ qselect.setMinimal(true);
+ docking.setMinimal(true);
+ station.setMinimal(true);
+
+ xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlData;
+ xmlData.replace(stationlist, "<stationlist>\n\t\t<format>tderadio-1.0</format>");
+ xmlData.replace(qselect, "");
+ xmlData.replace(docking, "");
+ xmlData.replace(station, "<FrequencyRadioStation>\n"
+ "\t\t\t<" stationIDElement "></" stationIDElement ">"
+ "\\1</FrequencyRadioStation>"
+ );
+
+ int p = 0;
+ int f = 0;
+ while ( (f = xmlData.find("<" stationIDElement "></" stationIDElement ">", p) ) >= 0) {
+ xmlData.insert(f + 2 + TQString(stationIDElement).length(), createStationID());
+ }
+
+ xmlData.replace(emptyLines, "\n");
+ }
+
+ presetFile.close();
+
+
+ ////////////////////////////////////////////////////////////////////////
+ // write output
+ ////////////////////////////////////////////////////////////////////////
+
+ if (! presetFile.open(IO_WriteOnly)) {
+ kdDebug() << "convertFile: "
+ << i18n("error opening preset file")
+ << " " << file << " "
+ << i18n("for writing") << endl;
+ return false;
+ }
+
+ TQTextStream outs(&presetFile);
+ outs.setEncoding(TQTextStream::UnicodeUTF8);
+
+ outs << xmlData;
+
+ if (presetFile.status() != IO_Ok) {
+ kdDebug() << "StationList::writeXML: "
+ << i18n("error writing preset file")
+ << " " << file
+ << " (" << presetFile.state() << ")"
+ << endl;
+ return false;
+ }
+
+ return true;
+}
+
+
+static const char *description = "convert-presets";
+
+static TDECmdLineOptions options[] =
+{
+ { "q", I18N_NOOP("be quiet"), 0},
+ { "+[preset files]", I18N_NOOP("preset file to convert"), 0 },
+ TDECmdLineLastOption
+};
+
+int main(int argc, char *argv[])
+{
+ TDEAboutData aboutData("convert-presets", I18N_NOOP("convert-presets"),
+ VERSION, description, TDEAboutData::License_GPL,
+ "(c) 2003-2005 Martin Witte",
+ 0,
+ "http://sourceforge.net/projects/tderadio",
+ 0);
+ aboutData.addAuthor("Martin Witte", "", "witte@kawo1.rwth-aachen.de");
+
+ TDECmdLineArgs::init( argc, argv, &aboutData );
+ TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.
+
+ TDEApplication a (false, false);
+
+ TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
+
+ for (int i = 0; i < args->count(); ++i) {
+ const char *x = args->arg(i);
+ if (! convertFile(x)) {
+ return -1;
+ } else {
+ if (! args->isSet("q"))
+ kdDebug() << x << ": ok" << endl;
+ }
+ }
+ if (args->count() == 0) {
+ kdDebug() << "no input" << endl;
+ return -1;
+ }
+
+ return 0;
+}