FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ESP32-Text-Reader/src/bookmark.cpp at main · UnsignedArduino/ESP32-Text-Reader · GitHub
UnsignedArduino
/
ESP32-Text-Reader
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ESP32-Text-Reader
/
src
/
bookmark.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
107 lines (105 loc) · 3.39 KB
Breadcrumbs
ESP32-Text-Reader
/
src
/
bookmark.cpp
Copy path
File metadata and controls
107 lines (105 loc) · 3.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
Arduino.h
>
#
include
<
SdFat.h
>
#
include
<
ArduinoJson.h
>
#
include
"
sdUtils.h
"
#
include
"
bookmark.h
"
bool
getLastPage
(
const
char
* path,
unsigned
long
long
size,
unsigned
long
crc32,
unsigned
long
&result) {
char
bookmarkPath[
MAX_PATH_LEN
];
memset
(bookmarkPath,
0
,
MAX_PATH_LEN
);
strncat
(bookmarkPath,
BOOKMARK_DIR_PATH
,
MAX_PATH_LEN
);
strncat
(bookmarkPath,
"
/
"
,
MAX_PATH_LEN
);
strncat
(bookmarkPath, path,
MAX_PATH_LEN
);
Serial.
print
(
"
Looking for bookmark file at
"
);
Serial.
println
(bookmarkPath);
if
(sd.
exists
(bookmarkPath)) {
Serial.
print
(
"
Opening file
"
);
Serial.
print
(bookmarkPath);
Serial.
println
(
"
for bookmark
"
);
FsFile bookmarkFile = sd.
open
(bookmarkPath);
if
(!bookmarkFile) {
Serial.
println
(
"
Failed to open bookmark file for writing!
"
);
return
false
;
}
StaticJsonDocument<
384
> doc;
DeserializationError error =
deserializeJson
(doc, bookmarkFile);
bookmarkFile.
close
();
if
(error) {
Serial.
print
(
"
Failed to deserialize JSON:
"
);
Serial.
println
(error.
c_str
());
return
false
;
}
const
char
* jsonPath = doc[
"
path
"
];
unsigned
long
long
jsonSize = doc[
"
size
"
];
unsigned
long
jsonCrc32 = doc[
"
crc32
"
];
unsigned
long
jsonPage = doc[
"
page
"
];
if
(
strcasecmp
(path, jsonPath) !=
0
) {
Serial.
print
(
"
Paths are different! (
"
);
Serial.
print
(path);
Serial.
print
(
"
!=
"
);
Serial.
print
(jsonPath);
Serial.
println
(
"
)
"
);
return
false
;
}
else
if
(size != jsonSize) {
Serial.
print
(
"
Sizes are different! (
"
);
Serial.
print
(size);
Serial.
print
(
"
!=
"
);
Serial.
print
(jsonSize,
HEX
);
Serial.
println
(
"
)
"
);
return
false
;
}
else
if
(crc32 != jsonCrc32) {
Serial.
print
(
"
CRC32s are different! (0x
"
);
Serial.
print
(crc32,
HEX
);
Serial.
print
(
"
!= 0x
"
);
Serial.
print
(jsonCrc32,
HEX
);
Serial.
println
(
"
)
"
);
return
false
;
}
else
{
Serial.
print
(
"
Old page:
"
);
Serial.
println
(jsonPage);
result = jsonPage;
Serial.
println
(
"
Successfully read bookmark!
"
);
return
true
;
}
}
else
{
Serial.
println
(
"
No last page has been set!
"
);
return
false
;
}
}
bool
setLastPage
(
const
char
* path,
unsigned
long
long
size,
unsigned
long
crc32,
unsigned
long
page) {
char
bookmarkPath[
MAX_PATH_LEN
];
memset
(bookmarkPath,
0
,
MAX_PATH_LEN
);
strncat
(bookmarkPath,
BOOKMARK_DIR_PATH
,
MAX_PATH_LEN
);
strncat
(bookmarkPath,
"
/
"
,
MAX_PATH_LEN
);
strncat
(bookmarkPath, path,
MAX_PATH_LEN
);
if
(!sd.
exists
(
BOOKMARK_DIR_PATH
)) {
Serial.
print
(
"
Bookmark directory
"
);
Serial.
print
(
BOOKMARK_DIR_PATH
);
Serial.
println
(
"
does not exist, creating it!
"
);
sd.
mkdir
(
BOOKMARK_DIR_PATH
);
}
Serial.
print
(
"
Opening file
"
);
Serial.
print
(bookmarkPath);
Serial.
println
(
"
for bookmark
"
);
FsFile bookmarkFile = sd.
open
(bookmarkPath,
O_WRITE
|
O_CREAT
|
O_TRUNC
);
if
(!bookmarkFile) {
Serial.
println
(
"
Failed to open bookmark file for writing!
"
);
return
false
;
}
StaticJsonDocument<
384
> doc;
Serial.
print
(
"
Path:
"
);
Serial.
println
(path);
doc[
"
path
"
] = path;
Serial.
print
(
"
Size:
"
);
Serial.
println
(size);
doc[
"
size
"
] = size;
Serial.
print
(
"
CRC32: 0x
"
);
Serial.
println
(crc32,
HEX
);
doc[
"
crc32
"
] = crc32;
Serial.
print
(
"
Last page:
"
);
Serial.
println
(page);
doc[
"
page
"
] = page;
serializeJson
(doc, bookmarkFile);
bookmarkFile.
close
();
Serial.
println
(
"
Successfully written bookmark!
"
);
return
true
;
}
Back
|
FazBrowse Home
|
New Git URL