a4ab5a089968 — Duncan Ross Palmer 2 years ago
Merged rel/1 (1.3.0) into default
9 files changed, 163 insertions(+), 55 deletions(-)

M .hgtags
M COPYING
M Makefile
M README
M debian/changelog
M debian/control
M raw2c.c
M ref.c
M ref.h
M .hgtags +1 -0
@@ 4,3 4,4 @@ 3a9fc4d684dae7ff3baaa380f937eb6b5aa509e8
 a07e4bd29e0ab9d1ce90bb4f3f58d582dc0d912e raw2c-1.2.1
 58d02df06520237e086a0ab2eb5c0630c1817f45 raw2c-1.2.2
 46266b39e4a8dc5d1881a53c6b30543de2d49fd5 raw2c-1.2.2-1
+824a2ca0c245cd2a63bc4e42c39765cd0b4e7d5e raw2c-1.3.0

          
M COPYING +1 -1
@@ 1,5 1,5 @@ 
 raw2c
-Copyright (c) 2016, David Duncan Ross Palmer (2E0EOL) and others,
+Copyright (c) 2018, Duncan Ross Palmer (2E0EOL) and others,
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without

          
M Makefile +1 -1
@@ 1,7 1,7 @@ 
 #!/usr/bin/make -f
 #
 # raw2c
-# Copyright (c) 2016, David Duncan Ross Palmer (2E0EOL) and others,
+# Copyright (c) 2018, Duncan Ross Palmer (2E0EOL) and others,
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without

          
M README +1 -1
@@ 1,5 1,5 @@ 
 raw2c
