Click here to Skip to main content
15,314,644 members
Articles / Desktop Programming / Win32
Posted 16 Feb 2012


5 bookmarked

CodeCount counting the lines of source code

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
21 Feb 2012CPOL2 min read
It is a reminiscent SLOC console in libc/libc++ cross GNU and WIN32


CodeCount is a reminiscent console giving thanks to 80s Linux geek. Today there are fatter and fatter .NET && Visual Studio IDE, but why not try libc && vim. It is based on libc/libc++, GNU glibc and WIN32 library, so it need to be consider how to cross platform but not to mention cross build to different CPU chipset.

These days, I had a look at Pete Becker`s C++11 standard Maximilien suggested :) wrote some testcase complied with g++ 4.4.5 and visual studio 2010. But g++ 4.4.5 did not support lambda, vs 2010 did not support std::thread library.

Parsing Program Options

GUI developer might not consider char *argv[] any more, but program options is useful to a console. There is getopt provided by GNU libc, but nope to native WIN32 except that XGetopt developed by Hans Dietrich. So I simply used argv[1] to indicate the directory or file path.

Directory or File

C# developer simply input some source code in Visual Studio .Net, then it automatically add more to check the file path attribution. What about GNU glibc and libc? There is S_ISDIR macro supported by GNU libc to show the type of file, and GetFileAttributes under Windows.

FIXME: GetFileAttributes(...) return 18 (not in the Return Value list) with D:\project\.svn path variable.

/* GNU libc */
struct stat buf;
stat(path, &buf);
if (S_ISDIR(buf.st_mode))
    printf("it is a directory\n");
    printf("it is a file\n");
/* WIN32 */
switch (GetFileAttributes(path))
case -1:
    printf("invalid file\n");
case 18:
    printf("it is a directory\n");
     printf("it is a file\n");

File Name

There is basename (GNU libc) and PathFindFileName (WIN32) to get the file name from file path. For example, the file name of /home/xzhai/hello.c is hello.c and C:\hello.c is hello.c.

Walking Directory

The customized TreeView ActiveX control or UserControl might be drag into CFormView or WinForm, then simply write some code to walk directory. To GNU libc there are opendir and readdir, WIN32 has FindFirstFile and FindNextFile.

/* GNU libc */
DIR *dp;
struct dirent *ep;
dp = opendir(path);
while (ep = readdir(dp)) 
    printf("%s\n", ep->d_name);

/* WIN32 */
char buffer[MAX_PATH];
memset(buffer, 0, MAX_PATH);
sprintf(buffer, "%s\\*", path);
hFind = FindFirstFile(buffer, &FindData);
printf("%s\n", FindData.cFileName);
while (0 != FindNextFile(hFind, &FindData)) 
    printf("%s\n", FindData.cFileName);

/* GNU libc */
void walkdir(char *path) 
    while (ep = readdir(dp)) 
        if (is_dir(ep->d_name))

Is the line including code or comment?

It need to remove the lines without source code nor comment.


ANSI C way

while ('\0' != *line) 
    /* If there is a character not blank nor enter */
    if (32 != *line && '\n' != *line) 
        /* the line including code or comment */
/* there is no source code nor comment */

ISO C++ way

std::string::iterator iter;
// walk through the std::string with iterator refer to char
for (iter = obj_str.begin(); iter != obj_str.end(); iter++) 
    // If there is a character not blank nor enter
    if (32 != *iter && '\n' != *iter) 
        return true;
return false;

OOP log in C

Not only C++ can oriented object programming, but also C via typedef struct oop_t.

/* OOP in C */
typedef struct 
    int (*init)();
    void (*cleanup)();
    void (*write)(char *);
} log_t;

The function pointer init and cleanup act like construct and destruct in C++.

/* allocation for log_t* object */
log_t *log_init() 
    log_t *ret = NULL;
    ret = malloc(sizeof(log_t));
    /* given the funtion pointer address */
    ret->init = &m_init;
    ret->cleanup = &m_cleanup;
    ret->write = &m_write;
    return ret;

Private members and operations

static FILE *m_fptr = NULL;
static int m_init();
static void m_cleanup();
static void m_write(char *log); 


2012-02-20 xzhai

  • Fix m_filetype WIN32 issue, GetFileAttributes(...) return 18 with "D:\project\.svn" path variable.

2012-02-16 xzhai

  • Fix m_is_codefile issue, previous version might consider BuildLog.htm is source code.
  • Simplified m_filename in WIN32.
  • Add log support.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leslie Zhai
China China
An individual human existence should be like a river - small at first, narrowly contained within its banks, and rushing passionately past boulders and over waterfalls. Gradually the river grows wider, the banks recede, the waters flow more quietly, and in the end, without any visible break, they become merged in the sea, and painlessly lose their individual being.

Comments and Discussions

GeneralMy vote of 1 Pin
Maximilien16-Feb-12 5:40
MemberMaximilien16-Feb-12 5:40 
AnswerRe: My vote of 1 Pin
Lakamraju Raghuram21-Feb-12 7:17
MemberLakamraju Raghuram21-Feb-12 7:17 
QuestionSomewhat useless. Pin
Maximilien16-Feb-12 4:20
MemberMaximilien16-Feb-12 4:20 
AnswerRe: Somewhat useless. Pin
Leslie Zhai16-Feb-12 21:47
MemberLeslie Zhai16-Feb-12 21:47 
AnswerRe: Somewhat useless. Pin
DaveAuld17-Feb-12 2:15
professionalDaveAuld17-Feb-12 2:15 
GeneralRe: Somewhat useless. Pin
Randor 17-Feb-12 16:45
professional Randor 17-Feb-12 16:45 
QuestionWhy? Pin
Mario Majčica16-Feb-12 3:58
professionalMario Majčica16-Feb-12 3:58 
AnswerRe: Why? Pin
eXplodus16-Feb-12 4:06
MembereXplodus16-Feb-12 4:06 
GeneralRe: Why? Pin
Leslie Zhai16-Feb-12 21:43
MemberLeslie Zhai16-Feb-12 21:43 
AnswerRe: Why? Pin
Leslie Zhai16-Feb-12 21:41
MemberLeslie Zhai16-Feb-12 21:41 
QuestionFormatting Pin
Mehdi Gholam16-Feb-12 3:44
MemberMehdi Gholam16-Feb-12 3:44 
Please fix the formatting of your article.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.