changeset 47:7cb4ca7cf257

Use a common.c file to hold concurrent functions Also I fixed that 1.4 KB bug I think
author Paper <mrpapersonic@gmail.com>
date Sat, 21 May 2022 18:41:54 -0400
parents 7cb9fad3f5ee
children 652343b56a60
files Makefile include/common.h src/common.c src/gui.c src/main.c
diffstat 5 files changed, 58 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Fri May 20 14:41:46 2022 -0400
+++ b/Makefile	Sat May 21 18:41:54 2022 -0400
@@ -4,12 +4,12 @@
 src/%.o : src/%.c
 	$(CC) -c $(CC_FLAGS) $< -o $@
 
-msvpvf: src/main.o
-	$(CC) -o $@ $< $(CC_FLAGS) $(LD_FLAGS)
+msvpvf: src/main.o src/common.o
+	$(CC) -o $@ $^ $(CC_FLAGS) $(LD_FLAGS)
 
 # GUI is windows-only, please use cross-compiler!
-gui: src/gui.o
-	$(CC) -o $@ $< $(CC_FLAGS) $(LD_FLAGS) -mwindows
+gui: src/gui.o src/common.o
+	$(CC) -o $@ $^ $(CC_FLAGS) $(LD_FLAGS) -mwindows
 
 clean:
 	rm -f src/*.o *.exe msvpvf gui
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/common.h	Sat May 21 18:41:54 2022 -0400
@@ -0,0 +1,2 @@
+void set_data(unsigned char magic[], uint16_t version, FILE* target);
+int copy_file(char* source_file, char* target_file);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common.c	Sat May 21 18:41:54 2022 -0400
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdint.h>
+
+void set_data(unsigned char magic[], uint16_t version, FILE* target) {
+	int i;
+	fseek(target, 0x46, SEEK_SET);
+	fputc(version, target);
+	for (i=0; i<=sizeof(*magic); ++i) {
+		fseek(target, 0x18+i, SEEK_SET);
+		fputc(magic[i], target);
+	}
+}
+
+int copy_file(char* source_file, char* target_file) {
+	char ch[4096];
+	FILE *source, *target;
+
+	source = fopen(source_file, "rb");
+
+	if (source == NULL)
+		return 1;
+
+	target = fopen(target_file, "wb");
+
+	if (target == NULL) {
+		fclose(source);
+		return 1;
+	}
+
+	while (fgetc(source) != EOF) {
+		size_t b = fread(ch, 1, sizeof(ch), source);
+		if (b)
+			fwrite(ch, 1, b, target);
+	}
+
+	fclose(target);
+	fclose(source);
+	return 0;
+}
--- a/src/gui.c	Fri May 20 14:41:46 2022 -0400
+++ b/src/gui.c	Sat May 21 18:41:54 2022 -0400
@@ -27,6 +27,7 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <commdlg.h>
+#include "../include/common.h"
 #define _WIN32_WINNT 0x0400
 #define ARRAYSIZE(a) \
 	sizeof(a)/sizeof(a[0])
@@ -46,40 +47,6 @@
 } type;
 char* file_name = " ";
 
-void set_data(unsigned char magic[], uint16_t version, FILE* target) {
-	int i;
-	fseek(target, 0x46, SEEK_SET);
-	fputc(version, target);
-	for (i=0; i<=sizeof(*magic); ++i) {
-		fseek(target, 0x18+i, SEEK_SET);
-		fputc(magic[i], target);
-	}
-}
-
-int copy_file(char* source_file, char* target_file) {
-	/* Copy a file */
-	FILE *source, *target;
-
-	source = fopen(source_file, "rb");
-	if (source == NULL) return 1;
-	target = fopen(target_file, "wb");
-	if (target == NULL) {
-		fclose(source);
-		return 1;
-	}
-
-	size_t n, m;
-	unsigned char buff[8192];
-	do {
-		n = fread(buff, 1, sizeof(buff), source);
-		if (n) m = fwrite(buff, 1, n, target);
-		else m = 0;
-	} while ((n > 0) && (n == m));
-
-	fclose(target);
-	fclose(source);
-	return 0;
-}
 
 void display_file(char* path) {
 	/* Read the file to memory */
@@ -177,24 +144,14 @@
 																	  * these arguments are for, so chances
 																	  * are that you don't either. 
 																	 **/
-	TCHAR versions[][10] = {TEXT("8"), TEXT("9"), TEXT("10"), 
+	TCHAR versions[][10] = {TEXT("8"),  TEXT("9"),  TEXT("10"), 
 							TEXT("11"), TEXT("12"), TEXT("13"), 
 							TEXT("14"), TEXT("15"), TEXT("16"), 
 							TEXT("17"), TEXT("18"), TEXT("19")};
 
-	TCHAR A[16];
-
-	/**
-	 * Here we can't just use a for loop 
-	 * and cast all of those to `TEXT()`.
-	 * Why? I don't know. My brain is too
-	 * small to figure it out.
-	**/
-	memset(&A,0,sizeof(A));
 	int i = 0;
-	for (i = 0; i <= 11; i++) {
-		strncpy((TCHAR*)A, (TCHAR*)versions[i], ARRAYSIZE(A));
-		SendMessage(hWndComboBox, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A);
+	for (i = 0; i < ARRAYSIZE(versions); i++) {
+		SendMessage(hWndComboBox, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)versions[i]);
 	}
 	SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)3, (LPARAM)0);
 	/* Open File */
