你的位置:首页 > 操作系统

[操作系统]刚刚写的一个lua下解释csv的工具。

 

csvtool = {}function csvtool:csv2table(filename)  if type(filename) ~= "string" or filename == "" then    print("the filename is not a string or a empty string")    return nil  end  local filepath = cc.FileUtils:getInstance():fullPathForFilename(filename)  local filedate = cc.FileUtils:getInstance():getStringFromFile(filepath)  print(filedate)  local dataByLine = Split(filedate,'\n')  local titleArray = Split(dataByLine[1],',')  local lineIndex = 1  local titleSize = table.getn(titleArray)  local returnTable = {}  for line = 2,table.getn(dataByLine)-1 do    local dataByLien_split = Split(dataByLine[line],',')    returnTable[lineIndex] = {}    for i = 1,titleSize do      returnTable[lineIndex][titleArray[i]] = dataByLien_split[i]    end    lineIndex = lineIndex+1  end  return returnTableendfunction csvtool:returnCsvTableTitle(csvtable)  local titleTable = {}  local titleIndex = 1  for key, var in pairs(csvtable[1]) do    titleTable[titleIndex] = key    titleIndex = titleIndex + 1  end  return titleTableendfunction csvtool:returnScvTitleIndex(csvtable,findtitlename)  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 then    print("the argment 1 is not a csvtable or is a empty table")    return nil  end  if findtitlename == "" or type(findtitlename) ~= "string" then    print("the findtitlename is a empty string or the type not a string")    return nil  end  local csvtitle = csvtool:returnCsvTableTitle(csvtable)  for key, var in pairs(csvtitle) do        if var == findtitlename then      return key    end  end  return nilendfunction csvtool:findLineByKeyAndValue(csvtable,key,value)  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 then    print("the argment 1 is not a csvtable or is a empty table")    return nil  end  if type(key) ~= "string" or key == "" then    print("the argment 2 is not a string or is a empty string")    return nil  end  if csvtable[1][key] == nil then    print("the key is not found in csvtable")    return nil  end  local linereturn = {}  value = tostring(value)  local isFound = false    for k, v in pairs(csvtable) do    if v[key] == value then      isFound = true      table.insert(linereturn,v)    end  end  if isFound then    return linereturn    else    print("not found this key-value item")    return nil  endendfunction csvtool:findoneLineByKeyAndValue(csvtable,key,value)  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 then    print("the argment 1 is not a csvtable or is a empty table")    return nil  end  if type(key) ~= "string" or key == "" then    print("the argment 2 is not a string or is a empty string")    return nil  end  if csvtable[1][key] == nil then    print("the key is not found in csvtable")    return nil   end  local linereturn = {}  value = tostring(value)  for k, v in pairs(csvtable) do    if v[key] == value then      return v    end  end  print("not found this key-value item")  return nilendfunction csvtool:findValueByOneKeyAndValue(csvtable,findkey,anotherkey,anothervalue) --find a value by another key-value  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 then    print("the argment 1 is not a csvtable or is a empty table")    return nil  end  if type(findkey) ~= "string" or findkey == "" then    print("the argment 2 is not a string or is a empty string")    return nil  end  if csvtable[1][findkey] == nil then    print("the find key is not found in csvtable")    return nil  end  if csvtable[1][anotherkey] == nil then    print("the another key is not found in csvtable")    return nil  end  local finditem = self:findoneLineByKeyAndValue(csvtable,anotherkey,anothervalue)  local foundvalue = finditem[findkey]  return foundvalue --stringendfunction csvtool:returnOneRowByKey(csvtable, rowkey)  if type(csvtable) ~= "table" or table.getn(csvtable) == 1 then    print("the argment 1 is not a table or a empty table")    return nil  end  if type(rowkey) ~= "string" or rowkey == "" then    print("the argment 2 is not a string or is a empty string")    return nil  end  if csvtable[1][rowkey] == nil then    print("the table is not have the rowkey row")    return nil  end  local returnRow = {}  local returnRowIndex = 1  for k, v in pairs(csvtable) do    returnRow[returnRowIndex] = v[rowkey]    returnRowIndex = returnRowIndex + 1  end  return returnRowendfunction csvtool:getcsvsize(filename)  local csvtable = self:csv2table(filename)  local size = table.getn(csvtable)  return sizeendfunction Split(szFullString, szSeparator)   local nFindStartIndex = 1   local nSplitIndex = 1   local nSplitArray = {}   while true do     local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)     if not nFindLastIndex then    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))     break   end   nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)   nFindStartIndex = nFindLastIndex + string.len(szSeparator)   nSplitIndex = nSplitIndex + 1   end   return nSplitArray end