-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileParse.cpp
More file actions
144 lines (135 loc) · 3.05 KB
/
Copy pathfileParse.cpp
File metadata and controls
144 lines (135 loc) · 3.05 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
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
/*
Sudoku File Parser
Written by David Wiebe
*/
#include <string.h>
#include <unistd.h>
#include <fstream>
#include "sudoku.h"
#include "fileParse.h"
Sudoku *sudokuFromFile(const char *filePath)
{
char *file = loadText(filePath);
replaceChar(file, '\n', ' ');
const char **tokens = (const char **)split(file, " ");
int *numbers = selectNumbers(tokens);
int size;
for (size = 0; isNumber(tokens[size]); size++)
;
Sudoku *sudoku = new Sudoku(size, numbers);
delete[] file;
for (int i = 0; tokens[i] != nullptr; i++)
{
delete[] tokens[i];
}
delete[] tokens;
delete[] numbers;
return sudoku;
}
bool fileExists(const char *filePath)
{
return (access(filePath, F_OK) != -1);
}
char **split(const char *input, const char *delimiter)
{
int delimiterLength = strlen(delimiter);
char **output = new char *[100000];
int count = 0;
int runLength = 0;
while (*input != 0)
{
if (match(input, delimiter))
{
if (runLength > 0)
{
char *token = new char[runLength + 1];
output[count] = token;
memccpy(token, input - runLength, 0, runLength);
token[runLength] = 0;
count++;
runLength = 0;
input += delimiterLength;
}
else
{
input += delimiterLength;
}
}
else
{
input++;
runLength++;
}
}
char *token = new char[runLength + 1];
output[count] = token;
memccpy(token, input - runLength, 0, runLength);
token[runLength] = 0;
count++;
output[count] = NULL;
return output;
}
bool match(const char *source, const char *compair)
{
while (*compair != 0)
{
if (*source != *compair)
{
return false;
}
source++;
compair++;
}
return true;
}
void replaceChar(char *input, char find, char replace)
{
for (; *input != 0; input++)
{
if (*input == find)
*input = replace;
}
}
char *loadText(const char *filePath)
{
FILE *f = fopen(filePath, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *string = new char[fsize + 1];
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
return string;
}
int *selectNumbers(const char **tokens)
{
int *numbers = new int[100000];
int foundNumbers = 0;
for (int i = 0; tokens[i] != nullptr; i++)
{
const char *string = tokens[i];
if (isNumber(string))
{
numbers[foundNumbers] = atoi(string);
foundNumbers++;
}
}
return numbers;
}
bool isNumber(const char *string)
{
int i = 0;
if (isdigit(string[0]) == false && (string[0] != '-' || isdigit(string[1]) == false))
{
return false;
}
for (int i = 1; string[i] != 0; i++)
{
if (isdigit(string[i]) == false)
{
return false;
}
}
return true;
}