代码统计工具 source code count ソース規模計算ツール

 

 

 

TSuperSouceCountWin.h

 

  1. //---------------------------------------------------------------------------
  2. #ifndef TSuperSouceCountWinH
  3. #define TSuperSouceCountWinH
  4. //---------------------------------------------------------------------------
  5. #include <Classes.hpp>
  6. #include <Controls.hpp>
  7. #include <StdCtrls.hpp>
  8. #include <Forms.hpp>
  9. #include "SUIForm.hpp"
  10. #include <ExtCtrls.hpp>
  11. #include "SUIGroupBox.hpp"
  12. #include "SUIImagePanel.hpp"
  13. #include "TFlatSplitterUnit.hpp"
  14. #include "TFlatPanelUnit.hpp"
  15. #include "SUIButton.hpp"
  16. #include "SUIComboBox.hpp"
  17. #include "SUIListBox.hpp"
  18. #include "SUIPageControl.hpp"
  19. #include "SUITabControl.hpp"
  20. #include <FileCtrl.hpp>
  21. #include "SUIEdit.hpp"
  22. #include "SUIListView.hpp"
  23. #include <ComCtrls.hpp>
  24. #include "SUIMemo.hpp"
  25. #include "Utilities.h"
  26. //---------------------------------------------------------------------------
  27. #define MAXCHARS 20 
  28. //---------------------------------------------------------------------------
  29. //配置情報
  30. typedef struct _signinfo {
  31.     char * type;
  32.     char * blockstart;
  33.     char * blockend;
  34.     char * linesign;
  35.     char * escape;
  36.     char * quote;
  37. } SignInfo;
  38. //---------------------------------------------------------------------------
  39. //ファイル情報
  40. typedef struct _lines {
  41.     char * type;
  42.     AnsiString filename;
  43.     ULong phylines;
  44.     ULong logicallines;
  45.     ULong commentlines;
  46. } Lines;
  47. //---------------------------------------------------------------------------
  48. class TSuperSouceCountWin : public TForm
  49. {
  50. __published:    // IDE-managed Components
  51.     TsuiForm *suiForm1;
  52.     TFlatPanel *FlatPanel1;
  53.     TFlatSplitter *FlatSplitter1;
  54.     TFlatPanel *FlatPanel2;
  55.     TsuiPageControl *suiPageControl1;
  56.     TsuiTabSheet *suiTabSheet1;
  57.     TsuiTabSheet *suiTabSheet2;
  58.     TsuiDriveComboBox *suiDriveComboBox1;
  59.     TsuiDirectoryListBox *suiDirectoryListBox1;
  60.     TsuiButton *btnFolderCount;
  61.     TsuiEdit *edFile;
  62.     TsuiListView *suiListView1;
  63.     TsuiButton *btnDelete;
  64.     TsuiButton *btnDeleteAll;
  65.     TsuiButton *btnSelectFile;
  66.     TsuiButton *btnadd;
  67.     TsuiButton *btnReplace;
  68.     TsuiGroupBox *suiGroupBox1;
  69.     TsuiButton *btnFileCount;
  70.     TLabel *Label1;
  71.     TLabel *lbFileTotalCount;
  72.     TLabel *Label2;
  73.     TLabel *lbFolderTotalCount;
  74.     TsuiMemo *memoResult;
  75.     TLabel *Label3;
  76.     TsuiEdit *edNO;
  77.     TStatusBar *StatusBar1;
  78.     TsuiCheckBox *cbSubFolder;
  79.     void __fastcall btnFolderCountClick(TObject *Sender);
  80.     void __fastcall btnaddClick(TObject *Sender);
  81.     void __fastcall btnFileCountClick(TObject *Sender);
  82. private:    // User declarations
  83.     AnsiString BugNO;
  84.     SignInfo sign;
  85.     Lines total;
  86.     void __fastcall DoFolder(AnsiString folder);
  87.     int __fastcall ReadSignDB(ULong  hash, SignInfo * sign);
  88.     int __fastcall CountLines(char * filename, SignInfo sign, Lines * lines);
  89.     void __fastcall WriteResults(Lines result,bool total = false);
  90. public:     // User declarations
  91.     __fastcall TSuperSouceCountWin(TComponent* Owner);
  92. };
  93. //---------------------------------------------------------------------------
  94. extern PACKAGE TSuperSouceCountWin *SuperSouceCountWin;
  95. //---------------------------------------------------------------------------
  96. #endif

 

