void Fold_Doc(unsigned int startPos, int length, int initStyle, WordList *[], Accessor &styler) { bool foldComment = styler.GetPropertyInt("fold.comment") != 0; bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0; unsigned int endPos = startPos + length; int visibleChars = 0; int lineCurrent = styler.GetLine(startPos); int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK; int levelCurrent = levelPrev; char chNext = styler[startPos]; int styleNext = styler.StyleAt(startPos); for (unsigned int i = startPos; i < endPos; i++) { char ch = chNext; chNext = styler.SafeGetCharAt(i + 1); int style = styleNext; styleNext = styler.StyleAt(i + 1); bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); // Comment folding if (foldComment && atEOL && IsCommentLine(lineCurrent, styler)) { if (!IsCommentLine(lineCurrent - 1, styler) && IsCommentLine(lineCurrent + 1, styler)) levelCurrent++; else if (IsCommentLine(lineCurrent - 1, styler) && !IsCommentLine(lineCurrent+1, styler)) levelCurrent--; } if (style == sID::OPERATOR) { if ( ch == '<' && chNext != '/' ) { levelCurrent++; } else if (ch == '<' && chNext == '/') { levelCurrent--; } } if (atEOL) { int lev = levelPrev; if (visibleChars == 0 && foldCompact) lev |= SC_FOLDLEVELWHITEFLAG; if ((levelCurrent > levelPrev) && (visibleChars > 0)) lev |= SC_FOLDLEVELHEADERFLAG; if (lev != styler.LevelAt(lineCurrent)) { styler.SetLevel(lineCurrent, lev); } lineCurrent++; levelPrev = levelCurrent; visibleChars = 0; } if (!isspacechar(ch)) visibleChars++; } // Fill in the real level of the next line, keeping the current flags as they will be filled in later int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK; styler.SetLevel(lineCurrent, levelPrev | flagsNext); }
/* main */ int main(int argc, char *argv[]) { FILE *fp; char *filename = argv[1]; char readline[N] = { '\0' }; char buf[4]; int comment_count = 0; int all_count = 0; int space_count = 0; /* ファイルのオープン */ if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "%sのオープンに失敗しました.\n", filename); system("pause"); exit(EXIT_FAILURE); } else { if (fread(buf, 1, 3, fp) == 3){ // BOMチェック if (memcmp(buf, "\xef\xbb\xbf", 3) != 0){ rewind(fp); } } } /* ファイルの終端まで文字を読み取る */ while (fgets(readline, N, fp) != NULL) { all_count++; if (IsStartCommentLine(readline) == 1){ comment_count++; while (IsEndCommentLine(readline) == 0){ if (fgets(readline, N, fp) == NULL){ break; } all_count++; comment_count++; } }else if (IsCommentLine(readline) == 1){ comment_count++; }else if (IsSpaceLine(readline) == 1){ space_count++; } } /* ファイルのクローズ */ fclose(fp); printf("全体の行数は%d行でした。\n", all_count); printf("コメント行の行数は%d行でした。\n", comment_count); printf("ステップ数は全部で%d行でした。\n", (all_count - (comment_count + space_count))); system("pause"); return 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; }