Optimization of showing dir contents function

Started by
10 comments, last by GameDev.net 19 years, 7 months ago
Hello, I wrote a code to show the contents of a directory + file size in bytes. Ok, no problem with it. I just want to know if it is possible to write a shorter code to jump the "." and ".." as you can see in my if: /* Jump the actual dir indicator (.) and before dir (..) */. Thanks Alfred

#include<dirent.h>
#include<stdio.h>
#include<sys/stat.h>

const char *actual_dir = "D:/Programacao/Aprendizagem/C - Completo e Total/cap9/";

int main()
{
    DIR *dr;
    struct dirent *datum;
    struct stat buf;
    
    if(!(dr = opendir(actual_dir)))
    {
        printf("Error when trying to open dir...");
        return 0;
    }    
    
    /* Lists directory content including file names started with a dot (.) */
    while(datum = readdir(dr))
    {
        /* Jump the actual dir indicator (.) and before dir (..) */
        if( (*(datum->d_name) == '.' && datum->d_namlen == 1) // .
            ||
            ((*(datum->d_name) == '.' && *(datum->d_name+1) == '.') && datum->d_namlen == 2 ) // ..
        )
        {            
            continue;            
        }        
        
        /* Get file data */
        stat(datum->d_name, &buf);
        
        printf("%-50s%10d\n", datum->d_name, buf.st_size);
    }
    
    printf("\nPress [ENTER] to exit.\n");
    getchar();
    return 0;
}


The struct dirent as you see in "dirent.h":

struct dirent
{
	long		d_ino;		/* Always zero. */
	unsigned short	d_reclen;	/* Always zero. */
	unsigned short	d_namlen;	/* Length of name in d_name. */
	char		d_name[FILENAME_MAX]; /* File name. */
};

Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
Advertisement
Well, if you changed to using c-style nul-terminated strings (why are you storing the length seperately? is the string not nul terminated already?), then you could use strcmp
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Oh! strcmp... I have had forgot it.
Thanks Extrarius! Rate ++ to you.

why are you storing the length seperately?
I'm using the C standart library, it stores.

I just changed the if to:
        /* Jump the actual dir indicator (.) and before dir (..) */        if(!strcmp(datum->d_name, ".") || !strcmp(datum->d_name, ".."))        {            continue;        } 


and it works perfect as the before sentence.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
So does that code actually work? Because if it does, then that means that the strings are null-terminated, which makes it alot easier to compare them with other strings.
Quote:Original post by bytecoder
So does that code actually work? Because if it does, then that means that the strings are null-terminated, which makes it alot easier to compare them with other strings.


Yes, both codes worked well.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
BTW, unless you're "optimizing" for source code length, the strcmp version might be worse for you because it is actually larger in terms of (machine)code size. But it does look prettier.
Quote:
BTW, unless you're "optimizing" for source code length, the strcmp version might be worse for you because it is actually larger in terms of (machine)code size. But it does look prettier.

Not just prettier, but more readable, too; which is much more important than optimizing for speed considering the fact that you usually read code more often than you write it.
Yes, my intention was to reduce the source code, not the object code.

Thanks.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
Quote:Original post by bytecoder
Quote:
BTW, unless you're "optimizing" for source code length, the strcmp version might be worse for you because it is actually larger in terms of (machine)code size. But it does look prettier.

Not just prettier, but more readable, too; which is much more important than optimizing for speed considering the fact that you usually read code more often than you write it.
Well, you usually run code more often than you read it or write it, so speed is pretty important.
    /* Lists directory content including file names started with a dot (.) */    while(datum = readdir(dr))        if(strcmp( datum->d_name, ".") && strcmp( datum->d_name, ".."))        {	        /* Get file data */	        stat(datum->d_name, &buf);	        printf("%-50s%10d\n", datum->d_name, buf.st_size);        }


cheers!

This topic is closed to new replies.

Advertisement