0

I have a csv file and some of the text in it is as follows:

sendLink, 23, VL_name1, 0.5, 0.5
PATH, (device_1, 1, SW1, 10),\
      (SW_2, 23, SW_1, 23),\
      (SW_1, 9, device_2, 1)
PATH, (device_3, 2, SW_12, 10),\
      (SW_12, 23, SW_11, 23),\
      (SW_11, 9, device_2, 2)

sendLink, 24, VL_name2, 0.5, 0.5
PATH, (device_4, 1, SW_09, 24),\
      (SW_01, 9, device_2, 1)
PATH, (device_5, 2, SW_19, 24),\
      (SW_11, 9, device_2, 2)

sendLink, 25, VL_name3, 0.5, 0.5
PATH, (device_7, 1, SW_09, 24),\
      (SW_09, 17, SW_01, 24),\
      (SW_01, 9, device_2, 1)
PATH, (device_8, 2, SW_19, 24),\
      (SW_19, 17, SW_11, 24),\
      (SW_11, 9, device_2, 2)

I am using Qt and have fetched all the data from the csv into a QStringList where each element of QStringList is one line from the csv.

I have defined a struct like below:

typedef struct{
    QString outputESResource;
    QString inputESResource;
} Path_t;

and now my intention is to scan though the QString list and for every line that starts with sendLink, I should read the immediate next lines that start with PATH, save the second element(device_1, device_3) into outputESResource and then look in the lines after the PATH line and fetch the before last column (device_2) in this example and save it into inputESResource.

For this, I am searching through the stringList for lines starting with PATH as follows:

for(int i=0; i < fileContents.count(); i++)
{
  if(fileContents[i].startsWith("PATH", Qt::CaseSensitive) )
   {
   //scan next element in stringlist
   }
 }

I am not sure if this is the right way to implement this. Could someone please advice on it.

smyslov
  • 1,279
  • 1
  • 8
  • 29
  • Possible duplicate of [Parsing through a csv file in Qt](http://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-qt) – James Turner Jun 08 '16 at 13:28

1 Answers1

1

From what I understand... here is some pseudo code that may help you. There is nothing wrong with your idea I think, not sure how you are reading from file - but there is no real need to use QString immediately - usually QIODevice::readLine returns a QByteArray which are just as good as QString for this sort of thing...

Path_t pathInfo;

while (!file.atEnd()) {
    QByteArray line = file.readLine().trimmed(); // remove white space at ends
    if (line.startsWith("sendLink"))
    {
        // do sendLink stuff...
        // maybe you need to assign a new pathInfo...?
    }
    else if (line.startsWith("PATH")
    {
        // now get the rest of the PATH line. This should keep reading
        // and appending lines until there is no '\' at the end.
        while (line.endsWith("\"))
        {
            // remove the "\" from the end
            line.chop(1);
            // Read the next line (trim white space)
            line.append(file.readLine().trimmed());
        }
        // assuming you don't want the brackets?
        line.remove('(');
        line.remove(')');
        // split the line
        QList<QByteArray> words = line.split(',');
        // populate your struct - assuming that the positions are always 0 and 10... 
        // otherwise you will have to parse this bit too
        pathInfo.outputESResource = words[0].toLatin1(); // convert to QString for your struct
        pathInfo.inputESResource = words[10].toLatin1(); // convert to QString for your struct

        // do something with pathInfo....
    }
}

Note: Not built or tested - don't have your orig compliable version. Also there are too many unknowns for what you want to do with this info so I have not attempted to store pathInfo anywhere...

Edit

I have added a little loop to append the PATH line all together and remove white space and '/'s - as well as the same code that removes '(' and ')'...

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • There's is still one catch here. Each line from the csv is saved as a separate string which is why I cannot assume that the positions are 0 and 10. I am sure about the device_4,device_7 etc since they are in the same string as PATH. But device_2 is not the same string and I am supposed to fetch this value. I am unsure how to go about with this problem – smyslov Jun 08 '16 at 14:54
  • @smyslov ok, I have added a little bit more. Firstly all the reads from the file now trims the white space away from the ends of each line - this is always quite useful. Also I added a little loop to re-construct the PATH into one line with '\', '(' and ')' all removed... Now your format should be correct. But you don't need to use [0] and [10], you can use further functions to find where each device is. I simply don't understand your rules precisely so I have made a guess, but look at: `indexOf()` and `lastIndexOf()` to get where the first and last device are or just loop through your vector – code_fodder Jun 08 '16 at 18:27