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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #ifndef MYQUEUE_H #define MYQUEUE_H #pragma warning(disable:4996) #include <stdlib.h> #include <string.h> typedef char* T; typedef struct qNode { T data; struct qNode * next; }qNode; typedef struct myQueue { qNode * front; qNode * back; int queueNum; }myQueue; int empty(myQueue *q) { //1 비어있다. //0 요소가 있다. if(q->queueNum > 0) return 0; else return 1; } void enqueue(myQueue *q, T inputData) { qNode * newNode; //newNode = new qNode; newNode = (qNode*)malloc(sizeof(struct qNode)); newNode->next = NULL; //newNode->data = inputData; newNode->data = (char*)malloc( strlen(inputData) +1 ); strcpy(newNode->data, inputData); *(newNode->data + strlen(inputData)) = 0; if( empty(q) ) { //비어있을때의 동작 q->front = newNode; q->back = newNode; } else { q->back->next = newNode; q->back = newNode; } q->queueNum++; return; } void initQueue(myQueue *q) { q->front = NULL; q->back = NULL; q->queueNum = 0; } T frontQueue(myQueue *q) { return q->front->data; } T backQueue(myQueue *q) { return q->back->data; } void deQueue(myQueue *q) { qNode * temp; //큐에서 삭제 if(empty(q)) { return; }else { temp = q->front; q->front = q->front->next; //char * 자원해제 free(temp->data); free(temp); q->queueNum--; } } void destroyQueue(myQueue *q) { while( !empty(q) ) { //dequeue를 한면서 메모리를 해제한다. deQueue(q); } } #endif |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #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; } |