annotate Source/ParseCSV.cpp @ 15:585caf503ef5 tip

Tidy up for ROLI
author Geogaddi\David <d.m.ronan@qmul.ac.uk>
date Tue, 17 May 2016 18:50:19 +0100
parents 262e084a15a9
children
rev   line source
d@0 1 /*
d@0 2 ==============================================================================
d@0 3
d@0 4 ParseCSV.cpp
d@0 5 Created: 27 Aug 2014 3:18:54pm
d@0 6 Author: david.ronan
d@0 7
d@0 8 ==============================================================================
d@0 9 */
d@0 10
d@0 11 #include "ParseCSV.h"
d@0 12 #include "AudioFileData.h"
d@0 13
d@0 14 #include <fstream>
d@0 15 #include <string>
d@0 16 #include <vector>
d@0 17
d@0 18 ParseCSV::ParseCSV()
d@0 19 {
d@0 20
d@0 21 };
d@0 22
d@0 23 ParseCSV::~ParseCSV()
d@0 24 {
d@1 25
d@0 26 };
d@0 27
d@0 28 AudioFileData ParseCSV::Parse(std::string fileName)
d@0 29 {
d@0 30 //open the file from which to read the data
d@1 31 std::ifstream myFile;// = std::ifstream();
d@0 32
d@0 33 myFile.open(fileName, std::ifstream::in);
d@0 34
d@0 35 std::vector<std::string> data = ReadLines(myFile);
d@0 36
d@0 37 std::vector<std::string> fileNames = std::vector<std::string>();
d@0 38 std::vector<std::string> labels = std::vector<std::string>();
d@0 39
d@0 40 for (size_t i = 0; i < data.size(); i++)
d@0 41 {
d@0 42 std::string s = data[i];
d@0 43 std::string delimiter = ",";
d@0 44
d@0 45 std::string newfile = "";
d@0 46 std::string newlabel = "";
d@0 47
d@0 48 size_t pos = 0;
d@0 49 int idx = 1;
d@0 50 std::string token = "";
d@0 51
d@0 52 while ((pos = s.find(delimiter)) != std::string::npos)
d@0 53 {
d@0 54 token = s.substr(0, pos);
d@0 55
d@0 56 switch (idx)
d@0 57 {
d@0 58 case 1:
d@0 59 newfile = token;
d@0 60 idx++;
d@0 61 break;
d@0 62 default:
d@0 63 break;
d@0 64 }
d@0 65
d@0 66 s.erase(0, pos + delimiter.length());
d@0 67 newlabel = s;
d@0 68 }
d@0 69
d@0 70 fileNames.push_back(newfile);
d@1 71 labels.push_back(newlabel);
d@0 72 }
d@0 73
d@0 74 m_AudioFileData = AudioFileData(fileNames, labels);
d@0 75
d@0 76 myFile.close();
d@0 77
d@0 78 return m_AudioFileData;
d@0 79 }
d@0 80
d@0 81 std::vector<std::string> ParseCSV::ReadLines(std::ifstream& is)
d@0 82 {
d@0 83 std::vector<std::string> lines = std::vector<std::string>();
d@0 84 std::string line = "";
d@0 85
d@0 86 while (std::getline(is, line))
d@0 87 {
d@0 88 lines.push_back(line);
d@0 89 }
d@0 90
d@0 91 return lines;
d@9 92 }