TSuperSouceCountWin.h
- //---------------------------------------------------------------------------
- #ifndef TSuperSouceCountWinH
- #define TSuperSouceCountWinH
- //---------------------------------------------------------------------------
- #include <Classes.hpp>
- #include <Controls.hpp>
- #include <StdCtrls.hpp>
- #include <Forms.hpp>
- #include "SUIForm.hpp"
- #include <ExtCtrls.hpp>
- #include "SUIGroupBox.hpp"
- #include "SUIImagePanel.hpp"
- #include "TFlatSplitterUnit.hpp"
- #include "TFlatPanelUnit.hpp"
- #include "SUIButton.hpp"
- #include "SUIComboBox.hpp"
- #include "SUIListBox.hpp"
- #include "SUIPageControl.hpp"
- #include "SUITabControl.hpp"
- #include <FileCtrl.hpp>
- #include "SUIEdit.hpp"
- #include "SUIListView.hpp"
- #include <ComCtrls.hpp>
- #include "SUIMemo.hpp"
- #include "Utilities.h"
- //---------------------------------------------------------------------------
- #define MAXCHARS 20
- //---------------------------------------------------------------------------
- //配置情報
- typedef struct _signinfo {
- char * type;
- char * blockstart;
- char * blockend;
- char * linesign;
- char * escape;
- char * quote;
- } SignInfo;
- //---------------------------------------------------------------------------
- //ファイル情報
- typedef struct _lines {
- char * type;
- AnsiString filename;
- ULong phylines;
- ULong logicallines;
- ULong commentlines;
- } Lines;
- //---------------------------------------------------------------------------
- class TSuperSouceCountWin : public TForm
- {
- __published: // IDE-managed Components
- TsuiForm *suiForm1;
- TFlatPanel *FlatPanel1;
- TFlatSplitter *FlatSplitter1;
- TFlatPanel *FlatPanel2;
- TsuiPageControl *suiPageControl1;
- TsuiTabSheet *suiTabSheet1;
- TsuiTabSheet *suiTabSheet2;
- TsuiDriveComboBox *suiDriveComboBox1;
- TsuiDirectoryListBox *suiDirectoryListBox1;
- TsuiButton *btnFolderCount;
- TsuiEdit *edFile;
- TsuiListView *suiListView1;
- TsuiButton *btnDelete;
- TsuiButton *btnDeleteAll;
- TsuiButton *btnSelectFile;
- TsuiButton *btnadd;
- TsuiButton *btnReplace;
- TsuiGroupBox *suiGroupBox1;
- TsuiButton *btnFileCount;
- TLabel *Label1;
- TLabel *lbFileTotalCount;
- TLabel *Label2;
- TLabel *lbFolderTotalCount;
- TsuiMemo *memoResult;
- TLabel *Label3;
- TsuiEdit *edNO;
- TStatusBar *StatusBar1;
- TsuiCheckBox *cbSubFolder;
- void __fastcall btnFolderCountClick(TObject *Sender);
- void __fastcall btnaddClick(TObject *Sender);
- void __fastcall btnFileCountClick(TObject *Sender);
- private: // User declarations
- AnsiString BugNO;
- SignInfo sign;
- Lines total;
- void __fastcall DoFolder(AnsiString folder);
- int __fastcall ReadSignDB(ULong hash, SignInfo * sign);
- int __fastcall CountLines(char * filename, SignInfo sign, Lines * lines);
- void __fastcall WriteResults(Lines result,bool total = false);
- public: // User declarations
- __fastcall TSuperSouceCountWin(TComponent* Owner);
- };
- //---------------------------------------------------------------------------
- extern PACKAGE TSuperSouceCountWin *SuperSouceCountWin;
- //---------------------------------------------------------------------------
- #endif
TSuperSouceCountWin.cpp
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "TSuperSouceCountWin.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "SUIForm"
- #pragma link "SUIImagePanel"
- #pragma link "SUIGroupBox"
- #pragma link "SUIImagePanel"
- #pragma link "TFlatSplitterUnit"
- #pragma link "TFlatPanelUnit"
- #pragma link "SUIButton"
- #pragma link "SUIComboBox"
- #pragma link "SUIListBox"
- #pragma link "SUIPageControl"
- #pragma link "SUITabControl"
- #pragma link "SUIEdit"
- #pragma link "SUIListView"
- #pragma link "SUIMemo"
- #pragma resource "*.dfm"
- //---------------------------------------------------------------------------
- const char * Exp = "--------------------------------------------";
- TSuperSouceCountWin *SuperSouceCountWin;
- //---------------------------------------------------------------------------
- __fastcall TSuperSouceCountWin::TSuperSouceCountWin(TComponent* Owner)
- : TForm(Owner)
- {
- ReadSignDB(MyHash(NULL), &sign);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSuperSouceCountWin::btnFolderCountClick(TObject *Sender)
- {
- memoResult->Text = "";
- BugNO = edNO->Text;
- total.phylines = 0;
- total.logicallines = 0;
- total.commentlines = 0;
- DoFolder(suiDirectoryListBox1->Directory);
- WriteResults(total,true);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSuperSouceCountWin::DoFolder(AnsiString folder)
- {
- TSearchRec sr;
- if (FindFirst(folder+"//*.*", faAnyFile, sr) == 0){
- do{
- Application->ProcessMessages();
- if(sr.Name!="." && sr.Name!=".."){
- if( cbSubFolder->Checked && (sr.Attr & faDirectory) == faDirectory ){
- DoFolder(folder+"//"+sr.Name);
- }else{
- Lines lines;
- CountLines((folder+"//"+sr.Name).c_str(), sign, &lines);
- WriteResults(lines);
- total.phylines += lines.phylines;
- total.logicallines += lines.logicallines;
- total.commentlines += lines.commentlines;
- }
- }
- } while (FindNext(sr) == 0);
- FindClose(sr);
- }
- }
- //---------------------------------------------------------------------------
- int __fastcall TSuperSouceCountWin::ReadSignDB(ULong hashcode, SignInfo * sign)
- {
- FILE * fp;
- char value[MAXCHARS + 1] = "/0";
- int token = '=';
- if (!sign) {
- return 1;
- }
- fp = fopen("sign.db", "r");
- if (!fp) {
- return 1;
- }
- fseek(fp, 0, SEEK_SET);
- while (fgets(value, MAXCHARS, fp)) {
- if(atoi(value) == hashcode){
- break;
- }
- }
- //fgets(value, MAXCHARS, fp);
- sign->type = new char[strlen(value) + 1];
- strcpy(sign->type, value);
- fgets(value, MAXCHARS, fp);
- ReadValue(value, token, &sign->blockstart);
- RmnewLine(sign->blockstart);
- fgets(value, MAXCHARS, fp);
- ReadValue(value, token, &sign->blockend);
- RmnewLine(sign->blockend);
- fgets(value, MAXCHARS, fp);
- ReadValue(value, token, &sign->linesign);
- RmnewLine(sign->linesign);
- fgets(value, MAXCHARS, fp);
- ReadValue(value, token, &sign->escape);
- RmnewLine(sign->escape);
- fgets(value, MAXCHARS, fp);
- ReadValue(value, token, &sign->quote);
- RmnewLine(sign->quote);
- fclose(fp);
- return 0;
- }
- //---------------------------------------------------------------------------
- int __fastcall TSuperSouceCountWin::CountLines(char * filename, SignInfo sign, Lines * lines)
- {
- FILE * fp;
- int iscomment = 0, both = 0, linecomment = 0, commentend = 0, isquote = 0;
- int c = 0;
- bool isstart = false;
- char * buffer, * newline, * tmp, * src;
- ULong nsize, nread;
- if(!lines){
- return 1;
- }
- lines->type = new char[strlen(sign.type) + 1];
- if(!lines->type){
- return 1;
- }
- strcpy(lines->type, sign.type);
- lines->filename = filename;
- lines->phylines = 0;
- lines->logicallines = 0;
- lines->commentlines = 0;
- fp = fopen(filename, "r");
- if(!fp){
- return 1;
- }
- fseek(fp, 0, SEEK_END);
- nsize = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- buffer = new char[nsize + 1];
- if(!buffer){
- return 1;
- }
- for(nread = 0; nread < nsize; nread++){
- c = fgetc(fp);
- if(c == EOF){
- break;
- }
- *(buffer + nread) = c;
- }
- *(buffer + nread) = '/0';
- src = buffer;
- newline = buffer;
- while(*buffer){
- if (!strncmp(buffer, BugNO.c_str(), BugNO.Length())) {
- if(!isstart){
- isstart = true;
- }else{
- isstart = false;
- }
- }
- if(isstart){
- if (!strncmp(buffer, sign.escape, strlen(sign.escape))) {
- buffer += strlen(sign.escape) + strlen(sign.quote) + 1;
- continue;
- }
- if (!strncmp(buffer, sign.quote, strlen(sign.quote)) && !iscomment) {
- isquote = !isquote;
- }
- if (*buffer == sign.blockstart[0] && !isquote) {
- if (!strncmp(buffer, sign.blockstart, strlen(sign.blockstart))) {
- iscomment = 1;
- commentend = 0;
- tmp = buffer;
- if (!isquote) {
- for(; tmp > newline; tmp--){
- if (!isspace(*tmp)) {
- //printf("a both:%d/n", lines->phylines + 1);
- both = 1;
- break;
- }
- }
- }
- }
- }
- if(*buffer == sign.linesign[0] && !isquote) {
- if (!strncmp(buffer, sign.linesign, strlen(sign.linesign))) {
- linecomment = 1;
- tmp = buffer;
- if(!isquote){
- for(; tmp > newline; tmp--){
- if (!isspace(*tmp)) {
- //printf("b both:%d/n", lines->phylines + 1);
- both = 1;
- break;
- }
- }
- }
- }
- }
- if(*buffer == sign.blockend[0] && !isquote){
- if (!strncmp(buffer, sign.blockend, strlen(sign.blockend))) {
- commentend = 1;
- iscomment = 0;
- linecomment = 0;
- tmp = buffer + strlen(sign.blockend);
- }
- }
- if (*buffer == '/n') {
- lines->phylines++;
- if (iscomment) {
- //printf("blockstart:%d/n", lines->phylines);
- lines->commentlines++;
- if (both) {
- lines->logicallines++;
- both = 0;
- }
- }else if(linecomment){
- //printf("line:%d/n", lines->phylines);
- linecomment = 0;
- lines->commentlines++;
- if(both){
- lines->logicallines++;
- both = 0;
- }
- } else if(commentend){
- //printf("blockend:%d/n", lines->phylines);
- commentend = 0;
- lines->commentlines++;
- if (both) {
- lines->logicallines++;
- both = 0;
- }else{
- for(; tmp < buffer; tmp++){
- if (!isspace(*tmp)) {
- //printf("c both:%d/n", lines->phylines);
- lines->logicallines++;
- break;
- }
- }
- }
- }else{
- //printf("logical:%d/n", lines->phylines);
- lines->logicallines++;
- }
- newline = buffer + 1;
- }
- }
- buffer++;
- }
- free(src);
- fclose(fp);
- return 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSuperSouceCountWin::WriteResults(Lines result , bool total)
- {
- char str[256];
- memoResult->Text = memoResult->Text + "/r/n" + Exp;
- if(!total){
- sprintf(str, "ファイル名:%s/t実際行数:%d/t論理行数:%d/tコメント行数:%d/t/n", result.filename , result.phylines, result.logicallines, result.commentlines);
- }else{
- sprintf(str, "統計:/t実際行数:%d/t論理行数:%d/tコメント行数:%d/t/n", result.phylines, result.logicallines, result.commentlines);
- lbFolderTotalCount->Caption = result.logicallines;
- }
- memoResult->Text = memoResult->Text + "/r/n" + str;
- memoResult->Perform(WM_VSCROLL,SB_BOTTOM,0);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSuperSouceCountWin::btnaddClick(TObject *Sender)
- {
- TListItem *listItem;
- listItem = suiListView1->Items->Add();
- listItem->Caption = edFile->Text;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSuperSouceCountWin::btnFileCountClick(TObject *Sender)
- {
- memoResult->Text = "";
- total.phylines = 0;
- total.logicallines = 0;
- total.commentlines = 0;
- for(int i = 0 ; i < suiListView1->Items->Count ; i++){
- Lines lines;
- CountLines((suiListView1->Items->Item[i]->Caption).c_str(), sign, &lines);
- WriteResults(lines);
- total.phylines += lines.phylines;
- total.logicallines += lines.logicallines;
- total.commentlines += lines.commentlines;
- }
- WriteResults(total,true);
- lbFileTotalCount->Caption = total.logicallines;
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- #ifndef UtilitiesH
- #define UtilitiesH
- //---------------------------------------------------------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- //---------------------------------------------------------------------------
- typedef unsigned long int ULong;
- int ReadLine(FILE * infile, char ** line);
- int IsDigits(char * str);
- int IsSpace(char * str);
- ULong MyHash(char * str);
- int ReadValue(char * str, int token, char ** value);
- int RmnewLine(char * str);
- //---------------------------------------------------------------------------
- #endif
Utilities.cpp
- //---------------------------------------------------------------------------
- #pragma hdrstop
- #include "Utilities.h"
- //---------------------------------------------------------------------------
- #define BUFSIZE 80
- //---------------------------------------------------------------------------
- int ReadLine(FILE * infile, char ** line) {
- char * tmp;
- int c, n = 0, size = BUFSIZE;
- *line = new char[size + 1];
- if (!*line){
- return 0;
- }
- while ((c = fgetc(infile)) != EOF){
- if (n == size){
- size *= 2;
- tmp = (char *) realloc(*line, size + 1);
- if(!tmp){
- return n;
- }
- *line = tmp;
- }
- *(*line + n) = c;
- n++;
- if (c == '/n')
- break;
- }
- if(n == 0 && c == EOF){
- free(*line);
- *line = NULL;
- return 0;
- }
- *(*line + n)= '/0';
- tmp = 0;
- return n;
- }
- //---------------------------------------------------------------------------
- int IsDigits(char * str)
- {
- if(!str){
- return 0;
- }
- while(*str){
- if (isdigit(*str))
- *str++;
- else
- return 0;
- }
- return 1;
- }
- //---------------------------------------------------------------------------
- int IsSpace(char * str)
- {
- if(!str){
- return 0;
- }
- while (*str) {
- if (isspace(*str))
- *str++;
- else
- return 0;
- }
- return 1;
- }
- //---------------------------------------------------------------------------
- #define SMALL_MAGICNUM 3
- #define BIG_MAGICNUM 13
- //---------------------------------------------------------------------------
- ULong MyHash(char * str)
- {
- ULong result = 0, tmp = 0;
- ULong i, len = 0;
- if (!str) {
- return 0;
- }
- len = strlen(str);
- for (i = 0; i < len; i++) {
- if (str[i] >= 'A') {
- if (str[i] <= 'L')
- tmp = str[i] * BIG_MAGICNUM;
- else
- tmp = str[i] * SMALL_MAGICNUM;
- } else {
- tmp = str[i] * SMALL_MAGICNUM;
- }
- if (!result)
- result = tmp;
- else
- result += tmp;
- }
- return result;
- }
- //---------------------------------------------------------------------------
- int ReadValue(char * str, int token, char ** value)
- {
- char * tmp;
- if(!str) {
- return 1;
- }
- tmp = strrchr(str, token);
- if (tmp) {
- tmp++;
- *value = new char[strlen(tmp) + 1];
- if (!*value) {
- *value = 0;
- tmp = 0;
- return 1;
- }
- strcpy(*value, tmp);
- }else{
- tmp = 0;
- return 1;
- }
- return 0;
- }
- //---------------------------------------------------------------------------
- int RmnewLine(char * str)
- {
- if (!str) {
- return 1;
- }
- while (*str) {
- if (*str == '/n') {
- *str = '/0';
- break;
- }
- str++;
- }
- return 0;
- }
- //---------------------------------------------------------------------------
- #pragma package(smart_init)