Un langage que j’apprécie beaucoup: Objective C
Adir.h:
#import <objc/Object.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#define TYPEDIR 1
#define TYPEFILE 2
extern int offset;
@interface Adir : Object
{
struct dirent **namelist;
int n;
int count;
char *path;
}
- free;
- (int) set_Entry: (char*) dirname;
- (int) set_next;
- (int) get_count;
- (char*) get_name;
- (int) get_type;
@end
Adir.m:
#import "Adir.h"
int select_dir (const struct dirent *adir)
{
//if ((adir->d_type == DT_DIR)
if ((strcmp(adir->d_name, ".."))
&&(strcmp(adir->d_name, ".")))
return 1;
else
return 0;
}
@implementation Adir
+ new
{
self = [super new];
return self;
}
- free
{
while(count--) free (namelist [count]);
free (namelist);
return [super free];
}
- (int) set_Entry: (char*) dirname
{
n = scandir(dirname, &namelist, select_dir, alphasort);
count = n;
return count;
}
- (int) set_next
{
return --n;
}
- (int) get_count
{
return n;
}
- (char*) get_name
{
if (n>0)
return namelist[n-1]->d_name;
else
return (char *)0;
}
- (int) get_type
{
if (namelist[n-1]->d_type == DT_DIR) return TYPEDIR;
return TYPEFILE;
}
@end
Readdir.h:
#import <objc/Object.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
@interface Readdir : Object
{
char *path; // These are instance variables.
int size;
}
/* Public methods */
- free;
- Entry: (char *) dirname;
- scan;
@end
Readdir.m:
#import "Adir.h"
#import "Readdir.h"
@implementation Readdir
+ new
{
self = [super new];
return self;
}
- free
{
free(path);
return [super free];
}
- Entry: (char *) dirname
{
asprintf(&path, "%s", dirname);
return self;
}
- scan
{
id adir;
id subdir;
char *subpath;
adir = [Adir new];
[adir set_Entry:path];
while([adir get_count])
{
int i;
for (i=0; i<offset; i++) printf("| ");
if ([adir get_type] == TYPEDIR)
{
printf("|+ %s\n", [adir get_name]);
offset++;
subdir = [Readdir new];
asprintf(&subpath, "%s/%s", path, [adir get_name]);
[subdir Entry: subpath];
[subdir scan];
[subdir free];
free (subpath);
offset--;
}else
printf("|_ %s\n", [adir get_name]);
[adir set_next];
}
[adir free];
return self;
}
@end
main.m:
#import <objc/Object.h>
#import "Readdir.h"
int offset;
main(int argc, char* argv[], char** env)
{
int i;
id readdir;
offset = 0;
if (argc != 2)
{
printf("Usage: %s dir\n", argv[0]);
exit(0);
}
readdir = [Readdir new];
[readdir Entry: argv[1]];
[readdir scan];
[readdir free];
}
A compiler avec :
make -n :
gcc -o Adir.o -c Adir.m -Wno-import -I/usr/lib/gcc/x86_64-linux-gnu/4.3/include/
gcc -o main.o -c main.m -Wno-import -I/usr/lib/gcc/x86_64-linux-gnu/4.3/include/
gcc -o Readdir.o -c Readdir.m -Wno-import -I/usr/lib/gcc/x86_64-linux-gnu/4.3/include/
gcc -lobjc -lpthread -o readdir.objc Adir.o main.o Readdir.o -L/usr/lib/gcc/x86_64-linux-gnu/4.3/
(à adapter _64…)
Résultat:
|+ tools
| |+ updater
| | |_ utils.c
| | |_ update.c
| | |_ string.c
| | |_ ppcstring.S
| | |_ junk
| | |_ flash_hw.c
| | |_ flash.c
| | |_ dummy.c
| | |_ ctype.c
| | |_ cmd_flash.c
| | |_ Makefile
| |+ scripts
| | |_ send_image
| | |_ send_cmd
| | |_ flash_param
| | |_ dot.kermrc
| | |_ README
| |_ mkimage.c
| |+ logos
| | |_ denx.bmp
| |_ inca-swap-bytes.c
| |_ img2srec.c
| |_ gen_eth_addr.c
| |+ gdb
| | |_ serial.h
| | |_ serial.c
| | |_ remote.h
:smt111 (j’aime bien ce smiley, merci aux administrateurs. ).