]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
Use std::vector instead of plain array to store supported output formats
authorMichal Malý <madcatxster@devoid-pointer.net>
Fri, 17 Apr 2015 17:01:10 +0000 (19:01 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Fri, 17 Apr 2015 17:01:10 +0000 (19:01 +0200)
datafileexporter.cpp
datafileexporter.h

index d1b32947de844a358c813e89f15908198c082b76..27eb5a3b72cb77ac73f07d3f415737637a27818c 100644 (file)
@@ -28,7 +28,8 @@
 #include <QtCore/QLocale>
 #include <QtWidgets/QMessageBox>
 
-const OutputFormat DataFileExporter::SUPPORTED_FORMATS[] = { OutputFormat("Comma separated values", "csv") };
+
+const std::vector<DataFileExporter::OutputFormat> DataFileExporter::SUPPORTED_FORMATS({DataFileExporter::OutputFormat("Comma separated values", "csv")});
 
 /* Static methods */
 QStringList DataFileExporter::supportedFormats()
@@ -36,7 +37,7 @@ QStringList DataFileExporter::supportedFormats()
   QStringList list;
 
   for (const OutputFormat& of : SUPPORTED_FORMATS) {
-    list << of.first;
+    list << of.description;
   }
 
   return list;
@@ -59,7 +60,7 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
   DataFileExporter::ReturnCode ret;
   DataWriterBackend* backend;
 
-  if (formatIdx < 0 || formatIdx >= sizeof(SUPPORTED_FORMATS)) {
+  if (formatIdx < 0 || formatIdx >= SUPPORTED_FORMATS.size()) {
     return ReturnCode::E_INVAL_FORMAT;
   }
 
@@ -154,10 +155,9 @@ void DataFileExporter::guessFormatFromSuffix(const QString& name)
   if (suffix.length() == 0)
     return;
 
-  for (size_t i = 0; i < sizeof(SUPPORTED_FORMATS); i++) {
-    const OutputFormat& of = SUPPORTED_FORMATS[i];
-    if (suffix.compare(of.second) == 0) {
-      emit formatChanged(i);
+  for (int idx = 0; idx < SUPPORTED_FORMATS.size(); idx++) {
+    if (suffix.compare(SUPPORTED_FORMATS[idx].suffix) == 0) {
+      emit formatChanged(idx);
       return;
     }
   }
index 136000e542e23186fe2df529cb4d44862ffbb2f0..447eb256fd8fd33b5b88f3567e959a6bfc3dadaf 100644 (file)
@@ -31,8 +31,6 @@
 #include <QtCore/QFile>
 #include <QtCore/QObject>
 
-typedef std::pair<const QString, const QString> OutputFormat;
-
 class DataFileExporter : public QObject
 {
   Q_OBJECT
@@ -56,12 +54,19 @@ private:
     CSV,
     INVALID
   };
+  struct OutputFormat {
+    OutputFormat(const QString& description, const QString& suffix) :
+      description(description), suffix(suffix) {}
+
+    const QString description;
+    const QString suffix;
+  };
 
   OutputFormats formatIdxToFormat(const int idx);
   QString toCSVLine(double time, double value);
   ReturnCode writeToFile(QFile& file, const DataWriterBackend* writer, const std::pair<QVariantList, std::vector<QVariantList>>& data);
 
-  static const OutputFormat SUPPORTED_FORMATS[];
+  static const std::vector<OutputFormat> SUPPORTED_FORMATS;
 
 signals:
   void formatChanged(const int idx);