#include <stdio.h>
#include <sys/types.h>
#include "MyQueue.h"
#include "dirent.h"
#pragma warning(disable:4996)
myQueue mQueue;
static int
find_directory(
const char *dirname)
{
DIR *dir;
char buffer[PATH_MAX + 2];
char *p = buffer;
const char *src;
char *end = &buffer[PATH_MAX];
int ok;
char * extName;
/* Copy directory name to buffer */
src = dirname;
while (p < end && *src != '\0') {
*p++ = *src++;
}
*p = '\0';
/* Open directory stream */
dir = opendir (dirname);
if (dir != NULL) {
struct dirent *ent;
/* Print all files and directories within the directory */
while ((ent = readdir (dir)) != NULL) {
char *q = p;
char c;
/* Get final character of directory name */
if (buffer < q) {
c = q[-1];
} else {
c = ':';
}
/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '\\') {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
*q++ = '\\';
#else
*q++ = '/';
#endif
}
/* Append file name */
src = ent->d_name;
while (q < end && *src != '\0') {
*q++ = *src++;
}
*q = '\0';
/* Decide what to do with the directory entry */
switch (ent->d_type) {
case DT_REG:
/* Output file name with directory */
extName = strrchr(ent->d_name, '.');
if(extName == NULL) break;
if(strcmp(extName, ".txt") == 0)
{
//printf("fine txt file : %s\n", ent->d_name);
//printf ("%s\n", buffer);
enqueue(&mQueue, buffer);
}
break;
case DT_DIR:
/* Scan sub-directory recursively */
if (strcmp (ent->d_name, ".") != 0
&& strcmp (ent->d_name, "..") != 0) {
find_directory (buffer);
}
break;
default:
/* Do not device entries */
/*NOP*/;
}
}
closedir (dir);
ok = 1;
} else {
/* Could not open directory */
printf ("Cannot open directory %s\n", dirname);
ok = 0;
}
return ok;
}
void myDir(const char * dirPath)
{
DIR * dp;
struct dirent * ent;
char * extName;
dp = opendir(dirPath);
if(dp != NULL)
{
while(1)
{
ent = readdir(dp);
if(ent == NULL)
break;
extName = strrchr(ent->d_name, '.');
if(strcmp(extName, ".txt") == 0)
printf("fine txt file : %s\n", ent->d_name);
}
}
}
int IsAvailableEMail(const char * srcMail)
{
// 찾음 : 1
// 없음 : 0
int iAtCount = 0; //@ 위치
int iDotCount = 0; // . 위치
int i;
char * eMail = (char*)malloc( strlen(srcMail) + 1 );
strcpy(eMail, srcMail);
if(strcmp(eMail, "") == 0) return 0;
for(i = 0; i < strlen(eMail); i++)
{
if(i > 0 && eMail[i] == '@' ) iAtCount = i+1; // ①
if(iAtCount > 0 && i > iAtCount && eMail[i] == '.') iDotCount = i+1; // ②
}
free(eMail);
if(i > iDotCount && iAtCount > 0 && iDotCount > 0) return 1; // ③
else return 0;
}
int main()
{
//입력받은 디렉토리에서 확장자가 .txt인 파일을 찾고
//큐에 넣은 후 이메일 주소를 뽑아내는 구조
//큐 초기화
FILE * fp;
char buf[1024];
char * path;
const char * filePath;
initQueue(&mQueue);
//디렉토리 순회
find_directory("C:\\Mail");
////제대로 나오는지 출력
//while( !empty(&mQueue) )
//{
// printf("%s\n", frontQueue(&mQueue));
// deQueue(&mQueue);
//}
//
//찾은 파일을 열어서 이메일 주소를 확인
while( !empty(&mQueue) )
{
path = frontQueue(&mQueue);
filePath = path;
fp = fopen(filePath, "r");
if(fp != NULL)
{
int isEmail;
char * ch;
int i;
//while(fgets(buf, 1024, fp))
while( 0 < fscanf(fp, "%s", buf) )
{
//printf("%s", buf);
isEmail = IsAvailableEMail(buf);
if(isEmail)
{
ch = strchr(buf, '"');
if(ch != NULL)
*ch = ' ';
ch = strchr(buf, '(');
if(ch != NULL)
*ch = ' ';
ch = strchr(buf, ')');
if(ch != NULL)
*ch = ' ';
printf("%s\n", buf);
}
}
fclose(fp);
deQueue(&mQueue);
}
}
destroyQueue(&mQueue);
return 0;
}
|