Browse Source

Got rid of asprintf

Piotr Czajkowski 5 years ago
parent
commit
cb228f60bc
3 changed files with 7 additions and 6 deletions
  1. 4 3
      fileinfo.c
  2. 1 0
      fileinfo.h
  3. 2 3
      transform.c

+ 4 - 3
fileinfo.c

@@ -7,9 +7,9 @@ struct fileInfo* newFileInfo(const char *filePath) {
 	if (!strrchr(filePath, '.')) return NULL;
 
 	struct fileInfo *info = malloc(sizeof(struct fileInfo));
-	size_t filePathLength = strlen(filePath);
-	info->fullname = calloc(filePathLength+1, 1);
-	strncpy(info->fullname, filePath, filePathLength);
+	info->fullnameLength = strlen(filePath);
+	info->fullname = calloc(info->fullnameLength+1, 1);
+	strncpy(info->fullname, filePath, info->fullnameLength);
 	
 	size_t basenameLength = strlen(basename(info->fullname));
 	info->_base = calloc(basenameLength+1, 1);
@@ -17,6 +17,7 @@ struct fileInfo* newFileInfo(const char *filePath) {
 
 	char *dot = ".";
 	info->name = strtok(info->_base, dot);
+	info->nameLength = strlen(info->name);
 	info->ext = strtok(NULL, dot);
 	if (info->name && info->ext)
 		return info;

+ 1 - 0
fileinfo.h

@@ -1,6 +1,7 @@
 struct fileInfo
 {
 	char *fullname, *name, *ext;
+	int fullnameLength, nameLength;
 	char *_base; // this is just for slicing
 };
 

+ 2 - 3
transform.c

@@ -1,4 +1,3 @@
-#define _GNU_SOURCE //asks stdio.h to include asprintf
 #include <string.h>
 #include <stdio.h>
 #include <libxml/xmlmemory.h>
@@ -35,9 +34,9 @@ int transformXML(XMLBuff *infile) {
 	result = xsltApplyStylesheet(stylesheet, doc, NULL);
 	Stopif(!result, return 0, "Transformation failed for %s!\n", infile->name);
 
-	char *outFile;
 	struct fileInfo *fi = newFileInfo(infile->name);
-	asprintf(&outFile, "%s.html", fi->name);
+	char *outFile = malloc(fi->nameLength+6);
+	snprintf(outFile, fi->nameLength+6, "%s.html", fi->name);
 	freeFileInfo(fi);
 
 	FILE *fp;