Skip to content

Commit 6f54fd4

Browse files
committed
Update to 0.9.5
Changelog: * New confirmation message before making changes. * Fixed memory leaks. * Refactored code.
1 parent 309b48b commit 6f54fd4

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<h1 align="center">Nmly</h1>
22

33
<p align="center">
4-
<img src="https://img.shields.io/badge/version-0.9.4-blue.svg"> <img src="https://img.shields.io/badge/license-MIT-orange.svg">
4+
<img src="https://img.shields.io/badge/version-0.9.5-blue.svg"> <img src="https://img.shields.io/badge/license-MIT-orange.svg">
55
</p>
66

77
<h4 align="center">Nmly is a massive file renamer utility with useful functions and written in C.</h4>

helper.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,28 @@ char *changeCases(const char *dir, const char *filename, const int upper)
180180
char *name = strBefore(filename, '.');
181181
char *extension = strAfter(filename, '.');
182182
char *new = malloc((strlen(dir) + strlen(filename) + 2) * sizeof(char));
183-
char *cases = malloc(strlen(filename) * sizeof(char));
183+
char *cases;
184184

185185
strcpy(new, dir);
186186
strcat(new, "/");
187187

188188
//Without extension
189189
if (name == NULL) {
190+
cases = malloc(strlen(filename) + 1 * sizeof(char));
190191
strCases(cases, filename, upper);
191192
strcat(new, cases);
192193
//With extension
193194
} else {
195+
cases = malloc(strlen(name) + 1 * sizeof(char));
194196
strCases(cases, name, upper);
195197
strcat(new, cases);
196198
strcat(new, ".");
197199
strcat(new, extension);
198200
}
199201

200202
free(cases);
203+
free(name);
204+
free(extension);
201205

202206
return new;
203207
}
@@ -228,15 +232,18 @@ char *replace(const char *dir, const char *filename, const char *ori, const char
228232
strcat(new, "/");
229233
strcat(new, replaced);
230234

235+
free(replaced);
236+
231237
return new;
232238
}
233239

234240

235241
char *strReplace(const char *str, const char *ori, const char *rep)
236242
{
237-
char *new = malloc(4096);
243+
char *new = malloc(BUFFER * sizeof(char));
238244
new[0] = '\0';
239-
char *remaining = malloc(strlen(str) + 1 * sizeof(char));
245+
char *remaining_ref = malloc(strlen(str) + 1 * sizeof(char));
246+
char *remaining = remaining_ref;
240247
char *pos;
241248

242249
strcpy(remaining, str);
@@ -253,6 +260,8 @@ char *strReplace(const char *str, const char *ori, const char *rep)
253260

254261
strcat(new, remaining);
255262

263+
free(remaining_ref);
264+
256265
return new;
257266
}
258267

@@ -299,6 +308,7 @@ char *switchSides(const char *dir, const char *filename, const char sep)
299308

300309
doSwitch(new, tmp, part_one, part_two);
301310

311+
free(name);
302312
free(part_one);
303313
free(part_two);
304314

@@ -319,6 +329,8 @@ char *switchSides(const char *dir, const char *filename, const char sep)
319329
strcat(new, ".");
320330
strcat(new, extension);
321331

332+
free(extension);
333+
free(name);
322334
free(part_one);
323335
free(part_two);
324336

helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef HELPER_H_
22
#define HELPER_H_
3+
#define BUFFER 512
34

45
/**
56
* Copy a portion of the specified string into a new one.

nmly.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@
99
#include "nmly.h"
1010
#include "helper.h"
1111

12-
#define BEFORE 0
13-
#define AFTER 1
14-
#define REPLACE 2
15-
#define UPPER 3
16-
#define LOWER 4
17-
#define SWITCH 5
18-
#define REVERSE 6
19-
#define REMOVE 7
20-
2112
const char *preview_msg = "\n%i File(s) to be modified in %i folder(s)";
2213
const char *success_msg = "\n%i File(s) modified in %i folder(s)";
2314
const char *dir_error_msg = "Cannot open directory %s\n";
15+
const char *dir_confirm_msg = "Apply the changes in the following directory '%s'? [Y/n] ";
2416
const char *compare_msg = "%s > %s \n";
2517
const char *time_msg = "\n%f Segs\n";
2618
const char *arg_error_msg = "Error: Invalid command\n";
27-
const char *version_msg = "Nmly v0.9.4\n";
19+
const char *version_msg = "Nmly v0.9.5\n";
2820
char *working_path = ".";
2921
char *filter = "";
3022
int files_n = 0, folders_n = 0;
@@ -56,7 +48,7 @@ char *getChanges(char *path, char *argv[])
5648
{
5749
char *dir = strBefore(path, '/');
5850
char *filename = strAfter(path, '/');
59-
char *new_path = path;
51+
char *new_path = NULL;
6052

6153
//Matches filter by extension
6254
char *extension = strAfter(filename, '.');
@@ -93,7 +85,7 @@ char *getChanges(char *path, char *argv[])
9385
void listDir(char *basedir, char *argv[])
9486
{
9587
DIR *dir;
96-
char b[512];
88+
char b[BUFFER];
9789
struct dirent *ent;
9890

9991
if ((dir = opendir(basedir)) == NULL) {
@@ -135,6 +127,8 @@ void processFile(char *entpath, char *argv[])
135127
char *new_path = getChanges(entpath, argv);
136128

137129
if (new_path == NULL) {
130+
free(new_path);
131+
138132
return;
139133
}
140134

@@ -274,11 +268,23 @@ int main(int argc, char *argv[])
274268

275269
float start_time = (float) clock() / CLOCKS_PER_SEC;
276270

271+
//Confirmation
272+
if (!preview) {
273+
char confirm;
274+
275+
printf(dir_confirm_msg, working_path);
276+
scanf("%c", &confirm);
277+
278+
if (confirm != 'Y' && confirm != 'y') {
279+
return 0;
280+
}
281+
}
282+
277283
listDir(working_path, argv);
278284

279285
float total_time = ((float) clock() / CLOCKS_PER_SEC) - start_time;
280286

281-
const char *msg = (preview == 1) ? preview_msg : success_msg;
287+
const char *msg = (preview) ? preview_msg : success_msg;
282288
printf(msg, files_n, folders_n);
283289
printf(time_msg, total_time);
284290

nmly.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#ifndef NMLY_H_
22
#define NMLY_H_
33

4+
#define BEFORE 0
5+
#define AFTER 1
6+
#define REPLACE 2
7+
#define UPPER 3
8+
#define LOWER 4
9+
#define SWITCH 5
10+
#define REVERSE 6
11+
#define REMOVE 7
12+
413
/**
514
* Returns 1 if the specified path is a file, 0 otherwise.
615
* @param path the path.
@@ -33,6 +42,7 @@ void listDir(char *basedir, char *argv[]);
3342
/**
3443
* Process the given file
3544
* @param entpath the file path.
45+
* @param argv the arguments array.
3646
*/
3747
void processFile(char *entpath, char *argv[]);
3848

@@ -48,8 +58,6 @@ int mapArgs(int argc, char *argv[]);
4858

4959
/**
5060
* Display the help text.
51-
* @param test blah blah parameter.
52-
* @return 0 if blah blah passed.
5361
*/
5462
void help();
5563

0 commit comments

Comments
 (0)