a8/a8/stringlist.cc
aozhiwei ec92ae9234 1
2019-03-26 16:36:42 +08:00

89 lines
1.9 KiB
C++

#include <a8/a8.h>
#include <a8/stringlist.h>
namespace a8
{
StringList::StringList()
{
}
StringList::~StringList()
{
stringitem_list.clear();
}
bool StringList::LoadFromFile(const char* szFile)
{
stringitem_list.clear();
FILE *fp = fopen(szFile, "rb");
if(fp){
fseek(fp, 0, SEEK_END);
int fileSize = ftell(fp);
if(fileSize){
char *p = (char*)malloc(fileSize + 1);
if(p){
*(p + fileSize) = '\0';
fseek(fp, 0, SEEK_SET);
fread(p, 1, fileSize, fp);
SetText(p);
free(p);
}
}
fclose(fp);
return true;
}else{
return false;
}
}
void StringList::Add(const std::string& str)
{
stringitem_list.push_back(str);
}
int StringList::Count()
{
return stringitem_list.size();
}
std::string StringList::String(unsigned int idx)
{
if(idx >=0 && idx < stringitem_list.size())
return stringitem_list[idx];
return "";
}
void StringList::SetText(const char* szText)
{
stringitem_list.clear();
const char *p1 = szText;
const char *p2 = szText;
while(*p2){
if(*p2 == '\r' || *p2 == '\n'){
Add(p2 > p1 ? std::string(p1, p2 - p1) : "\r\n");
if (*p2 == '\r' && *(p2 + 1) == '\n') {
p2++;
}
p1 = p2 + 1;
}
p2++;
}
#if 0
if (p2 > p1) {
Add(p2 > p1 ? std::string(p1, p2 - p1) : "");
}
#endif
}
std::string StringList::Text()
{
std::string text;
for (auto& str : stringitem_list) {
text += str + "\r";
}
return text;
}
}