Skip to content

Commit de39178

Browse files
author
Administrator
committed
first commit
0 parents  commit de39178

10 files changed

+163
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.DS_Store
2+
/.vscode
3+
/search_world_0_1
4+
/search_world_0_1.dSYM

inc/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
clang main.c inc.c -o main

inc/inc.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "inc.h"
2+
3+
int inc(void){
4+
return 0;
5+
}

inc/inc.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int inc(void);

inc/main

12.3 KB
Binary file not shown.

inc/main.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "main.h"
2+
#include "inc.h"
3+
4+
int main(){
5+
printf("%d\n", inc());
6+
assert(1!=0 && "1=0");
7+
return 0;
8+
}

inc/main.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <assert.h>
4+
5+

search_word.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <errno.h>
3+
#include <stdlib.h>
4+
5+
#define ERROR -1
6+
#define BUFFER_SIZE 100*10
7+
#define NO_KEYWORD_FOUND -1
8+
#define KEYWORD_ONE_FOUND_FIRST 1
9+
#define KEYWORD_TWO_FOUND_FIRST 2
10+
11+
12+
int parseFile(char* file_name)
13+
{
14+
int return_value = ERROR;
15+
FILE* file_pointer =0;
16+
char* buffer =0;
17+
18+
if(file_name != NULL)
19+
{
20+
if(file_pointer = fopen(file_name,"r"))
21+
{
22+
if(buffer=malloc(BUFFER_SIZE))
23+
{
24+
return_value = NO_KEYWORD_FOUND;
25+
while (fgets(buffer, BUFFER_SIZE, file_pointer)!=NULL)
26+
{
27+
if(strcmp("KEYWORD_ONE\n", buffer)==0)
28+
{
29+
return_value = KEYWORD_ONE_FOUND_FIRST;
30+
break;
31+
}
32+
if(strcmp("KEYWORD_TWO\n", buffer)==0)
33+
{
34+
return_value = KEYWORD_TWO_FOUND_FIRST;
35+
break;
36+
}
37+
}
38+
free(buffer);
39+
}
40+
fclose(file_pointer);
41+
}
42+
}
43+
return return_value;
44+
}

search_world_0_1.c

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <stdio.h>
2+
#include <errno.h>
3+
#include <stdlib.h>
4+
#include <strings.h>
5+
#include <assert.h>
6+
7+
#define ERROR -100
8+
#define BUFFER_SIZE 100*10
9+
#define NO_KEYWORD_FOUND -1
10+
#define KEYWORD_ONE_FOUND_FIRST 1
11+
#define KEYWORD_TWO_FOUND_FIRST 2
12+
13+
typedef struct fileparser
14+
{
15+
FILE * file_pointer;
16+
char* buffer;
17+
}FileParser;
18+
19+
FileParser* createParser(char* file_name);
20+
int searchFileForKeywords(FileParser* parser);
21+
void cleanupParser(FileParser* parser);
22+
23+
int parseFile(char* file_name)
24+
{
25+
int return_value;
26+
FileParser * parser = createParser(file_name);
27+
return_value = searchFileForKeywords(parser);
28+
cleanupParser(parser);
29+
return return_value;
30+
}
31+
32+
int searchFileForKeywords(FileParser* parser)
33+
{
34+
if(parser == NULL)
35+
{
36+
return ERROR;
37+
}
38+
while(fgets(parser->buffer, BUFFER_SIZE, parser->file_pointer)!=NULL)
39+
{
40+
if(strcmp("KEYWORD_ONE\n", parser->buffer)==0)
41+
{
42+
return KEYWORD_ONE_FOUND_FIRST;
43+
}
44+
if(strcmp("KEYWORD_TWO\n", parser->buffer)==0)
45+
{
46+
return KEYWORD_TWO_FOUND_FIRST;
47+
}
48+
}
49+
return NO_KEYWORD_FOUND;
50+
}
51+
52+
FileParser* createParser(char* file_name)
53+
{
54+
assert(file_name!=NULL && "Недопустимое имя файла");
55+
FileParser* parser = malloc(sizeof(FileParser));
56+
if(parser)
57+
{
58+
parser->file_pointer=fopen(file_name, "r");
59+
parser->buffer = malloc(BUFFER_SIZE);
60+
if(!parser->file_pointer || !parser->buffer)
61+
{
62+
cleanupParser(parser);
63+
return NULL;
64+
}
65+
}
66+
return parser;
67+
}
68+
69+
void cleanupParser(FileParser* parser)
70+
{
71+
if(parser)
72+
{
73+
if(parser->buffer)
74+
{
75+
free(parser->buffer);
76+
}
77+
if(parser->file_pointer)
78+
{
79+
fclose(parser->file_pointer);
80+
}
81+
free(parser);
82+
}
83+
}
84+
85+
int main(int argc, char* argv[])
86+
{
87+
if(argv[1]!=NULL) printf("%d\n", parseFile(argv[1]));
88+
else printf("Нужно имя файла\n");
89+
90+
return 0;
91+
}

test.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
qwerty
2+
KEYWORD_ON
3+
KEYWORD_TWO

0 commit comments

Comments
 (0)