第四個工具類:csvreader
csvreader是用來支援讀取csv**用的
csv格式其實就是乙個有固定格式的txt,一行每一列用英文','隔開
遊戲專案中,很多遊戲靜態表。策劃提供的是csv**,可以用excel編輯方便。客戶端unity也傾向於讀取csv**,純文字,格式簡單,讀取方便
然後為了讀取更方便,我們人為規定了csv前3行的內容有特殊意義,下圖是乙個示例
第一行的每一列為列名稱
第二行的每一列為列型別
第三行的每一列為列名稱中文說明
第四行開始為實際資料
具體說明一下支援的列型別
4個基本型別:
u:對應unsigned int
i:對應int
f:對應float
s:對應std::string
子結構:如果該列是乙個子結構,則具體寫明子結構的欄位名和字段型別,用英文分號';'分割
如上圖中的structid;u;structnum;i;structfloat;f;strucename;s:
表示這個子結構有4個字段:structid,unsigned int型別,structnum,int型別,structfloat,float型別,strucename,字串型別
實際資料中子結構字段之間用英文與運算子『&』分割
如上圖中的1&-2&3.4&aaa:
表示structid=1,structnum=-2,structfloat=3.4f,strucename=aaa,即
列型別的限定型別:
k:表示該列是主鍵,作為索引使用
支援最多3列主鍵,即3列組合起來確定唯一一行記錄
主鍵不一定放在前3列,可以用中間的某些列做主鍵
主鍵的型別限定為整數,即k後面的列型別為i或者u
lst:表示該列為不定長陣列
如上列中的lst:u:
表示該列是乙個unsigned int的陣列,即對應std::vector
如上例中的lst:structid;u;structnum;i;structfloat;f;strucename;s:
表示該列是乙個子結構的陣列
實際資料中陣列的元素和元素之間用英文分號';'分割
如上例中的1&-2&3.4&aaa;5&-6&7.8&bbb:
表示該子結構陣列有2個元素,第乙個元素是,第二個元素是
k或者lst後面跟英文冒號「:」,再跟具體列型別
以上為人為規定
那麼對於這樣的一張表,讀進記憶體對應的結構可能如下所示:
class struct
;class test
;
本文的csvreader就是為了實現將如上人為規定了格式的csv檔案,讀取成對應的類物件的乙個工具類
(因為策劃對錶結構的修改是很頻繁的,表結構一旦修改,對應的類結構就要跟著修改,乾脆自動生成省事)
csvreader提供這樣幾個介面:
1、readline,讀取一行內容,讀到乙個快取中,按csv格式保留的分隔符英文逗號',',分割成每一列的值
這裡有乙個檔案編碼的問題,即在windows下,utf8格式的檔案有個bom頭,即檔案前3個位元組是0xef,0xbb,0xbf需要過濾掉,行末尾的'\r',『\n',需要過濾掉
2、checkline,校驗一行內容按逗號分隔之後的列數,是否和第一行的列名稱的列數一致
如果不一致,說明實際資料中,尤其是字串型別的列中,存在英文的逗號,但是英文逗號是csv檔案保留的分隔符
所以如果實際資料中出現英文逗號,就無法確定哪一列對應哪一列了
3、name2index,根據列名稱,返回該列是第幾列,首列為0
4、getvalue,讀取單字段型別(非陣列)的列的值
5、getvaluelist,讀取陣列列的值
6、loadfile,載入指定檔案,並處理前3行內容,識別出列名稱,以及列數量
對於子結構,需要子結構過載賦值運算子,引數為乙個string,如
***& operator = (const std::string &other),實現類似上例中「1&-2&3.4&aaa」這樣乙個字串的解析
這裡也用到了之前提到的stringtool,用來進行字串的分割,以及校驗是否是乙個合法的數字
上**csvreader.h
#ifndef __csvreader_h__
#define __csvreader_h__
#include #include #include #include #include "stringtool.h"
namespace common;
template bool csvreader::getstructlist(size_t index, std::vector& values, const std::string& split)
}return true;
}else
}template bool csvreader::getstruct(size_t index, t& value)
else
} }}
#endif
csvreader.cpp
#include "csvreader.h"
#include namespace common
csvreader::~csvreader()
}size_t csvreader::readline(bool firstline)}}
// 去除每行末尾\r\n
while (1 <= len)
else
}if (0 < len)
return len;
}else
}const std::vector& csvreader::getline()
bool csvreader::checkline()
else
}bool csvreader::openfile(const std::string& file_path_name)
else
}bool csvreader::loadfile(const std::string& file_path_name)
// 讀列型別
readline();
// 讀注釋
readline();
return true;
}else
}size_t csvreader::name2index(const std::string& name)
else
}bool csvreader::getvaluelist(size_t index, std::vector& values, const std::string& split)
return true;
}else
}bool csvreader::getvalue(size_t index, std::string& value)
else
}bool csvreader::getvaluelist(size_t index, std::vector& values, const std::string& split)
return true;
}else
}bool csvreader::getvalue(size_t index, unsigned int& value)
else
}else
}bool csvreader::getvaluelist(size_t index, std::vector& values, const std::string& split)
return true;
}else
}bool csvreader::getvalue(size_t index, int& value)
else
}else
}bool csvreader::getvaluelist(size_t index, std::vector& values, const std::string& split)
return true;
}else
}bool csvreader::getvalue(size_t index, float& value)
else
}else}}}
實用的工具類庫
git位址 使用說明一 使用說明二 把json字串轉換成list list maps gsonutils.fromjson objectmap.get img url tostring gsonutils.gettype object.class 同理把json字串轉換成list,只需要改變返回型別...
集合之Collections集合工具類(四)
操作collection以及map介面的工具類 reverse list 反轉list中元素的順序 shuffle list 對list集合元素進行隨機排序 sort list 根據元素的自然順序對指定list集合元素按照公升序排序 sort list,comparator 根據指定的compara...
四 執行緒的併發工具類
countdownlatch是什麼?countdownlatch,英文翻譯為倒計時鎖存器,是乙個同步輔助類,在完成一組正在其他執行緒中執行的操作之前,它允許乙個或多個執行緒一直等待。閉鎖可以延遲線程的進度直到其到達終止狀態,閉鎖可以用來確保某些活動直到其他活動都完成才繼續執行 countdownla...