TSuperSouceCountWin.cpp

 

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "TSuperSouceCountWin.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "SUIForm"
  8. #pragma link "SUIImagePanel"
  9. #pragma link "SUIGroupBox"
  10. #pragma link "SUIImagePanel"
  11. #pragma link "TFlatSplitterUnit"
  12. #pragma link "TFlatPanelUnit"
  13. #pragma link "SUIButton"
  14. #pragma link "SUIComboBox"
  15. #pragma link "SUIListBox"
  16. #pragma link "SUIPageControl"
  17. #pragma link "SUITabControl"
  18. #pragma link "SUIEdit"
  19. #pragma link "SUIListView"
  20. #pragma link "SUIMemo"
  21. #pragma resource "*.dfm"
  22. //---------------------------------------------------------------------------
  23. const char * Exp = "--------------------------------------------";
  24. TSuperSouceCountWin *SuperSouceCountWin;
  25. //---------------------------------------------------------------------------
  26. __fastcall TSuperSouceCountWin::TSuperSouceCountWin(TComponent* Owner)
  27.     : TForm(Owner)
  28. {
  29.     ReadSignDB(MyHash(NULL), &sign);
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TSuperSouceCountWin::btnFolderCountClick(TObject *Sender)
  33. {
  34.     memoResult->Text = "";
  35.     BugNO = edNO->Text;
  36.     total.phylines = 0;
  37.     total.logicallines = 0;
  38.     total.commentlines = 0;
  39.     DoFolder(suiDirectoryListBox1->Directory);
  40.     WriteResults(total,true);
  41. }
  42. //---------------------------------------------------------------------------
  43. void __fastcall TSuperSouceCountWin::DoFolder(AnsiString folder)
  44. {
  45.     TSearchRec sr;
  46.     if (FindFirst(folder+"//*.*", faAnyFile, sr) == 0){
  47.         do{
  48.             Application->ProcessMessages();
  49.             if(sr.Name!="." && sr.Name!=".."){
  50.                 if( cbSubFolder->Checked && (sr.Attr & faDirectory) == faDirectory ){
  51.                     DoFolder(folder+"//"+sr.Name);
  52.                 }else{
  53.                     Lines lines;
  54.                     CountLines((folder+"//"+sr.Name).c_str(), sign, &lines);
  55.                     WriteResults(lines);
  56.                     total.phylines += lines.phylines;
  57.                     total.logicallines += lines.logicallines;
  58.                     total.commentlines += lines.commentlines;
  59.                 }
  60.             }
  61.         } while (FindNext(sr) == 0);
  62.         FindClose(sr);
  63.     }
  64. }
  65. //---------------------------------------------------------------------------
  66. int __fastcall TSuperSouceCountWin::ReadSignDB(ULong hashcode, SignInfo * sign)
  67. {
  68.     FILE * fp;
  69.     char value[MAXCHARS + 1] = "/0";
  70.     int token = '=';
  71.     if (!sign) {
  72.         return 1;
  73.     }
  74.     fp = fopen("sign.db""r");
  75.     if (!fp) {  
  76.         return 1;
  77.         
  78.     }
  79.     fseek(fp, 0, SEEK_SET);
  80.     while (fgets(value, MAXCHARS, fp)) {
  81.         if(atoi(value) == hashcode){
  82.             break;
  83.         }
  84.     }
  85.     
  86.     //fgets(value, MAXCHARS, fp);
  87.     sign->type = new char[strlen(value) + 1];
  88.     strcpy(sign->type, value);
  89.     
  90.     fgets(value, MAXCHARS, fp);
  91.     ReadValue(value, token, &sign->blockstart);
  92.     RmnewLine(sign->blockstart);
  93.     
  94.     fgets(value, MAXCHARS, fp);
  95.     ReadValue(value, token, &sign->blockend);
  96.     RmnewLine(sign->blockend);
  97.     
  98.     fgets(value, MAXCHARS, fp);
  99.     ReadValue(value, token, &sign->linesign);
  100.     RmnewLine(sign->linesign);
  101.     fgets(value, MAXCHARS, fp);
  102.     ReadValue(value, token, &sign->escape);
  103.     RmnewLine(sign->escape);
  104.     fgets(value, MAXCHARS, fp);
  105.     ReadValue(value, token, &sign->quote);
  106.     RmnewLine(sign->quote);
  107.     
  108.     fclose(fp); 
  109.     return 0;
  110.     
  111. }
  112. //---------------------------------------------------------------------------
  113. int __fastcall TSuperSouceCountWin::CountLines(char * filename, SignInfo sign, Lines * lines)
  114. {
  115.     FILE * fp;
  116.     int iscomment = 0, both = 0, linecomment = 0, commentend = 0, isquote = 0;
  117.     int c = 0;
  118.     bool isstart = false;
  119.     char * buffer, * newline, * tmp, * src;
  120.     ULong nsize, nread;
  121.     
  122.     if(!lines){
  123.         return 1;
  124.     }
  125.     lines->type = new char[strlen(sign.type) + 1];
  126.     if(!lines->type){
  127.         return 1;
  128.     }
  129.     strcpy(lines->type, sign.type);
  130.     lines->filename = filename; 
  131.     lines->phylines = 0;
  132.     lines->logicallines = 0;
  133.     lines->commentlines = 0;
  134.     
  135.     fp = fopen(filename, "r");
  136.     if(!fp){
  137.         return 1;
  138.     }
  139.     fseek(fp, 0, SEEK_END);
  140.     nsize = ftell(fp);
  141.     fseek(fp, 0, SEEK_SET);
  142.     buffer = new char[nsize + 1];
  143.     if(!buffer){
  144.         return 1;
  145.     }
  146.     for(nread = 0; nread < nsize; nread++){
  147.         c = fgetc(fp);
  148.         if(c == EOF){
  149.             break;
  150.         }
  151.         *(buffer + nread) = c;
  152.     }
  153.     *(buffer + nread) = '/0';
  154.     
  155.     src = buffer;
  156.     newline = buffer;
  157.     while(*buffer){
  158.         if (!strncmp(buffer, BugNO.c_str(), BugNO.Length())) {
  159.             if(!isstart){
  160.                 isstart = true;
  161.             }else{
  162.                 isstart = false;
  163.             }
  164.         }
  165.         if(isstart){
  166.             if (!strncmp(buffer, sign.escape, strlen(sign.escape))) {
  167.                 buffer += strlen(sign.escape) + strlen(sign.quote) + 1;
  168.                 continue;
  169.             
  170.             }
  171.             if (!strncmp(buffer, sign.quote, strlen(sign.quote)) && !iscomment) {
  172.                 isquote = !isquote;
  173.             }
  174.             if (*buffer == sign.blockstart[0] && !isquote) {
  175.                 if (!strncmp(buffer, sign.blockstart, strlen(sign.blockstart))) {
  176.                     iscomment = 1;
  177.                     commentend = 0;
  178.                     tmp = buffer;
  179.                     if (!isquote) {
  180.                         for(; tmp > newline; tmp--){
  181.                             if (!isspace(*tmp)) {
  182.                                 //printf("a both:%d/n", lines->phylines + 1);
  183.                                 both = 1;
  184.                                 break;
  185.                             }
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.             if(*buffer == sign.linesign[0] && !isquote) {
  191.                 if (!strncmp(buffer, sign.linesign, strlen(sign.linesign))) {
  192.                     linecomment = 1;
  193.                     tmp = buffer;
  194.                     if(!isquote){
  195.                         for(; tmp > newline; tmp--){
  196.                             if (!isspace(*tmp)) {
  197.                                 //printf("b both:%d/n", lines->phylines + 1);
  198.                                 both = 1;
  199.                                 break;
  200.                             }
  201.                         }
  202.                     }
  203.                 }
  204.             }
  205.             if(*buffer == sign.blockend[0] && !isquote){
  206.                 if (!strncmp(buffer, sign.blockend, strlen(sign.blockend))) {
  207.                     commentend = 1;
  208.                     iscomment = 0;
  209.                     linecomment = 0;
  210.                     tmp = buffer + strlen(sign.blockend);
  211.                 }
  212.             }
  213.             if (*buffer == '/n') {
  214.                 lines->phylines++;
  215.                 if (iscomment) {
  216.                     //printf("blockstart:%d/n", lines->phylines);
  217.                     lines->commentlines++;
  218.                     if (both) {
  219.                         lines->logicallines++;
  220.                         both = 0;
  221.                     }
  222.                 }else if(linecomment){
  223.                     //printf("line:%d/n", lines->phylines);
  224.                     linecomment = 0;
  225.                     lines->commentlines++;
  226.                     if(both){
  227.                         lines->logicallines++;
  228.                         both = 0;
  229.                     }
  230.                 } else if(commentend){
  231.                     //printf("blockend:%d/n", lines->phylines);
  232.                     commentend = 0;
  233.                     lines->commentlines++;
  234.                     if (both) {
  235.                         lines->logicallines++;
  236.                         both = 0;
  237.                     }else{
  238.                         for(; tmp < buffer; tmp++){
  239.                             if (!isspace(*tmp)) {
  240.                                 //printf("c both:%d/n", lines->phylines);
  241.                                 lines->logicallines++;
  242.                                 break;
  243.                             }
  244.                         }
  245.                     }
  246.                 }else{
  247.                     //printf("logical:%d/n", lines->phylines);
  248.                     lines->logicallines++;
  249.                 }
  250.                 newline = buffer + 1;
  251.             }
  252.         }
  253.         buffer++;
  254.     }
  255.     free(src);
  256.     fclose(fp);
  257.     return 0;
  258. }
  259. //---------------------------------------------------------------------------
  260. void __fastcall TSuperSouceCountWin::WriteResults(Lines result , bool total)
  261. {
  262.     char str[256];
  263.     memoResult->Text = memoResult->Text  +  "/r/n" + Exp;
  264.     if(!total){
  265.         sprintf(str, "ファイル名:%s/t実際行数:%d/t論理行数:%d/tコメント行数:%d/t/n", result.filename , result.phylines, result.logicallines, result.commentlines);
  266.     }else{
  267.         sprintf(str, "統計:/t実際行数:%d/t論理行数:%d/tコメント行数:%d/t/n", result.phylines, result.logicallines, result.commentlines);
  268.         lbFolderTotalCount->Caption =  result.logicallines;
  269.     }
  270.     memoResult->Text = memoResult->Text  +  "/r/n" + str;
  271.     memoResult->Perform(WM_VSCROLL,SB_BOTTOM,0);
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall TSuperSouceCountWin::btnaddClick(TObject *Sender)
  275. {
  276.     TListItem  *listItem;
  277.     listItem = suiListView1->Items->Add();
  278.     listItem->Caption = edFile->Text;
  279. }
  280. //---------------------------------------------------------------------------
  281. void __fastcall TSuperSouceCountWin::btnFileCountClick(TObject *Sender)
  282. {
  283.     memoResult->Text = "";
  284.     total.phylines = 0;
  285.     total.logicallines = 0;
  286.     total.commentlines = 0;
  287.     for(int i = 0 ; i < suiListView1->Items->Count ; i++){
  288.         Lines lines;
  289.         CountLines((suiListView1->Items->Item[i]->Caption).c_str(), sign, &lines);
  290.         WriteResults(lines);
  291.         total.phylines += lines.phylines;
  292.         total.logicallines += lines.logicallines;
  293.         total.commentlines += lines.commentlines;
  294.     }
  295.     WriteResults(total,true);
  296.     lbFileTotalCount->Caption  =  total.logicallines;
  297. }
  298. //---------------------------------------------------------------------------
Utilities.h

 

  1. //---------------------------------------------------------------------------
  2. #ifndef UtilitiesH
  3. #define UtilitiesH
  4. //---------------------------------------------------------------------------
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. //---------------------------------------------------------------------------
  10. typedef unsigned long int ULong;
  11. int ReadLine(FILE * infile, char ** line);
  12. int IsDigits(char * str);
  13. int IsSpace(char * str);
  14. ULong MyHash(char * str);
  15. int ReadValue(char * str, int token, char ** value);
  16. int RmnewLine(char * str);
  17. //---------------------------------------------------------------------------
  18. #endif

 

Utilities.cpp

  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. #include "Utilities.h"
  4. //---------------------------------------------------------------------------
  5. #define BUFSIZE 80
  6. //---------------------------------------------------------------------------
  7. int ReadLine(FILE * infile, char ** line) {
  8.     char * tmp;
  9.     int c, n = 0, size = BUFSIZE;
  10.     *line  = new char[size + 1];
  11.     if (!*line){
  12.         return 0;
  13.     }
  14.     while ((c = fgetc(infile)) != EOF){
  15.         if (n == size){
  16.             size *= 2;
  17.             tmp = (char *) realloc(*line, size + 1);
  18.             
  19.             if(!tmp){
  20.                 return n;
  21.             }
  22.             *line = tmp;
  23.         }
  24.         *(*line + n) = c;
  25.         n++;
  26.         
  27.         if (c == '/n')
  28.             break;
  29.     }
  30.     if(n == 0 && c == EOF){
  31.         free(*line);
  32.         *line = NULL;
  33.         return 0;
  34.     }
  35.     
  36.     *(*line + n)= '/0';
  37.     tmp = 0;
  38.     return n;
  39. }
  40. //---------------------------------------------------------------------------
  41. int IsDigits(char * str)
  42. {   
  43.     if(!str){
  44.         return 0;
  45.     }
  46.     while(*str){
  47.         if (isdigit(*str))
  48.             *str++;
  49.         else
  50.             return 0;
  51.     }
  52.     return 1;
  53. }
  54. //---------------------------------------------------------------------------
  55. int IsSpace(char * str)
  56. {
  57.     if(!str){
  58.         return 0;
  59.     }
  60.     while (*str) {
  61.         if (isspace(*str))
  62.             *str++;
  63.         else
  64.             return 0;
  65.     }
  66.     return 1;
  67. }
  68. //---------------------------------------------------------------------------
  69. #define SMALL_MAGICNUM 3
  70. #define BIG_MAGICNUM 13
  71. //---------------------------------------------------------------------------
  72. ULong MyHash(char * str)
  73. {
  74.     ULong result = 0, tmp = 0;
  75.     ULong i, len = 0;
  76.     if (!str) {
  77.         return 0;
  78.     }
  79.     
  80.     len = strlen(str);
  81.     for (i = 0; i < len; i++) {
  82.         if (str[i] >= 'A') {
  83.             if (str[i] <= 'L')
  84.                 tmp = str[i] * BIG_MAGICNUM;
  85.             else
  86.                 tmp = str[i] * SMALL_MAGICNUM;
  87.                 
  88.         } else {
  89.             tmp = str[i] * SMALL_MAGICNUM;
  90.             
  91.         }
  92.         if (!result)
  93.             result = tmp;
  94.         else 
  95.             result += tmp;
  96.             
  97.     }
  98.     return result;
  99.     
  100. }
  101. //---------------------------------------------------------------------------
  102. int ReadValue(char * str, int token, char ** value)
  103. {
  104.     char * tmp;
  105.     
  106.     if(!str) {
  107.         return 1;
  108.     }
  109.             
  110.     tmp = strrchr(str, token);
  111.     if (tmp) {
  112.         tmp++;
  113.         *value = new char[strlen(tmp) + 1];
  114.         if (!*value) {
  115.             *value = 0;
  116.             tmp = 0;
  117.             return 1;
  118.         }
  119.         strcpy(*value, tmp);
  120.     }else{
  121.         tmp = 0;
  122.         return 1;
  123.     }
  124.     return 0;
  125. }
  126. //---------------------------------------------------------------------------
  127. int RmnewLine(char * str)
  128. {
  129.     if (!str) {
  130.         return 1;
  131.     }
  132.     while (*str) {
  133.         if (*str == '/n') {
  134.             *str = '/0';
  135.             break;
  136.         }
  137.         str++;
  138.     }
  139.     return 0;
  140. }
  141. //---------------------------------------------------------------------------
  142. #pragma package(smart_init)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值