Skip to content

Commit df9c8b6

Browse files
committed
Simplify code by using guard clauses
1 parent 7024428 commit df9c8b6

File tree

1 file changed

+58
-56
lines changed

1 file changed

+58
-56
lines changed

src/Utils.h

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,69 +24,71 @@
2424

2525

2626
[[gnu::unused]] static bool copyFolder(const char* source, const char* destination) {
27-
DIR* dir = opendir(source);
28-
if (dir == nullptr) {
29-
printf("Failed to open source directory\n");
30-
return false;
27+
DIR* dir = opendir(source);
28+
if (dir == nullptr) {
29+
printf("Failed to open source directory\n");
30+
return false;
31+
}
32+
33+
// Create destination directory if it doesn't exist
34+
if (mkdir(destination, 0777) != 0 && errno != EEXIST) {
35+
printf("Failed to create destination directory\n");
36+
closedir(dir);
37+
return false;
38+
}
39+
40+
struct dirent* entry;
41+
while ((entry = readdir(dir)) != nullptr) {
42+
// Skip "." and ".."
43+
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
44+
continue;
3145
}
3246

33-
// Create destination directory if it doesn't exist
34-
if (mkdir(destination, 0777) != 0 && errno != EEXIST) {
35-
printf("Failed to create destination directory\n");
47+
char sourcePath[PATH_MAX_LENGTH];
48+
snprintf(sourcePath, PATH_MAX_LENGTH, "%s/%s", source, entry->d_name);
49+
50+
char destinationPath[PATH_MAX_LENGTH];
51+
snprintf(destinationPath, PATH_MAX_LENGTH, "%s/%s", destination, entry->d_name);
52+
53+
struct stat fileInfo;
54+
if (stat(sourcePath, &fileInfo) != 0) {
55+
closedir(dir);
56+
return false;
57+
}
58+
59+
if (S_ISDIR(fileInfo.st_mode)) {
60+
// Recursively copy subdirectories
61+
if (!copyFolder(sourcePath, destinationPath)) {
3662
closedir(dir);
3763
return false;
38-
}
64+
}
65+
} else {
66+
// Copy regular files
67+
FILE* sourceFile = fopen(sourcePath, "r");
68+
if (sourceFile == nullptr) {
69+
closedir(dir);
70+
return false;
71+
}
3972

40-
struct dirent* entry;
41-
while ((entry = readdir(dir)) != nullptr) {
42-
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
43-
char sourcePath[PATH_MAX_LENGTH];
44-
snprintf(sourcePath, PATH_MAX_LENGTH, "%s/%s", source, entry->d_name);
45-
46-
char destinationPath[PATH_MAX_LENGTH];
47-
snprintf(destinationPath, PATH_MAX_LENGTH, "%s/%s", destination, entry->d_name);
48-
49-
struct stat fileInfo;
50-
if (stat(sourcePath, &fileInfo) != 0) {
51-
closedir(dir);
52-
return false;
53-
}
54-
55-
if (S_ISDIR(fileInfo.st_mode)) {
56-
// Recursively copy subdirectories
57-
if (!copyFolder(sourcePath, destinationPath)) {
58-
closedir(dir);
59-
return false;
60-
}
61-
} else {
62-
// Copy regular files
63-
FILE* sourceFile = fopen(sourcePath, "r");
64-
if (sourceFile == nullptr) {
65-
66-
closedir(dir);
67-
return false;
68-
}
69-
70-
FILE* destinationFile = fopen(destinationPath, "w");
71-
if (destinationFile == nullptr) {
72-
fclose(sourceFile);
73-
closedir(dir);
74-
return false;
75-
}
76-
77-
int c;
78-
while ((c = fgetc(sourceFile)) != EOF) {
79-
fputc(c, destinationFile);
80-
}
81-
82-
fclose(sourceFile);
83-
fclose(destinationFile);
84-
}
85-
}
73+
FILE* destinationFile = fopen(destinationPath, "w");
74+
if (destinationFile == nullptr) {
75+
fclose(sourceFile);
76+
closedir(dir);
77+
return false;
78+
}
79+
80+
int c;
81+
while ((c = fgetc(sourceFile)) != EOF) {
82+
fputc(c, destinationFile);
83+
}
84+
85+
fclose(sourceFile);
86+
fclose(destinationFile);
8687
}
88+
}
8789

88-
closedir(dir);
89-
return true;
90+
closedir(dir);
91+
return true;
9092
}
9193

9294
[[gnu::unused]] static std::string replaceLastPathComponent(const std::string& path, const std::string& newComponent) {

0 commit comments

Comments
 (0)