Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string GetValueFromCSVStore(string csvfilename, string columnname, string additionalkeys)
- {
- int i, j, k;
- int columnid = -1;
- storedcsv tempcsv;
- string[] columnnames;
- string[] additionalkeybuffer;
- string[] additionalkeyvaluebuffer;
- string[] buffer;
- char[] delimiterChars = { ',' };
- char[] keysdelimiterChars = { '|' };
- bool additionalkeycheck = false;
- bool ignorethisline = false;
- List<csvsearchkey> searchkeys = new List<csvsearchkey>();
- csvsearchkey tempsearchkey = new csvsearchkey();
- //load the right file
- tempcsv = thejerkstore.Find(delegate(storedcsv searchcsv) { return searchcsv.myname == csvfilename; });
- //find the column names, we find the column needed for this get
- columnnames = tempcsv.Lines[0].Split(delimiterChars);
- for (i = 0; i < columnnames.Length; i++)
- {
- if (columnnames[i] == columnname)
- {
- columnid = i;
- }
- }
- //we parse the additonal keys, with 2 splits
- additionalkeybuffer = additionalkeys.Split(delimiterChars);
- for (i = 0; i < additionalkeybuffer.Length; i++)
- {
- additionalkeyvaluebuffer = additionalkeybuffer[i].Split(keysdelimiterChars);
- if (additionalkeyvaluebuffer.Length == 2)
- {
- tempsearchkey.columnname = additionalkeyvaluebuffer[0];
- tempsearchkey.val = additionalkeyvaluebuffer[1];
- //once we know the column/value pair to check we need to column for that get
- for (j = 0; j < columnnames.Length; j++)
- {
- if (tempsearchkey.columnname == columnnames[j])
- {
- tempsearchkey.columnid = j;
- }
- }
- searchkeys.Add(tempsearchkey);
- }
- }
- //now we read every line of the file
- for (i = 0; i < tempcsv.Lines.Length; i++)
- {
- ignorethisline = false;
- buffer = tempcsv.Lines[i].Split(delimiterChars);
- for (j = 0; j < buffer.Length; j++)
- {
- //and check if against the additonal keys
- for (k = 0; k < searchkeys.Count; k++)
- {
- //if the value matches the condition is met
- if (buffer.Length > columnid)
- {
- if (buffer[searchkeys[k].columnid] == searchkeys[k].val)
- {
- additionalkeycheck = true;
- }
- else
- {
- //if it isn't, and we had a match before, then this is NOT the right line, move on.
- if (additionalkeycheck == true)
- {
- ignorethisline = true;
- }
- }
- }
- else
- {
- ignorethisline = true;
- }
- }
- //this is it.
- if ((ignorethisline == false) && (additionalkeycheck == true))
- {
- if (columnid != -1)
- {
- return buffer[columnid];
- }
- }
- }
- }
- return "";
- }
Advertisement
Add Comment
Please, Sign In to add comment