Browse Source

faux-file2c: Binary mode

Serj Kalichev 4 years ago
parent
commit
acede32bcd
3 changed files with 133 additions and 21 deletions
  1. 4 0
      faux/file.h
  2. 62 1
      faux/file/file.c
  3. 67 20
      utils/faux-file2c.c

+ 4 - 0
faux/file.h

@@ -21,9 +21,13 @@ faux_file_t *faux_file_open(const char *pathname, int flags, mode_t mode);
 int faux_file_close(faux_file_t *file);
 int faux_file_fileno(faux_file_t *file);
 bool_t faux_file_eof(const faux_file_t *file);
+
 char *faux_file_getline_raw(faux_file_t *file);
 char *faux_file_getline(faux_file_t *file);
 ssize_t faux_file_write(faux_file_t *file, const void *buf, size_t n);
+ssize_t faux_file_write_block(faux_file_t *f, const void *buf, size_t n);
+ssize_t faux_file_read(faux_file_t *f, void *buf, size_t n);
+ssize_t faux_file_read_block(faux_file_t *f, void *buf, size_t n);
 
 C_DECL_END
 

+ 62 - 1
faux/file/file.c

@@ -419,7 +419,7 @@ char *faux_file_getline(faux_file_t *f) {
  * @param [in] f File object.
  * @param [in] buf Buffer to write.
  * @param [in] n Number of bytes to write.
- * @return Number of bytes written or NULL on error.
+ * @return Number of bytes written or < 0 on error.
  */
 ssize_t faux_file_write(faux_file_t *f, const void *buf, size_t n) {
 
@@ -429,3 +429,64 @@ ssize_t faux_file_write(faux_file_t *f, const void *buf, size_t n) {
 
 	return faux_write(f->fd, buf, n);
 }
+
+
+/** @brief Writes data block to file.
+ *
+ * See faux_write_block() for documentation.
+ *
+ * @param [in] f File object.
+ * @param [in] buf Buffer to write.
+ * @param [in] n Number of bytes to write.
+ * @return Number of bytes written or < 0 on error.
+ */
+ssize_t faux_file_write_block(faux_file_t *f, const void *buf, size_t n) {
+
+	assert(f);
+	if (!f)
+		return -1;
+
+	return faux_write_block(f->fd, buf, n);
+}
+
+
+/** @brief Read data from file.
+ *
+ * See faux_read() for documentation.
+ *
+ * @param [in] f File object.
+ * @param [in] buf Buffer.
+ * @param [in] n Number of bytes.
+ * @return Number of bytes readed or < 0 on error.
+ */
+ssize_t faux_file_read(faux_file_t *f, void *buf, size_t n) {
+
+	assert(f);
+	if (!f)
+		return -1;
+
+// TODO: Read buffer first
+
+	return faux_read(f->fd, buf, n);
+}
+
+
+/** @brief Read data block from file.
+ *
+ * See faux_read_block() for documentation.
+ *
+ * @param [in] f File object.
+ * @param [in] buf Buffer.
+ * @param [in] n Number of bytes.
+ * @return Number of bytes readed or < 0 on error.
+ */
+ssize_t faux_file_read_block(faux_file_t *f, void *buf, size_t n) {
+
+	assert(f);
+	if (!f)
+		return -1;
+
+// TODO: Read buffer first
+
+	return faux_read_block(f->fd, buf, n);
+}

+ 67 - 20
utils/faux-file2c.c

@@ -40,6 +40,10 @@
 #define QUOTE(t) #t
 #define version(v) printf("%s\n", v)
 
+// Binary mode: Number of binary bytes per line. We want 80 syms in row but each
+// byte occupies 4 syms within string so 80 / 4 = 20.
+#define BIN_BYTES_PER_LINE 20
+
 // Command line options */
 struct opts_s {
 	bool_t debug;
@@ -80,8 +84,7 @@ int main(int argc, char *argv[]) {
 	while ((fn = faux_list_each(&iter))) {
 		faux_file_t *f = NULL;
 		char *buf = NULL;
-		bool_t eof = BOOL_FALSE;
-		unsigned int line_num = 0;
+		char *var_name = NULL;
 
 		file_num++;
 		f = faux_file_open(fn, O_RDONLY, 0);
@@ -92,26 +95,70 @@ int main(int argc, char *argv[]) {
 		}
 		printf("\n");
 		printf("// File \"%s\"\n", fn);
-		printf("const char *txt%u =\n", file_num);
-
-		while ((buf = faux_file_getline_raw(f))) {
-			char *escaped_str = NULL;
-			line_num++;
-			escaped_str = faux_str_c_esc(buf);
-			faux_str_free(buf);
-			if (escaped_str)
-				printf("\t\"%s\"\n", escaped_str);
-			faux_str_free(escaped_str);
+		var_name = opts->binary ? "bin" : "txt";
+		printf("const char *%s%u =\n", var_name, file_num);
+
+		// Binary mode
+		if (opts->binary) {
+			ssize_t bytes_readed = 0;
+			size_t total_bytes = 0;
+
+			buf = faux_malloc(BIN_BYTES_PER_LINE);
+			assert(buf);
+			if (!buf) {
+				fprintf(stderr, "Error: Memory problems\n");
+				break;
+			}
+
+			do {
+				char *escaped_str = NULL;
+
+				bytes_readed = faux_file_read_block(f, buf,
+					BIN_BYTES_PER_LINE);
+				if (bytes_readed < 0) {
+					fprintf(stderr, "Error: Can't open "
+						"file \"%s\"\n", fn);
+					total_errors++;
+					break;
+				}
+				if (0 == bytes_readed) // EOF
+					break;
+				total_bytes += bytes_readed;
+				escaped_str = faux_str_c_bin(buf, bytes_readed);
+				if (escaped_str)
+					printf("\t\"%s\"\n", escaped_str);
+				faux_str_free(escaped_str);
+			} while (BIN_BYTES_PER_LINE == bytes_readed);
+
+			faux_free(buf);
+			if (0 == total_bytes) // Empty file
+				printf("\t\"\"\n");
+		
+		// Text mode
+		} else {
+			bool_t eof = BOOL_FALSE;
+			unsigned int line_num = 0;
+
+			while ((buf = faux_file_getline_raw(f))) {
+				char *escaped_str = NULL;
+				line_num++;
+				escaped_str = faux_str_c_esc(buf);
+				faux_str_free(buf);
+				if (escaped_str)
+					printf("\t\"%s\"\n", escaped_str);
+				faux_str_free(escaped_str);
+			}
+			eof = faux_file_eof(f);
+			if (!eof) { // File reading was interrupted before EOF
+				fprintf(stderr, "Error: File \"%s\" reading was "
+					"interrupted before EOF\n", fn);
+				total_errors++;
+			} // Continue normal operations
+
+			if (0 == line_num) // Empty file is not error
+					printf("\t\"\"\n");
 		}
-		eof = faux_file_eof(f);
-		if (!eof) { // File reading was interrupted before EOF
-			fprintf(stderr, "Error: File \"%s\" reading was "
-				"interrupted before EOF\n", fn);
-			total_errors++;
-		} // Continue normal operations
 
-		if (0 == line_num) // Empty file is not error
-				printf("\t\"\"\n");
 		printf(";\n");
 		faux_file_close(f);
 	}