Exemplo n.º 1
0
bool PatchTree::UpdateTree(PatchTree **tree) {
	PatchTree **child = tree;
	bool getdelete = false;
	char *line = strcpy(new char[MAX_LINE], "");
	if(Parent) line[strlen(strcat(line, Parent))-1]!='/'?strcat(line, "/"):strlen(line)==1?strcpy(line, ""):line;
	strcat(line, Section);
	char *path, *start, *end;
	end = start = path = strcpy(new char[strlen(line)+1], line);
	while((end = strstr(end, "/"))||start) {	
		strncpy_s(line, MAX_LINE, start, end ? end++ - start : strlen(start));
		start = end;
		if(!(*child)) (*child) = start ? new PatchTree(line) : this;
		PatchTree **next = child;
		do {
			if(_stricmp((*next)->Section, line)==0) {// If already exists.
				if(start) child = &(*next)->Child;
				else getdelete = UpdateSection(next);// Updating current section.
				break;
			}
			next = &(*next)->Next;
		}while(*next);
		if (!(*next)) {
			(*next) = start ? new PatchTree(line) : this;
			child = &(*next)->Child;
		}
		if(!start) break;
	}
	delete[] path;
	delete[] line;
	return getdelete;
}
Exemplo n.º 2
0
int IniFile::Load(const string &filename)
{
    int err;
    string line;  // 带注释的行
    string cleanLine;  // 去掉注释后的行
    string comment;
    string rightComment;
    IniSection *currSection = NULL;  // 初始化一个字段指针

    release();

    fname_ = filename;
    std::ifstream ifs(fname_);
    if (!ifs.is_open()) {
        cout << "open file failed\n";
        return -1;
    }

    //增加默认段,即 无名段""
    currSection = new IniSection();
    currSection->name = "";
    sections_vt.push_back(currSection);

    // 每次读取一行内容到line
    while (std::getline(ifs, line)) {
        trim(line);

        // step 0,空行处理,如果长度为0,说明是空行,添加到comment,当作是注释的一部分
        if (line.length() <= 0) {
            comment += delim;
            continue;
        }

        // step 1
        // 如果行首不是注释,查找行尾是否存在注释
        // 如果该行以注释开头,添加到comment,跳过当前循环,continue
        if (IsCommentLine(line)) {
            comment += line + delim;
            continue;
        }

        // 如果行首不是注释,查找行尾是否存在注释,若存在,切割该行,将注释内容添加到rightComment
        split(line, commentHead, &cleanLine, &rightComment);


        // step 2,判断line内容是否为段或键
        //段开头查找 [
        if (cleanLine[0] == '[') {
            err = UpdateSection(cleanLine, comment, rightComment, &currSection);
        } else {
            // 如果该行是键值,添加到section段的items容器
             err = AddKeyValuePair(cleanLine, comment, rightComment, currSection);
        }

        if (err != 0) {
            ifs.close();
            return err;
        }

        // comment清零
        comment = "";
        rightComment = "";
    }

    ifs.close();

    return 0;
}