-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_funcs.cpp
More file actions
executable file
·105 lines (84 loc) · 2.35 KB
/
string_funcs.cpp
File metadata and controls
executable file
·105 lines (84 loc) · 2.35 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "string_funcs.h"
#include <iostream>
int strlen(const char* str)
{
int i = 0;
while (str[i++]);
return i - 1;
}
int strcmp(const char *str1, const char *str2)
{
int i = 0;
for ( ; str1[i] == str2[i] ; i++)
if (!str1[i])
return 0;
return str1[i] - str2[i];
}
void strcpy(char* str1, const char* str2)
{
int i = 0;
while (str1[i] = str2[i]) {
i++;
}
}
void get_first_string(char* first_string, const char buf[])
{
int i = 0;
while ( i < 256 && buf[i] != '\0' && buf[i] != ' ' && buf[i] != '\n') {
first_string[i] = buf[i];
i++;
}
first_string[i] = '\0';
}
/* function that takes a string (2nd parameter) with multiple sub-strings in it, and copies the n-th string (n is the 3rd parameter)
in anoter string (1st parameter). Returns true if it manages to make the copy, else returns false if it fails for some reason. */
bool get_nth_string(char* str, const char buf[], int n)
{
int i = 0;
for (int j = 1; j <= n - 1; j++) {
while (i < 256 && buf[i] != ' ' && buf[i] != '\t' && buf[i] != '\n' && buf[i] != '\0') {
i++;
}
if (i >= 256 || (buf[i] != ' ' && buf[i] != '\t')) {
return false;
}
while (i < 256 && (buf[i] == ' ' || buf[i] == '\t')) {
i++;
}
}
if (i >= 256 || (buf[i] == '\n' || buf[i] == '\0')) {
return false;
}
int k = 0;
while (i < 256 && buf[i] != ' ' && buf[i] != '\t' && buf[i] != '\n' && buf[i] != '\0') {
str[k] = buf[i];
i++;
k++;
}
str[k] = '\0';
return true;
}
/* function used to read the record fields from a buffer. Returns false is it fails to copy all the sub-strings of the buffer
to the correspoing strings. Else, if the copy is successful, returns true. */
bool get_record(char* key, char* firstname, char* lastname, char* year, char* sex, char* postcode, const char buf[])
{
if (get_nth_string(key, buf, 2) == false) {
return false;
}
if (get_nth_string(firstname, buf, 3) == false) {
return false;
}
if (get_nth_string(lastname, buf, 4) == false) {
return false;
}
if (get_nth_string(year, buf, 5) == false) {
return false;
}
if (get_nth_string(sex, buf, 6) == false) {
return false;
}
if (get_nth_string(postcode, buf, 7) == false) {
return false;
}
return true;
}