-Copyright (c) 2016, David Duncan Ross Palmer (2E0EOL) and others,
+Copyright (c) 2018, Duncan Ross Palmer (2E0EOL) and others,
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without

          
M debian/changelog +23 -0
@@ 1,3 1,26 @@ 
+raw2c (1.3.0) stable; urgency=low
+
+  * Sat Feb 20 11:43:43 2016 +0000, ca2543dff5ef: Added binary assets for recursion; larger data set testing
+  * Sat Feb 20 14:36:17 2016 +0000, 7e0eeac81f3e: Add a recurse option
+  * Sat Feb 20 18:06:28 2016 +0000, 0930c8c437d6: Start abstracting file handles and directory handles.
+  * Sat Feb 20 18:11:18 2016 +0000, b862e9afa986: Switch to using regular C exit codes, rather than false for failure
+  * Mon Feb 22 19:37:23 2016 +0000, bce5856e82b5: Merged 1.2.2-1 tag into default
+  * Mon Feb 22 19:37:31 2016 +0000, 20fe70069aa3: Merged default down into develop
+  * Mon Feb 22 19:38:22 2016 +0000, f69d56be73f2: Merged debian packaging
+  * Sat Mar 05 11:08:08 2016 +0000, 9250617205fc: Create branch to address issue #1
+  * Sat Mar 05 11:38:29 2016 +0000, c94dbc4a9f4c: Warn about files over 64KB
+  * Sat Mar 05 11:48:57 2016 +0000, 94b46b36946e: Pass actual stats to fileSizeWarning
+  * Sat Mar 05 11:51:38 2016 +0000, 2cf415d5410b: Type checks, remove warnings
+  * Sat Mar 05 11:58:12 2016 +0000, eaed55deaa23: English correction
+  * Sat Mar 05 13:42:50 2016 +0000, bcd419d97c3e: 65535 is the threshold
+  * Sun Mar 06 18:06:11 2016 +0000, 654dd170c173: Merged in i/1 (pull request #1)
+  * Sat Apr 07 11:55:18 2018 +0000, 2555ea909eb5: Fix some compiler warnings
+  * Sat Apr 07 12:02:58 2018 +0000, 87b86e4c07d7: Resolve "maybe uninitialized" header
+  * Sat Apr 07 12:03:52 2018 +0000, 9698aed20a1f: Remove apparently unfinished code because of unused variable "outputIsDir"
+  * Sat Apr 07 12:15:36 2018 +0000, 0d77e7a6ad0e: Merged branch f/201804-compiler-warnings at revision 9698aed20a1f into develop
+
+ -- Duncan Rosss Palmer <palmer@overchat.org>  Sat, 07 Apr 2018 12:20:22 +0000
+
 raw2c (1.2.2-1) stable; urgency=low
 
   * DDRP: Added Debian packaging

          
M debian/control +1 -1
@@ 1,7 1,7 @@ 
 Source: raw2c
 Section: devel
 Priority: extra
-Maintainer: David Duncan Ross Palmer <palmer@overchat.org>
+Maintainer: Duncan Ross Palmer <palmer@overchat.org>
 Build-Depends: debhelper (>= 9), build-essential
 Standards-Version: 3.7.3
 Homepage: https://bitbucket.org/2E0EOL/raw2c

          
M raw2c.c +133 -49
@@ 1,6 1,6 @@ 
 /*
 raw2c
-Copyright (c) 2016, David Duncan Ross Palmer (2E0EOL) and others,
+Copyright (c) 2018, Duncan Ross Palmer (2E0EOL) and others,
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without

          
@@ 30,14 30,20 @@ ARISING IN ANY WAY OUT OF THE USE OF THI
 POSSIBILITY OF SUCH DAMAGE.
 */
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
-static const char head[] = "/*\n  This file was autogenerated by raw2c, by David Duncan Ross Palmer.\n  Visit http://www.daybologic.co.uk\n*/\n\n";
+static const char head[] = "/*\n  This file was autogenerated by raw2c, by Duncan Ross Palmer.\n  Visit http://www.daybologic.co.uk\n*/\n\n";
 static struct {
 	int quiet;
+	int recurse;
 	int null_terminator;
 	char *inputFileName;
 	char *outputFileName;

          
@@ 45,26 51,42 @@ static struct {
 	char *headerName;
 } options;
 
+struct FileOrDir {
+	union {
+		FILE *file;
+		DIR *dir;
+	} handle;
+	int isDir;
+};
+
 #define compare(string1, string2) \
           (0 == strcmp((string1), (string2)))
 #define compareN(string1, string2, n) \
           (0 == strncmp((string1), (string2), (n)))
 
+static void initFileOrDir(struct FileOrDir *fileOrDir, const int isDir);
 static int parse(int argumentCount, char *argumentVector[]);
 static void syntax(void);
 static void title(void);
-static int openFiles(FILE **pPInput, FILE **pPOutput, FILE **pPHeader);
+static int openFiles(
+	struct FileOrDir *pInput,
+	struct FileOrDir *pOutput,
+	struct FileOrDir *pHeader
+);
 static char *randomSymbol(void);
 static char randChar(void);
-static void makeSource(FILE *hIn, FILE *hOut);
-static void makeHeader(FILE *hHeader);
+static void makeSource(struct FileOrDir *pIn, struct FileOrDir *pOut);
+static void makeHeader(struct FileOrDir *pHeader);
 static unsigned long int getFileLength(FILE *fileHandle);
+static void fileSizeWarning(const char *const fileName, const off_t sz);
 
 int main (int argc, char* argv[]) {
 
-	FILE *hIn, *hOut, *hHeader;
+	struct FileOrDir input, output, header;
 
-	if (!parse(argc, argv)) {
+	header.handle.file = NULL;
+
+	if (parse(argc, argv)) {
 		syntax();
 		return EXIT_FAILURE;
 	}

          
@@ 72,14 94,16 @@ int main (int argc, char* argv[]) {
 	if (!options.quiet) /* Are we allowed to talk? */
 		title();
 
-	if (!openFiles(&hIn, &hOut, &hHeader))
+	if (openFiles(&input, &output, &header))
 		return EXIT_FAILURE;
 
-	if (options.headerName) makeHeader(hHeader);
-	makeSource(hIn, hOut);
+	if (options.headerName) makeHeader(&header);
+	makeSource(&input, &output);
 
-	fclose(hIn);
-	fclose(hOut);
+	/* Close files */
+	fclose(input.handle.file);
+	fclose(output.handle.file);
+	if (options.headerName) fclose(header.handle.file);
 
 	return EXIT_SUCCESS;
 }

          
@@ 97,7 121,19 @@ static void syntax() {
 
 static void title() {
 
-	printf("raw2c by David Duncan Ross Palmer\n");
+	printf("raw2c by Duncan Ross Palmer\n");
+	return;
+}
+
+static void initFileOrDir(struct FileOrDir *fileOrDir, const int isDir) {
+
+	fileOrDir->isDir = isDir;
+	if (fileOrDir->isDir) {
+		fileOrDir->handle.dir = NULL;
+	} else {
+		fileOrDir->handle.file = NULL;
+	}
+
 	return;
 }
 

          
@@ 110,6 146,8 @@ static int parse(int argumentCount, char
 			options.quiet = 1;
 		} else if (compare(argumentVector[i], "-n")) {
 			options.null_terminator = 1;
+		} else if (compare(argumentVector[i], "-r")) {
+			options.recurse = 1;
 		} else if (compareN(argumentVector[i], "--symbol", 8)) {
 			char *pointer = argumentVector[i];
 			pointer += 8; /* Move along to symbol name */

          
@@ 127,14 165,14 @@ static int parse(int argumentCount, char
 				options.outputFileName = argumentVector[i];
 			} else {
 				printf("Too many filenames specified\n");
-				return 0;
+				return EXIT_FAILURE;
 			}
 		}
 	}
 
 	if (!options.inputFileName || !options.outputFileName) {
 		printf("Must specify input & output filenames\n");
-		return 0;
+		return EXIT_FAILURE;
 	}
 
 	if (!options.symbolName) {

          
@@ 144,40 182,68 @@ static int parse(int argumentCount, char
 		options.symbolName = randomSymbol();
 	}
 
-	return 1;
+	return EXIT_SUCCESS;
 }
 
-static int openFiles(FILE **pPInput, FILE **pPOutput, FILE **pPHeader) {
+static int openFiles(
+	struct FileOrDir *pInput,
+	struct FileOrDir *pOutput,
+	struct FileOrDir *pHeader
+) {
 
-	*pPInput = fopen(options.inputFileName, "rb");
+	struct stat fileInfo;
+
+	/* Find out if the input is a directory */
+	if (0 != stat(options.inputFileName, &fileInfo)) {
+		fprintf(stderr, "Cannot stat %s\n", options.inputFileName);
+		return EXIT_FAILURE;
+	}
+	initFileOrDir(pInput, S_ISDIR(fileInfo.st_mode));
 
-	if (!(*pPInput)) {
-		printf("Can\'t open input file \"%s\"\n", options.inputFileName);
-		return 0;
+	/* Find out if the file is "too large" and warn user */
+	fileSizeWarning(options.inputFileName, fileInfo.st_size);
+
+	initFileOrDir(pOutput, S_ISDIR(fileInfo.st_mode));
+
+	/* Open the files or directories */
+	if (pInput->isDir) {
+		abort(); /* TODO */
+	} else {
+		pInput->handle.file = fopen(options.inputFileName, "rb");
+
+		if (!pInput->handle.file) {
+			printf("Can\'t open input file \"%s\"\n", options.inputFileName);
+			return EXIT_FAILURE;
+		}
 	}
 
-	*pPOutput = fopen(options.outputFileName, "wt");
+	if (pOutput->isDir) {
+		abort(); /* TODO */
+	} else {
+		pOutput->handle.file = fopen(options.outputFileName, "wt");
 
-	if (!(*pPOutput)) {
-		printf("Can\'t open output file \"%s\"\n", options.outputFileName);
-		fclose(*pPInput); /* Cleanup one that worked */
-		return 0;
+		if (!pOutput->handle.file) {
+			printf("Can\'t open output file \"%s\"\n", options.outputFileName);
+			fclose(pOutput->handle.file); /* Cleanup one that worked */
+			return EXIT_FAILURE;
+		}
 	}
 
 	if (options.headerName) {
-		*pPHeader = fopen(options.headerName, "wt");
-		if (!(*pPHeader)) {
+		initFileOrDir(pHeader, S_ISDIR(fileInfo.st_mode));
+		pHeader->handle.file = fopen(options.headerName, "wt");
+		if (!pHeader->handle.file) {
 			printf("Can\'t open header file \"%s\"\n", options.headerName);
 
 			/* Cleanup file which worked */
-			fclose(*pPInput);
-			fclose(*pPOutput);
+			fclose(pInput->handle.file);
+			fclose(pOutput->handle.file);
 
-			return 0;
+			return EXIT_FAILURE;
 		}
 	}
 
-	return 1; /* OK */
+	return EXIT_SUCCESS;
 }
 
 static char *randomSymbol() {

          
@@ 209,22 275,24 @@ static char randChar() {
 	return validchars[randoffset];
 }
 
-static void makeSource(FILE *hIn, FILE *hOut) {
+static void makeSource(struct FileOrDir *pIn, struct FileOrDir *pOut) {
 
 	unsigned long int counter = 0UL;
 	unsigned long int length;
 
-	rewind(hIn);
-	rewind(hOut);
-	length = getFileLength(hIn);
+	assert(!pIn->isDir);
+	rewind(pIn->handle.file);
+	assert(!pOut->isDir);
+	rewind(pOut->handle.file);
+	length = getFileLength(pIn->handle.file);
 	if (options.null_terminator) length++;
 
-	fprintf(hOut, head); /* Put top comment into source */
+	fprintf(pOut->handle.file, head); /* Put top comment into source */
 
 	if (options.headerName)
-		fprintf(hOut, "#include \"%s\"\n", options.headerName);
+		fprintf(pOut->handle.file, "#include \"%s\"\n", options.headerName);
 
-	fprintf(hOut, "const char %s[] = {\n\t", options.symbolName);
+	fprintf(pOut->handle.file, "const char %s[] = {\n\t", options.symbolName);
 	while (counter < length) {
 
 		unsigned char thisByte;

          
@@ 234,34 302,35 @@ static void makeSource(FILE *hIn, FILE *
 			if (options.null_terminator) thisByte = '\0'; /* A null terminator */
 			freadResult = 1;
 		} else {
-			freadResult = fread(&thisByte, sizeof(thisByte), 1, hIn);
+			freadResult = fread(&thisByte, sizeof(thisByte), 1, pIn->handle.file);
 		}
 
 		if (freadResult >= 1)
-			fprintf(hOut, "\'\\x%02X\'", thisByte);
+			fprintf(pOut->handle.file, "\'\\x%02X\'", thisByte);
 
 		if (counter+1 < length) /* More to go */
-			fprintf(hOut, ", ");
+			fprintf(pOut->handle.file, ", ");
 
 		/* Seperate half paragraph boundries */
 		if (0 == ((1+counter) % 8)) {
-			fputc('\n', hOut);
-			fputc('\t', hOut);
+			fputc('\n', pOut->handle.file);
+			fputc('\t', pOut->handle.file);
 		}
 
 		counter++;
 	}
 
-	fprintf(hOut, "\n};\n");
+	fprintf(pOut->handle.file, "\n};\n");
 	return;
 }
 
-static void makeHeader(FILE *hHeader) {
+static void makeHeader(struct FileOrDir *pHeader) {
 
-	rewind(hHeader);
+	assert(!pHeader->isDir);
+	rewind(pHeader->handle.file);
 
-	fprintf(hHeader, head); /* Put top comment into source */
-	fprintf(hHeader, "extern const char %s[];\n", options.symbolName);
+	fprintf(pHeader->handle.file, head); /* Put top comment into source */
+	fprintf(pHeader->handle.file, "extern const char %s[];\n", options.symbolName);
 
 	return;
 }

          
@@ 277,3 346,18 @@ static unsigned long int getFileLength(F
 
 	return (unsigned long int)endPos;
 }
+
+static void fileSizeWarning(const char *const fileName, const off_t sz) {
+
+	const off_t threshold = (64 * 1024) -1;
+
+	if (threshold > sz)
+		return;
+
+	fprintf(
+		stderr,
+		"WARNING! \"%s\" is over %lu bytes, on 16-bit architectures, this could overflow a segment\n"
+		"\tFor large files, consider using librttb; https://bitbucket.org/2E0EOL/rttb\n",
+		fileName, threshold
+	);
+}

          
M ref.c +1 -1
@@ 1,5 1,5 @@ 
 /*
-  This file was autogenerated by raw2c, by David Duncan Ross Palmer.
+  This file was autogenerated by raw2c, by Duncan Ross Palmer.
   Visit http://www.daybologic.co.uk
 */
 

          
M ref.h +1 -1
@@ 1,5 1,5 @@ 
 /*
-  This file was autogenerated by raw2c, by David Duncan Ross Palmer.
+  This file was autogenerated by raw2c, by Duncan Ross Palmer.
   Visit http://www.daybologic.co.uk
 */