Learn the excellent source code-orisun-blog
Learning excellent source code
Determine whether the image is used based on the suffix of the file name.
?
123456789101112131415 |
Char * Pic_list [] = { "Jpg" , "BMP" , "PNG" , Null }; Int Ispic ( Char * Name) { Char ** Listwalk; Int Len; For (Listwalk = pic_list; * listwalk; listwalk ++ ){ Len = Strlen (* Listwalk ); If ( Strlen
(Name)> Len && ! Strcasecmp (name + Strlen (Name)-Len, * listwalk )) Break ; } Return (* Listwalk! = NULL ); }
|
There are two points: 1. learn how to traverse the array without knowing the length of the array, instead of for (I = 0; I <arr. len; I ++), but creates a two-dimensional array iterator (listwalk = pic_list ). In addition, why should we create a char ** listwalk = pic_list separately and traverse through listwalk instead of directly traversing through pic_list? After several rounds of pic_list ++, pic_list no longer points to the two-dimensional array header.
2. deliberately set the last element of the array to null. Because we do not know the length of the array, we must set an array ending flag. The C language does not check array subscript overflow, so the overflow pointer usually points to not necessarily null.
Create a multi-level directory
?
123456789101112131415161718192021222324252627282930313233343536373839 |
Int Mkpath ( Char * Path, mode_t mode, mode_t dir_mode) { Struct Stat Sb;
Register Char * Slash; // Slash diagonal lines Int Done = 0; Path = slash = strdup (PATH ); If (Path = NULL ){ Warn (
"Strdup" ); /* Void warn (const char * FMT,...); warn () is also based on the global variable errno to find errmessage, but better than perror, it supports formatting output */ Return (-1 ); } While (! Done ){ Slash + = Strspns
(Slash, "/" ); Slash + = Strcspns (Slash, "/" ); Done = (* slash = '\ 0' ); * Slash = '\ 0'
; If (STAT (path, & SB )){ If ( Errno ! = Enoent | (mkdir (path, done? Mode: dir_mode )&& Errno ! = Eexist )){ Warn (
"% S" , PATH ); Goto Err; } } Else If (! S_isdir (sb. st_mode )){ // S_isdir: determines whether a path is a directory. More related functions are defined in sys/STAT. h.
Warnx ( "% S: % s" , Path, Strerror (Enotdir )); Goto Err; } * Slash =
'/' ; } Free (PATH ); Return (0 ); Err: Free (PATH );
Return (-1 ); } |
1. Two char * s are also used this time. Slash and path point to the path string. path does not always point to the string header, and slash acts as the iterator. Slash skips the first '/' and then returns a '/' to '\ 0', which is equivalent to truncating the path. In this way, you can create directories step by step.
2. Check this line: If (errno! = Enoent | (mkdir (path, done? Mode: dir_mode) & errno! = Eexist), equivalent to three rows:
If (errno = enoent ){
If (mkdir (path, done? Mode: dir_mode )){
If (errno! = Eexist ){}
}
}
Writing in a row is a more refined expression.
Extensive use of macro definitionsIn addition to savingCodeIf you use macro-defined functions, you can avoid entering the stack and improve execution efficiency. This is similar to the inline function in C ++. In C ++, macro definition is not recommended and can be avoided.
Determine whether the image is used based on the suffix of the file name.
?
123456789101112131415 |
Char * Pic_list [] = { "Jpg" , "BMP" , "PNG" , Null }; Int Ispic ( Char * Name) { Char ** Listwalk;
Int Len; For (Listwalk = pic_list; * listwalk; listwalk ++ ){ Len = Strlen (* Listwalk ); If ( Strlen (Name)> Len &&
! Strcasecmp (name + Strlen (Name)-Len, * listwalk )) Break ; } Return (* Listwalk! = NULL ); } |
There are two points: 1. learn how to traverse the array without knowing the length of the array, instead of for (I = 0; I <arr. len; I ++), but creates a two-dimensional array iterator (listwalk = pic_list ). In addition, why should we create a char ** listwalk = pic_list separately and traverse through listwalk instead of directly traversing through pic_list? After several rounds of pic_list ++, pic_list no longer points to the two-dimensional array header.
2. deliberately set the last element of the array to null. Because we do not know the length of the array, we must set an array ending flag. The C language does not check array subscript overflow, so the overflow pointer usually points to not necessarily null.
Create a multi-level directory
?
123456789101112131415161718192021222324252627282930313233343536373839 |
Int Mkpath ( Char * Path, mode_t mode, mode_t dir_mode) { Struct Stat Sb;
Register Char * Slash; // Slash diagonal lines Int Done = 0; Path = slash = strdup (PATH ); If (Path = NULL ){ Warn (
"Strdup" ); /* Void warn (const char * FMT,...); warn () is also based on the global variable errno to find errmessage, but better than perror, it supports formatting output */ Return (-1 ); } While (! Done ){ Slash + = Strspns
(Slash, "/" ); Slash + = Strcspns (Slash, "/" ); Done = (* slash = '\ 0' ); * Slash = '\ 0'
; If (STAT (path, & SB )){ If ( Errno ! = Enoent | (mkdir (path, done? Mode: dir_mode )&& Errno ! = Eexist )){ Warn (
"% S" , PATH ); Goto Err; } } Else If (! S_isdir (sb. st_mode )){ // S_isdir: determines whether a path is a directory. More related functions are defined in sys/STAT. h.
Warnx ( "% S: % s" , Path, Strerror (Enotdir )); Goto Err; } * Slash =
'/' ; } Free (PATH ); Return (0 ); Err: Free (PATH );
Return (-1 ); } |
1. Two char * s are also used this time. Slash and path point to the path string. path does not always point to the string header, and slash acts as the iterator. Slash skips the first '/' and then returns a '/' to '\ 0', which is equivalent to truncating the path. In this way, you can create directories step by step.
2. Check this line: If (errno! = Enoent | (mkdir (path, done? Mode: dir_mode) & errno! = Eexist), equivalent to three rows:
If (errno = enoent ){
If (mkdir (path, done? Mode: dir_mode )){
If (errno! = Eexist ){}
}
}
Writing in a row is a more refined expression.
Extensive use of macro definitionsIn addition to saving the amount of code, using macro-defined functions can avoid stack loading and improve execution efficiency, which is similar to the inline function in C ++. In C ++, macro definition is not recommended and can be avoided.