Misc optimisation

This commit is contained in:
dogeystamp 2022-05-23 11:13:13 -04:00
parent ed4d92aae6
commit 74e1f5484a
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
3 changed files with 22 additions and 17 deletions

View File

@ -28,17 +28,22 @@ itemAction(itemStruct *item, const char *folder)
unsigned long long int newItems = 0;
size_t folderLen = strlen(folder);
size_t extLen = strlen(fileExt);
while (cur) {
char *filePath;
char *fileName = san(cur->title, 1);
size_t fileNameLen = strlen(fileName);
itemStruct *prev = cur;
// +1 for null terminator and +1 for path separator
size_t pathLen = folderLen + fileNameLen + extLen + 2;
if (fileName[0])
filePath = ecalloc(
strlen(folder)
+ strlen(fileName) + 2
+ strlen(fileExt),
sizeof(char));
filePath = ecalloc(pathLen, sizeof(char));
else {
logMsg(1, "Invalid article title.\n");
@ -48,16 +53,15 @@ itemAction(itemStruct *item, const char *folder)
continue;
}
strcpy(filePath, folder);
memcpy(filePath, folder, folderLen * sizeof(char));
unsigned long long int ind = strlen(filePath);
filePath[ind] = fsep();
filePath[ind + 1] = '\0';
filePath[folderLen] = fsep();
filePath[pathLen] = '\0';
strcat(filePath, fileName);
memcpy(filePath + folderLen + 1, fileName, fileNameLen * sizeof(char));
free(fileName);
strcat(filePath, fileExt);
memcpy(filePath + pathLen - extLen - 1, fileExt, extLen * sizeof(char));
FILE *itemFile = fopen(filePath, "a");
free(filePath);

2
util.c
View File

@ -68,7 +68,7 @@ san(char *str, int rep)
len = len > 255 ? 255 : len;
char *dup = ecalloc(len + 1, sizeof(char));
strcpy(dup, str);
memcpy(dup, str, (len + 1) * sizeof(char));
for (unsigned long long int i = 0; i < len; i++) {
char c = dup[i];

11
xml.c
View File

@ -182,9 +182,9 @@ parseXml(xmlDocPtr doc,
if (itemKey) {
for (unsigned long int i = 0; i < LEN(atts); i++) {
if (TAGIS(itemNode, attKeys[i])) {
*atts[i] = ecalloc(strlen(itemKey) + 1, sizeof(char));
strcpy(*atts[i], itemKey);
size_t keyLen = strlen(itemKey) + 1;
*atts[i] = ecalloc(keyLen, sizeof(char));
memcpy(*atts[i], itemKey, keyLen * sizeof(char));
break;
}
@ -205,8 +205,9 @@ parseXml(xmlDocPtr doc,
return 1;
}
item->link = ecalloc(strlen((char *) link) + 1, sizeof(char));
strcpy(item->link, (char *) link);
size_t linkLen = strlen((char *) link) + 1;
item->link = ecalloc(linkLen, sizeof(char));
memcpy(item->link, (char *) link, linkLen * sizeof(char));
xmlFree(link);
}