Click here to Skip to main content
Click here to Skip to main content

CodeCount counting the lines of source code

, 21 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) 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");
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.

/* 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

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

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); 

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)

Share

About the Author

Leslie Zhai
Engineer
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.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMaximilien16-Feb-12 6:40 
AnswerRe: My vote of 1 PinmemberLakamraju Raghuram21-Feb-12 8:17 
QuestionSomewhat useless. PinmemberMaximilien16-Feb-12 5:20 
AnswerRe: Somewhat useless. Pinmemberxiangzhai16-Feb-12 22:47 
AnswerRe: Somewhat useless. PinmentorDaveAuld17-Feb-12 3:15 
GeneralRe: Somewhat useless. Pinmember Randor 17-Feb-12 17:45 
QuestionWhy? PinmemberMario Majcica16-Feb-12 4:58 
AnswerRe: Why? PinmembereXplodus16-Feb-12 5:06 
GeneralRe: Why? Pinmemberxiangzhai16-Feb-12 22:43 
I have not checked Mono IDE supported the feature or not Smile | :)
AnswerRe: Why? Pinmemberxiangzhai16-Feb-12 22:41 
QuestionFormatting PinmvpMehdi Gholam16-Feb-12 4:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 21 Feb 2012
Article Copyright 2012 by Leslie Zhai
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid