-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.h
100 lines (92 loc) · 2.29 KB
/
FileSystem.h
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
#ifndef FILE_SYSTEM_H_
#define FILE_SYSTEM_H_
//SD Constants
const int chipSelect = 4;
//Global Files
File dataFile;
File configFile;
File nameFile;
void startSD();
void startFileSystem();
int getLine(File& file, char* buffer, int length);
char* findChar(char* buffer, const int start,const int length,const char target);
char* tabOver(char* buffer, const int length, const int numberOfTabs = 1);
///////////////////////////////////////////
//////////////FileSystem.cpp///////////////
///////////////////////////////////////////
void startSD()
{
pinMode(10, OUTPUT);
SD.begin(chipSelect);
}
void startFileSystem()
{
startSD();
dataFile = SD.open("Data.txt", FILE_WRITE);
dataFile.close();
configFile = SD.open("Config.txt", FILE_WRITE);//Create if it doesn't exist
configFile.close();
configFile = SD.open("Config.txt");//Open in read mode
nameFile = SD.open("Names.txt", FILE_WRITE);//Create if it doesn't exist
nameFile.close();
nameFile = SD.open("Names.txt");//Open in read mode
}
int getLine(File& file, char* buffer, int length)
{
char* endbuf = buffer+length;
char* begbuf = buffer;
for(; buffer<=endbuf; ++buffer)
{
if(file.available())
{
*buffer = file.read();
if(*buffer == '\n')
{
break;
}
}
else
{
break;
}
}
return buffer - begbuf;
}
//Returns first position of a character in an array from start to ending
//Returns negative for unsuccesful find
char* findChar(char* buffer, const int start,const int length,const char target)
{
char* begbuf = buffer + start;
char* endbuf = buffer + length;
for(;*begbuf != target; ++begbuf)
{
if(begbuf == endbuf)
{
return NULL;
}
}
return begbuf;
}
//Moves 1 past a tab character in an array
//Can move past multiple tabs at once
char* tabOver(char* buffer, const int length, const int numberOfTabs)
{
int start = 0;
char* seeker = buffer;
char* endbuf = buffer + length;
for(int tabs = 0; tabs < numberOfTabs; ++tabs)
{
seeker = findChar(buffer, start, length, '\t');
if(!(seeker))
{
return NULL;
}
else if(seeker < endbuf)
{
++seeker; //move 1 past tab
}
start = seeker - buffer; //move the start of the next search
}
return seeker;
}
#endif