-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtils.h
218 lines (182 loc) · 6 KB
/
Utils.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef Utils_H
#define Utils_H
#define PATH_MAX_LENGTH 255
#include "Arduino.h"
#include "Arduino_UnifiedStorage.h"
#include "Arduino_POSIXStorage.h"
#include <iostream>
#if !defined(HAS_SERIAL) && defined(HAS_RS485) && __has_include(<ArduinoRS485.h>)
#include <ArduinoRS485.h>
[[gnu::unused]] static void beginRS485(const int baudrate){
const auto bitduration { 1.f / baudrate };
const auto wordlen { 9.6f }; // OR 10.0f depending on the channel configuration
const auto preDelayBR { bitduration * wordlen * 3.5f * 1e6 };
const auto postDelayBR { bitduration * wordlen * 3.5f * 1e6 };
RS485.begin(baudrate);
RS485.setDelays(preDelayBR, postDelayBR);
RS485.noReceive();
}
[[gnu::unused]] static void debugPrintRS485(String s){
static bool rs485Initialized = false;
if (!rs485Initialized) {
beginRS485(115200);
rs485Initialized = true;
}
RS485.beginTransmission();
RS485.write(s.c_str(), strlen(s.c_str()));
RS485.write('\n');
RS485.endTransmission();
}
#endif
[[gnu::unused]] static String prettyPrintFileSystemType(FileSystems f){
if(f == 0) return "FAT";
else if(f == 1) return "LittleFS";
else return "";
}
[[gnu::unused]] static const char* createPartitionName(int number) {
// check if the number can be represented by a letter of the alphabet
if (number < 1 || number > 26) {
// Handle out-of-range numbers or errors as needed
return nullptr;
}
char partitionName[13]; // "partition_" + letter + '\0'
snprintf(partitionName, sizeof(partitionName), "%c", 'a' + number - 1);
// Dynamically allocate memory for the string and copy the generated value
char* dynamicName = new char[strlen(partitionName) + 1];
strcpy(dynamicName, partitionName);
return dynamicName;
}
[[gnu::unused]] static bool copyFolder(const char* source, const char* destination) {
DIR* dir = opendir(source);
if (dir == nullptr) {
printf("Failed to open source directory\n");
return false;
}
// Create destination directory if it doesn't exist
if (mkdir(destination, 0777) != 0 && errno != EEXIST) {
printf("Failed to create destination directory\n");
closedir(dir);
return false;
}
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
// Skip "." and ".."
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
size_t sourcePathLength = strlen(source) + strlen(entry->d_name) + 1;
size_t destinationPathLength = strlen(destination) + strlen(entry->d_name) + 1;
char* sourcePath = new char[sourcePathLength];
char* destinationPath = new char[destinationPathLength];
snprintf(sourcePath, sourcePathLength, "%s/%s", source, entry->d_name);
snprintf(destinationPath, destinationPathLength, "%s/%s", destination, entry->d_name);
struct stat fileInfo;
if (stat(sourcePath, &fileInfo) != 0) {
closedir(dir);
delete(sourcePath);
delete(destinationPath);
return false;
}
if (S_ISDIR(fileInfo.st_mode)) {
// Recursively copy subdirectories
if (!copyFolder(sourcePath, destinationPath)) {
closedir(dir);
delete(sourcePath);
delete(destinationPath);
return false;
}
} else {
// Copy regular files
FILE* sourceFile = fopen(sourcePath, "r");
if (sourceFile == nullptr) {
closedir(dir);
delete(sourcePath);
delete(destinationPath);
return false;
}
FILE* destinationFile = fopen(destinationPath, "w");
if (destinationFile == nullptr) {
fclose(sourceFile);
closedir(dir);
delete(sourcePath);
delete(destinationPath);
return false;
}
int c;
while ((c = fgetc(sourceFile)) != EOF) {
fputc(c, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
}
}
closedir(dir);
return true;
}
[[gnu::unused]] static std::string replaceLastPathComponent(const std::string& path, const std::string& newComponent) {
size_t lastSlashIndex = path.find_last_of('/');
if (lastSlashIndex != std::string::npos) {
std::string newPath = path.substr(0, lastSlashIndex + 1) + newComponent;
return newPath;
}
return path;
}
// Helper function to get the last path component
[[gnu::unused]] static std::string getLastPathComponent(const std::string& path) {
size_t lastSlashIndex = path.find_last_of('/');
if (lastSlashIndex != std::string::npos) {
return path.substr(lastSlashIndex + 1);
}
return path;
}
// Helper function to replace the first path component with a new component
[[gnu::unused]] static std::string replaceFirstPathComponent(const std::string& path, const std::string& destinationPath) {
std::string fileName = getLastPathComponent(path);
std::string newPath = destinationPath + "/" + fileName;
return newPath;
}
inline static String getErrno(){
switch (errno) {
case ENOENT:
return "No such file or directory";
case EEXIST:
return "File or directory already exists";
case EIO:
return "Input/output error";
case ENOTDIR:
return "Not a directory";
case EISDIR:
return "Is a directory";
case ENFILE:
return "Too many open files in system";
case EMFILE:
return "Too many open files";
case ENOSPC:
return "No space left on device";
case ENAMETOOLONG:
return "File name too long";
case EPERM:
return "Operation not permitted";
case ENODEV:
return "No such device";
case ENOTBLK:
return "Not a block device";
case EBUSY:
return "Device or resource busy";
case EAGAIN:
return "Resource temporarily unavailable";
case ENXIO:
return "No such device or address";
case ENOMEM:
return "Out of memory";
case EACCES:
return "Permission denied";
case EROFS:
return "Read-only file system";
case EINVAL:
return "Invalid argument";
default:
return "Undefined Error";
}
}
#endif