Created
July 9, 2016 23:57
-
-
Save otaks/608c6543ec9caba7429b4fbbbbe74f63 to your computer and use it in GitHub Desktop.
c findfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "findFile.h" | |
static void findFile_findRec_s( char * ptn, char* lpszDir, list l ); | |
static int findFile_checkPtn( char* fileName, char* ptn ); | |
void findFile_find( char * ptn, char* lpszDir, list l ) { | |
char t[ 1000 ]; | |
memset( t, 0x00, 1000 ); | |
sprintf( t, "%s*", lpszDir ); | |
findFile_findRec_s( ptn, t, l ); | |
} | |
static void findFile_findRec_s( char * ptn, char* lpszDir, list l ) | |
{ | |
char subpath[ _MAX_PATH ]; | |
char temp[ _MAX_PATH ]; | |
WIN32_FIND_DATA lp; | |
strcpy( temp, lpszDir ); | |
HANDLE h = FindFirstFile( temp, &lp ); | |
temp[ strlen( temp ) - 1 ] = '\0';//*を消している | |
do | |
{ | |
if( ( lp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) | |
&& strcmp( lp.cFileName, ".." ) != 0 && strcmp( lp.cFileName, "." ) != 0 ) | |
{ | |
//ディレクトリか | |
sprintf( subpath, "%s%s\\*", temp, lp.cFileName ); | |
findFile_findRec_s( ptn, subpath, l ); | |
} | |
else if( ( lp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) | |
!= FILE_ATTRIBUTE_DIRECTORY ) | |
{ | |
//ファイルか | |
if(findFile_checkPtn( lp.cFileName, ptn)){ //パターン適合か | |
//フルパス生成 | |
char t[ 1000 ]; | |
memset( t, 0x00, 1000 ); | |
sprintf( t, "%s%s", temp, lp.cFileName ); | |
list_add( l, t ); | |
} | |
//こちらはファイルサイズ | |
//size+=lp.nFileSizeHigh*MAXDWORD+lp.nFileSizeLow ; | |
} | |
} while( FindNextFile( h, &lp ) ); | |
FindClose( h ); | |
} | |
//パターン適合か | |
static int findFile_checkPtn( char* fileName, char* ptn ) { | |
int ret = TRUE; | |
int plen = strlen( ptn ); | |
int flen = strlen( fileName ); | |
int j = plen - 1; | |
for( int i = strlen( fileName ) - 1; i >= (flen - plen); i-- ) { | |
if( fileName[ i ] != ptn[ j ] ) { | |
ret = FALSE; | |
break; | |
} | |
j--; | |
} | |
return ret; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <shlwapi.h> | |
#pragma comment( lib, "shlwapi.lib" ) | |
#include "list.h" | |
/** | |
* ファイルリスト取得。 | |
* lpszDir配下(サブフォルダ含む)内でファイル名が | |
* ptnで終わる全ファイルのフルパスをlに格納する。 | |
* | |
* @param ptn 拡張子 | |
* @param lpszDir 親ディレクトリ | |
* @param l 格納先リスト | |
*/ | |
void findFile_find( char * ptn, char* lpszDir, list l ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include "list.h" | |
#define FREE(X) free(X);X=NULL; | |
//ノード | |
typedef struct _node node; | |
struct _node { | |
void* data; | |
node* next; | |
}; | |
//リスト | |
struct _list { | |
int num; | |
dataType type; | |
node* first; | |
}; | |
//リスト作成 | |
list list_create( dataType type ) { | |
list t = calloc( sizeof( struct _list ), 1 ); | |
t->type = type; | |
return t; | |
} | |
//リスト削除 | |
void list_delete( list t ) { | |
node* n = t->first; | |
while( n != NULL ) { | |
node* a = n; | |
n = n->next; | |
//データ種別個別処理 | |
switch( t->type ) | |
{ | |
case STRING: | |
FREE( a->data ); | |
break; | |
default: | |
break; | |
} | |
FREE( a ); | |
} | |
FREE( t ); | |
} | |
node* list_getLastNode_s( list t ) { | |
node* n = t->first; | |
while( n != NULL ) { | |
if( n->next == NULL ) { | |
return n; | |
} | |
n = n->next; | |
} | |
} | |
//ノード追加 | |
//bool_ list_add( list t, void* v) { | |
int list_add( list t, void* v ) { | |
//ノード作成、 | |
node* newNode = ( node* )calloc( sizeof( struct _node ), 1 ); | |
//データ設定 | |
switch( t->type ) | |
{ | |
case STRING: | |
newNode->data = ( char* )calloc( strlen( ( char* )v ) + 1, 1 ); | |
strcpy( (char*)newNode->data, ( char* )v ); | |
break; | |
default: | |
break; | |
} | |
//末尾ノードにつなぐ | |
if( t->num != 0 ) { | |
node* n = list_getLastNode_s( t ); | |
n->next = newNode; | |
} | |
else { | |
t->first = newNode; | |
} | |
( t->num )++; | |
return 1; | |
} | |
//ノード数取得 | |
int list_getNum( list t ) { | |
return t->num; | |
} | |
//ノード取得 | |
void* list_getNode( list t, int i ) { | |
node* n = t->first; | |
if( i < t->num ) { | |
for( int j = 0; j < i; j++ ) { | |
n = n->next; | |
} | |
} | |
return n->data; | |
} | |
//文字列ノード取得 | |
char* list_getCharNode( list t, int i ) { | |
return ( char * )list_getNode(t, i); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
typedef struct _list* list; | |
//データ種別 | |
typedef enum dataType_t { | |
STRING = 0, //文字列 | |
}dataType; | |
//リスト作成 | |
list list_create( dataType type ); | |
//リスト削除 | |
void list_delete( list t ); | |
//ノード追加 | |
// vをディープコピーして追加 | |
int list_add( list t, void* v ); | |
//ノード数取得 | |
int list_getNum( list t ); | |
//ノード取得 | |
void* list_getNode( list t, int i ); | |
//文字列ノード取得 | |
char* list_getCharNode( list t, int i ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "list.h" | |
#include "findFile.h" | |
int main() { | |
list l = list_create( STRING ); | |
list l2 = list_create( STRING ); | |
findFile_find( ".c", "D:\\99.work\\30\\", l ); | |
findFile_find( ".h", "D:\\99.work\\30\\", l2 ); | |
for( int i = 0; i < list_getNum( l ); i++ ) { | |
printf( "%s\n", ( char* )list_getNode( l, i ) ); | |
} | |
for( int i = 0; i < list_getNum( l2 ); i++ ) { | |
printf( "%s\n", ( char* )list_getNode( l2, i ) ); | |
} | |
list_delete( l ); | |
list_delete( l2 ); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "list.h" | |
int main() { | |
list l = list_create(STRING); | |
list_add( l, "chance." ); | |
list_add( l, "victory" ); | |
for( int i = 0; i < list_getNum( l ); i++ ) { | |
printf( "%s\n", (char *)list_getNode(l, i) ); | |
} | |
list_delete( l ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment