Guest User

the pokemon machines csv eater

a guest
Dec 26th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. public string GetValueFromCSVStore(string csvfilename, string columnname, string additionalkeys)
  2.         {
  3.             int i, j, k;
  4.             int columnid = -1;
  5.             storedcsv tempcsv;
  6.             string[] columnnames;
  7.             string[] additionalkeybuffer;
  8.             string[] additionalkeyvaluebuffer;
  9.             string[] buffer;
  10.             char[] delimiterChars = { ',' };
  11.             char[] keysdelimiterChars = { '|' };
  12.             bool additionalkeycheck = false;
  13.             bool ignorethisline = false;
  14.             List<csvsearchkey> searchkeys = new List<csvsearchkey>();
  15.             csvsearchkey tempsearchkey = new csvsearchkey();
  16.             //load the right file
  17.             tempcsv = thejerkstore.Find(delegate(storedcsv searchcsv) { return searchcsv.myname == csvfilename; });
  18.             //find the column names, we find the column needed for this get
  19.             columnnames = tempcsv.Lines[0].Split(delimiterChars);
  20.             for (i = 0; i < columnnames.Length; i++)
  21.             {
  22.                 if (columnnames[i] == columnname)
  23.                 {
  24.                     columnid = i;
  25.                 }
  26.                
  27.             }
  28.             //we parse the additonal keys, with 2 splits
  29.             additionalkeybuffer = additionalkeys.Split(delimiterChars);
  30.             for (i = 0; i < additionalkeybuffer.Length; i++)
  31.             {
  32.                 additionalkeyvaluebuffer = additionalkeybuffer[i].Split(keysdelimiterChars);
  33.                 if (additionalkeyvaluebuffer.Length == 2)
  34.                 {
  35.                     tempsearchkey.columnname = additionalkeyvaluebuffer[0];
  36.                     tempsearchkey.val = additionalkeyvaluebuffer[1];
  37.                     //once we know the column/value pair to check we need to column for that get
  38.                     for (j = 0; j < columnnames.Length; j++)
  39.                     {
  40.                         if (tempsearchkey.columnname == columnnames[j])
  41.                         {
  42.                             tempsearchkey.columnid = j;
  43.                         }
  44.                     }
  45.                     searchkeys.Add(tempsearchkey);
  46.                 }
  47.             }
  48.  
  49.             //now we read every line of the file
  50.             for (i = 0; i < tempcsv.Lines.Length; i++)
  51.             {
  52.                 ignorethisline = false;
  53.                 buffer = tempcsv.Lines[i].Split(delimiterChars);
  54.                 for (j = 0; j < buffer.Length; j++)
  55.                 {
  56.                     //and check if against the additonal keys
  57.                     for (k = 0; k < searchkeys.Count; k++)
  58.                     {
  59.                         //if the value matches the condition is met
  60.                         if (buffer.Length > columnid)
  61.                         {
  62.                             if (buffer[searchkeys[k].columnid] == searchkeys[k].val)
  63.                             {
  64.                                 additionalkeycheck = true;
  65.                             }
  66.                             else
  67.                             {
  68.                                 //if it isn't, and we had a match before, then this is NOT the right line, move on.
  69.                                 if (additionalkeycheck == true)
  70.                                 {
  71.                                     ignorethisline = true;
  72.                                 }
  73.                             }
  74.                         }
  75.                         else
  76.                         {
  77.                             ignorethisline = true;
  78.                         }
  79.                     }
  80.                     //this is it.
  81.                     if ((ignorethisline == false) && (additionalkeycheck == true))
  82.                     {
  83.                         if (columnid != -1)
  84.                         {
  85.                             return buffer[columnid];
  86.                         }
  87.                     }
  88.                 }    
  89.             }  
  90.             return "";
  91.         }
Advertisement
Add Comment
Please, Sign In to add comment