--- a/src/main.c	Fri May 20 14:41:46 2022 -0400
+++ b/src/main.c	Sat May 21 18:41:54 2022 -0400
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <getopt.h>
 #include <libgen.h>
+#include "../include/common.h"
 #ifdef _MSC_VER
 #define strdup(p) _strdup(p)
 #endif
@@ -29,40 +30,6 @@
     return str;
 }
 
-void set_data(unsigned char magic[], uint16_t version, FILE* target) {
-	int i;
-	fseek(target, 0x46, SEEK_SET);
-	fputc(version, target);
-	for (i=0; i<=sizeof(*magic); ++i) {
-		fseek(target, 0x18+i, SEEK_SET);
-		fputc(magic[i], target);
-	}
-}
-
-int copy_file(char* source_file, char* target_file) {
-	char ch;
-	FILE *source, *target;
-
-	source = fopen(source_file, "rb");
-
-	if (source == NULL)
-		return 1;
-
-	target = fopen(target_file, "wb");
-
-	if (target == NULL) {
-		fclose(source);
-		return 1;
-	}
-
-	while( ( ch = fgetc(source) ) != EOF )
-		fputc(ch, target);
-
-	fclose(target);
-	fclose(source);
-	return 0;
-}
-
 int main(int argc, char *argv[]) {
 	int c, option_index = 0;
 	unsigned char magic[16];
@@ -71,7 +38,7 @@
 		char input[256];
 		char output[256];
 		int version;
-		char type[256];
+		char type[4];
 	} args;
 	strcpy(args.input, " ");
 	strcpy(args.output, " ");
@@ -84,13 +51,13 @@
 				strncpy(args.input, optarg, sizeof(args.input)-1);  /* subtract 1 to make sure it's "null-safe" */
 				break;
 			case 'o':
-				strncpy(args.output, optarg, sizeof(args.input)-1);
+				strncpy(args.output, optarg, sizeof(args.output)-1);
 				break;
 			case 'v':
 				args.version = abs(atoi(strdup(optarg)));  /* abs() for possible negative inputs */
 				break;
 			case 't':
-				strncpy(args.type, optarg, sizeof(args.input)-1);
+				strncpy(args.type, optarg, sizeof(args.type)-1);
 				break;
 			case 'h':
 			default:
@@ -148,14 +115,14 @@
 	if (strcmp(args.output, " ") == 0) { /* string manipulation hell */
 		char* temp = (char*)calloc(256, sizeof(char));
 		temp[0] = '\0';
-		char str_version[16];
+		char str_version[4];
 		sprintf(str_version, "V%d", args.version);
-		strncat(temp, str_version, 2);
-		strncat(temp, "_", 1);
+		strncat(temp, str_version, 4);
+		strncat(temp, "_", 2);
 		strncat(temp, basename(args.input), 248);
 		strcpy(temp, strremove(temp, strrchr(basename(args.input), ('.')))); /* remove file extension */
-		strncat(temp, ".", 1);
-		strncat(temp, args.type, 3);
+		strncat(temp, ".", 2);
+		strncat(temp, args.type, 4);
 		strncpy(args.output, temp, 255);
 		free(temp);
 	}