include stdio include string const char ICON_NO const char DEFAULT_EXT

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <string.h>
const char ICON_NO[] = "-";
const char DEFAULT_EXT[] =".png";
//#define debug(msg1,msg2) if(msg2 == NULL) { printf (msg1,"NULL !!!!!!!"); } else { printf(msg1,msg2); }
#define debug(msg1,msg2)
/*
void strtolower(char * buf) {
int i;
int l = buf ? strlen(buf) : 0;
for ( i=0; i < l ; i++ ) {
buf[i]=tolower(buf[i]);
}
}
*/
int get_description(char * filename, int argc, char * argv[], char* result,int result_maxlen) {
char * ext_pnt = strrchr(filename,'.');
debug("%s","ext \n");
if(ext_pnt && ext_pnt != filename) {
char * ext = &ext_pnt[1];
debug("found %s\n",ext_pnt);
int i;
for ( i = 1; i< argc ; i++ ) {
char * arg = argv[i];
debug("arg %s\n",arg);
//strtolower(ext);
int extlen = strlen(ext);
char * eq_str = strchr(arg,'=');
int eq_pos=eq_str ? ( (int)(eq_str - arg)/sizeof(char) ) : extlen;
debug("eq_pos=%d\n",eq_pos);
if( (extlen == eq_pos) && strncasecmp(ext,arg,extlen) == 0 ) {
debug("strncmp ext=%s\n",ext);
strncpy(result,&arg[extlen+1],result_maxlen);
debug("str copyed=%s\n",&arg[extlen+1]);
return 1;
}
}
}
debug("not found: %s\n",ext_pnt);
strcpy(result,ICON_NO);
strcat(result, ext_pnt != NULL ? ext_pnt : DEFAULT_EXT);
//strcat(result, DEFAULT_EXT);
return 0;
}
//intputargs extension=description extension=descrtiption
/*
cat ./test.sh
#!/bin/sh
gcc test.c
clear
ls ./ | ./a.out `cat ./test.conf`
cat ./test.conf
c=c_file
sh=sh_file
txt=text_file
TXT=txt_file2
stdout:
filename=1.sh type=sh_file
filename=21.sh type=sh_file
filename=ansi-c.txt type=text_file
filename=a.out type=-.out
filename=KERNIGAN.TXT type=txt_file2
filename=test.c type=c_file
filename=test.conf type=-.con
*/
int main(int argc, char * argv[] ) {
char result[128];
char filename_buf[512];
debug("%s\n","start");
while ( fgets(filename_buf,sizeof(filename_buf), stdin) ) {
debug("fn=%s\n",filename_buf);
char * eoln = strchr(filename_buf,'\n');
if(eoln) {
debug("elon=%s\n",eoln);
eoln[0]='\0';
}
debug("fn2=%s\n",filename_buf);
int desc_ok = get_description(filename_buf, argc,argv,result,sizeof(result));
printf("filename=%s type=%s\n",filename_buf, result);
}
return 0;
}