Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

CodeCount counting the lines of source code

4.43/5 (3 votes)
21 Feb 2012CPOL2 min read 32.9K   330  
It is a reminiscent SLOC console in libc/libc++ cross GNU and WIN32

Introduction

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.

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

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.

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

/* WIN32 */
WIN32_FIND_DATA FindData;
HANDLE hFind = INVALID_HANDLE_VALUE;
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);
}
FindClose(hFind);

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

Is the line including code or comment?

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

>

ANSI C way

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

ISO C++ way

C++
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.

C++
/* 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++.

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

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

History

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.

License

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