Skip to content

Commit aa77951

Browse files
committed
edit code style
1 parent 8677953 commit aa77951

File tree

1 file changed

+105
-125
lines changed

1 file changed

+105
-125
lines changed

main.cpp

Lines changed: 105 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,15 @@ void listFiles() {
197197
*
198198
* @author Pascal Gollor (http://www.pgollor.de/cms/)
199199
*/
200-
bool dirExists(const char* path)
201-
{
202-
DIR *d = opendir(path);
200+
bool dirExists(const char* path) {
201+
DIR *d = opendir(path);
203202

204-
if (d)
205-
{
206-
closedir(d);
207-
return true;
208-
}
203+
if (d) {
204+
closedir(d);
205+
return true;
206+
}
209207

210-
return false;
208+
return false;
211209
}
212210

213211
/**
@@ -218,31 +216,30 @@ bool dirExists(const char* path)
218216
*
219217
* @author Pascal Gollor (http://www.pgollor.de/cms/)
220218
*/
221-
bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath)
222-
{
223-
u8_t buffer[spiffsFile->size];
224-
std::string filename = (const char*)(spiffsFile->name);
219+
bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath) {
220+
u8_t buffer[spiffsFile->size];
221+
std::string filename = (const char*)(spiffsFile->name);
225222

226-
// Open file from spiffs file system.
227-
spiffs_file src = SPIFFS_open(&s_fs, (char *)(filename.c_str()), SPIFFS_RDONLY, 0);
223+
// Open file from spiffs file system.
224+
spiffs_file src = SPIFFS_open(&s_fs, (char *)(filename.c_str()), SPIFFS_RDONLY, 0);
228225

229-
// read content into buffer
230-
SPIFFS_read(&s_fs, src, buffer, spiffsFile->size);
226+
// read content into buffer
227+
SPIFFS_read(&s_fs, src, buffer, spiffsFile->size);
231228

232-
// Close spiffs file.
233-
SPIFFS_close(&s_fs, src);
229+
// Close spiffs file.
230+
SPIFFS_close(&s_fs, src);
234231

235-
// Open file.
236-
FILE* dst = fopen(destPath, "wb");
232+
// Open file.
233+
FILE* dst = fopen(destPath, "wb");
237234

238-
// Write content into file.
239-
fwrite(buffer, sizeof(u8_t), sizeof(buffer), dst);
235+
// Write content into file.
236+
fwrite(buffer, sizeof(u8_t), sizeof(buffer), dst);
240237

241-
// Close file.
242-
fclose(dst);
238+
// Close file.
239+
fclose(dst);
243240

244241

245-
return true;
242+
return true;
246243
}
247244

248245
/**
@@ -254,71 +251,64 @@ bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath)
254251
*
255252
* todo: Do unpack stuff for directories.
256253
*/
257-
bool unpackFiles(std::string sDest)
258-
{
259-
spiffs_DIR dir;
260-
spiffs_dirent ent;
261-
262-
// Add "./" to path if is not given.
263-
if (sDest.find("./") == std::string::npos)
264-
{
265-
sDest = "./" + sDest;
266-
}
267-
268-
// Check if directory exists. If it does not then try to create it with permissions 755.
269-
if (! dirExists(sDest.c_str()))
270-
{
271-
std::cout << "Directory " << sDest << " does not exists. Try to create it." << std::endl;
272-
273-
// Try to create directory.
274-
// platform stuff...
254+
bool unpackFiles(std::string sDest) {
255+
spiffs_DIR dir;
256+
spiffs_dirent ent;
257+
258+
// Add "./" to path if is not given.
259+
if (sDest.find("./") == std::string::npos) {
260+
sDest = "./" + sDest;
261+
}
262+
263+
// Check if directory exists. If it does not then try to create it with permissions 755.
264+
if (! dirExists(sDest.c_str())) {
265+
std::cout << "Directory " << sDest << " does not exists. Try to create it." << std::endl;
266+
267+
// Try to create directory.
268+
// platform stuff...
275269
#if defined(_WIN32)
276-
if (_mkdir(sDest.c_str()) != 0)
270+
if (_mkdir(sDest.c_str()) != 0) {
277271
#else
278-
if (mkdir(sDest.c_str(), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0)
272+
if (mkdir(sDest.c_str(), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
279273
#endif
280-
{
281-
std::cerr << "Can not create directory!!!" << std::endl;
282-
return false;
283-
}
284-
}
285-
286-
// Open directory.
287-
SPIFFS_opendir(&s_fs, 0, &dir);
288-
289-
// Read content from directory.
290-
spiffs_dirent* it = SPIFFS_readdir(&dir, &ent);
291-
while (it)
292-
{
293-
// Check if content is a file.
294-
if ((int)(it->type) == 1)
295-
{
296-
std::string sDestFilePath = sDest + (const char*)(it->name);
297-
298-
// Unpack file to destination directory.
299-
if (! unpackFile(it, sDestFilePath.c_str()) )
300-
{
301-
std::cout << "Can not unpack " << it->name << "!" << std::endl;
302-
return false;
303-
}
304-
305-
// Output stuff.
306-
std::cout
307-
<< it->name
308-
<< '\t'
309-
<< " > " << sDestFilePath
310-
<< '\t'
311-
<< "size: " << it->size << " Bytes"
312-
<< std::endl;
313-
}
314-
315-
it = SPIFFS_readdir(&dir, &ent);
316-
}
317-
318-
// Close directory.
319-
SPIFFS_closedir(&dir);
320-
321-
return true;
274+
std::cerr << "Can not create directory!!!" << std::endl;
275+
return false;
276+
}
277+
}
278+
279+
// Open directory.
280+
SPIFFS_opendir(&s_fs, 0, &dir);
281+
282+
// Read content from directory.
283+
spiffs_dirent* it = SPIFFS_readdir(&dir, &ent);
284+
while (it) {
285+
// Check if content is a file.
286+
if ((int)(it->type) == 1) {
287+
std::string sDestFilePath = sDest + (const char*)(it->name);
288+
289+
// Unpack file to destination directory.
290+
if (! unpackFile(it, sDestFilePath.c_str()) ) {
291+
std::cout << "Can not unpack " << it->name << "!" << std::endl;
292+
return false;
293+
}
294+
295+
// Output stuff.
296+
std::cout
297+
<< it->name
298+
<< '\t'
299+
<< " > " << sDestFilePath
300+
<< '\t'
301+
<< "size: " << it->size << " Bytes"
302+
<< std::endl;
303+
}
304+
305+
it = SPIFFS_readdir(&dir, &ent);
306+
}
307+
308+
// Close directory.
309+
SPIFFS_closedir(&dir);
310+
311+
return true;
322312
}
323313

324314
// Actions
@@ -348,37 +338,35 @@ int actionPack() {
348338
*
349339
* @author Pascal Gollor (http://www.pgollor.de/cms/)
350340
*/
351-
int actionUnpack(void)
352-
{
353-
int ret = 0;
354-
s_flashmem.resize(s_imageSize, 0xff);
341+
int actionUnpack(void) {
342+
int ret = 0;
343+
s_flashmem.resize(s_imageSize, 0xff);
355344

356-
// open spiffs image
357-
FILE* fdsrc = fopen(s_imageName.c_str(), "rb");
358-
if (!fdsrc) {
359-
std::cerr << "error: failed to open image file" << std::endl;
360-
return 1;
361-
}
345+
// open spiffs image
346+
FILE* fdsrc = fopen(s_imageName.c_str(), "rb");
347+
if (!fdsrc) {
348+
std::cerr << "error: failed to open image file" << std::endl;
349+
return 1;
350+
}
362351

363-
// read content into s_flashmem
364-
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc);
352+
// read content into s_flashmem
353+
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc);
365354

366-
// close fiel handle
367-
fclose(fdsrc);
355+
// close fiel handle
356+
fclose(fdsrc);
368357

369-
// mount file system
370-
spiffsMount();
358+
// mount file system
359+
spiffsMount();
371360

372-
// unpack files
373-
if (! unpackFiles(s_dirName))
374-
{
375-
ret = 1;
376-
}
361+
// unpack files
362+
if (! unpackFiles(s_dirName)) {
363+
ret = 1;
364+
}
377365

378-
// unmount file system
379-
spiffsUnmount();
366+
// unmount file system
367+
spiffsUnmount();
380368

381-
return ret;
369+
return ret;
382370
}
383371

384372

@@ -440,22 +428,15 @@ void processArgs(int argc, const char** argv) {
440428
cmd.add( outNameArg );
441429
cmd.parse( argc, argv );
442430

443-
if (packArg.isSet())
444-
{
431+
if (packArg.isSet()) {
445432
s_dirName = packArg.getValue();
446433
s_action = ACTION_PACK;
447-
}
448-
else if (unpackArg.isSet())
449-
{
434+
} else if (unpackArg.isSet()) {
450435
s_dirName = unpackArg.getValue();
451436
s_action = ACTION_UNPACK;
452-
}
453-
else if (listArg.isSet())
454-
{
437+
} else if (listArg.isSet()) {
455438
s_action = ACTION_LIST;
456-
}
457-
else if (visualizeArg.isSet())
458-
{
439+
} else if (visualizeArg.isSet()) {
459440
s_action = ACTION_VISUALIZE;
460441
}
461442

@@ -474,8 +455,7 @@ int main(int argc, const char * argv[]) {
474455
return 1;
475456
}
476457

477-
switch (s_action)
478-
{
458+
switch (s_action) {
479459
case ACTION_PACK:
480460
return actionPack();
481461
break;

0 commit comments

Comments
 (0)