This repository was archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
89 lines (71 loc) · 2.5 KB
/
scanner.cpp
File metadata and controls
89 lines (71 loc) · 2.5 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include "slre.h"
using namespace std;
typedef struct _addr
{
char name[50];
char street[100];
char city[50];
char state[20];
int postal;
} ADDR;
const int MAX_CAPTURES = 10;
int scanner( char *filename, ADDR &addr) {
// RegEx
struct slre regEngine;
struct cap caps[MAX_CAPTURES];
string line;
ifstream infile(filename);
if ( !infile.is_open() ) return -1;
if ( !slre_compile(®Engine, "^(\\S+)=(\\S+)")) {
cout << "[ERR] compiling RE: " << regEngine.err_str << endl;
infile.close();
return -1;
}
while (getline(infile, line)) {
if (!slre_match(®Engine, line.c_str(), strlen(line.c_str()), caps)) {
cout << "[ERR] not valid format line : " << line << endl;
} else {
cout << "full match : " << caps[0].len << "." << caps[1].ptr << endl;
cout << "key : " << caps[1].len << "." << caps[1].ptr << endl;
cout << "value : " << caps[2].len << "." << caps[2].ptr << endl;
if ( caps[2].len > 0 ) {
string key(caps[1].ptr, caps[1].len);
string value(caps[2].ptr, caps[2].len);
if ( !strncmp ( caps[1].ptr, "name", caps[1].len) ) {
strncpy( addr.name, caps[2].ptr, 50);
}
if ( !strncmp ( caps[1].ptr, "street", caps[1].len) ) {
strncpy( addr.street, caps[2].ptr, 100);
}
if ( !strncmp ( caps[1].ptr, "city", caps[1].len) ) {
strncpy( addr.city, caps[2].ptr, 50);
}
if ( !strncmp ( caps[1].ptr, "state", caps[1].len) ) {
strncpy( addr.state, caps[2].ptr, 20);
}
if ( !strncmp ( caps[1].ptr, "zip", caps[1].len) ) {
addr.postal = atoi(caps[2].ptr);
}
}
}
}
infile.close();
return 0;
}
int main(int agc, char *argv[]) {
ADDR address;
memset( &address, 0, sizeof(ADDR));
if( scanner( argv[1] , address ) != 0 ) cout << "scanner error" << endl;;
cout << endl << "RESULT ====================" << endl;
cout << "NAME : "<< address.name << endl;
cout << "STREET : "<< address.street << endl;
cout << "CITY : "<< address.city << endl;
cout << "STATE : "<< address.state << endl;
cout << "ZIP : "<< address.postal << endl;
return 0;
}