summaryrefslogtreecommitdiff
path: root/main.c
blob: 9ab58febdc7de1b72019e9671592def6ddd50364 (plain) (blame)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <getopt.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>

#include "rabincarp.h"

int file_open( const char *fname )
{
    int fd;

    fd = open( fname, O_RDONLY );
    if ( fd == -1 )
    {
        printf("Couldn open file %s\n", fname);
        return -1;
    }

    return fd;
}

int file_read( int fd, char *buf, int size )
{
    int ret;

    if ( size < 1 )
        return -1;

    ret = (int)read( fd, buf, size );

    return ret;
}

int file_seek( int fd, off_t pos )
{
    off_t rc;

    if (fd<1)
    {
        return -1;
    }

    rc = lseek( fd, pos, SEEK_SET );
    if (rc == -1)
        return -1;
    return 0;

}

int file_close( int fd )
{
    close( fd );
    return 0;
}

typedef struct match_params
{
    char *fname;
    char *match_str;
    int match_len;
    int buf_size;
    int verbose;
} match_params;


void helper( char *progname )
{
    printf("Usage: %s [OPTS]\n\n"
        " -f - input file\n"
        " -m - match string\n"
        //" -b - set buffer size to read\n"
        " -v - more extra info in output\n"
        "Version: 0.0.1 \n"
        "\n"
        , progname);
}

int main(int argc, char **argv)
{

	int c;
    int i;
	int fd;
    int text_len;
    int ret;

	char *text = NULL;

    //search
    rol_hash_isearch search;

	//parametrs
    match_params mp;

    memset(&mp,0,sizeof(match_params));

    while ( (c = getopt(argc, argv, "f:m:b:v")) != -1 )
    {
        switch(c)
        {
        case 'f':
                mp.fname = optarg;
            break;
        case 'm':
                mp.match_str = optarg;
                mp.match_len = strlen(optarg);
            break;
        case 'b':
                mp.buf_size = atoi(optarg);
            break;
        case 'v':
                mp.verbose = 1;
            break;
        default:
            helper( argv[0] );
            exit(1);
        }
    }

    if (argc<2)
    {
        helper(argv[0]);
        return -1;
    }
    
    
    if (mp.fname == NULL)
    {
        printf("Set filename\n");
        return -1;
    }
    
    if (mp.match_str == NULL)
    {
        printf("Set search string\n");
        return -1;
    }
    
    fd = file_open( mp.fname );
    if (fd<0)
    {
        printf("Couldnt open file %s\n", mp.fname);
        
        return -1;
    }

    text_len = lseek(fd,0,SEEK_END);
    lseek(fd, 0,SEEK_SET);
    text = malloc(text_len);
    //printf("Read %d bytes\n",read(fd,text,text_len));

    ret = read(fd, text, text_len);
    if (mp.verbose)
    {
        printf("Read %d bytes\n",ret);
    }


    rlsi_hash_init( &search, 10, 13);
    while ((i = rlsi_hash_search(&search, mp.match_str, mp.match_len, text, text_len)) == 1)
    {
      //printf("Found smth %d\n",rlsi_hash_get(&search));
        printf("%d\n",rlsi_hash_get(&search));
    }

    file_close( fd );

	return 0;
}