areadcsv.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "areadcsv.h"
  19. /*!
  20. * \brief aReadCSV reads from a CSV file
  21. * \param filename - QString to csv file.
  22. * \return QVector<QStringList> of the CSV data, where each QStringList is one column of the input file
  23. */
  24. QVector<QStringList> aReadCsv(QString filename)
  25. {
  26. QFile csvfile(filename);
  27. csvfile.open(QIODevice::ReadOnly);
  28. QTextStream stream(&csvfile);
  29. QVector<QStringList> values;
  30. //Read CSV headers and create QStringLists accordingly
  31. QString line = stream.readLine();
  32. auto items = line.split(",");
  33. for (int i = 0; i < items.length(); i++) {
  34. QStringList list;
  35. list.append(items[i]);
  36. values.append(list);
  37. }
  38. //Fill QStringLists with data
  39. while (!stream.atEnd()) {
  40. QString line = stream.readLine();
  41. auto items = line.split(",");
  42. for (int i = 0; i < values.length(); i++) {
  43. values[i].append(items[i]);
  44. }
  45. }
  46. return values;
  47. }