Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Win32

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 31.5K   330   5   11
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)


Written By
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.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Maximilien16-Feb-12 5:40
Maximilien16-Feb-12 5:40 
AnswerRe: My vote of 1 Pin
Lakamraju Raghuram21-Feb-12 7:17
Lakamraju Raghuram21-Feb-12 7:17 
QuestionSomewhat useless. Pin
Maximilien16-Feb-12 4:20
Maximilien16-Feb-12 4:20 
AnswerRe: Somewhat useless. Pin
Leslie Zhai16-Feb-12 21:47
Leslie 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
eXplodus16-Feb-12 4:06 
GeneralRe: Why? Pin
Leslie Zhai16-Feb-12 21:43
Leslie Zhai16-Feb-12 21:43 
I have not checked Mono IDE supported the feature or not Smile | :)
AnswerRe: Why? Pin
Leslie Zhai16-Feb-12 21:41
Leslie Zhai16-Feb-12 21:41 
QuestionFormatting Pin
Mehdi Gholam16-Feb-12 3:44
Mehdi Gholam16-Feb-12 3:44 

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.