Ebrahim Mohammadi avatar Ebrahim Mohammadi committed 0015ab9

* Removed limit of maximum file size of 16 MiB (fixes #2)
* If no file is provided, at most 256 MiB of stdin will be processed

Comments (0)

Files changed (1)

iransystem-to-utf8.cpp

 #include <cstdint>
 #include <cstring>
 
-#define MAX_SIZE (1<<24)
+#define MAX_SIZE (1<<28)
 #define REVERSE 1		// Are characters of Right-to-Left parts of input in reverse order?
 
 static const char* ZWNJ = "\xE2\x80\x8C";
 	return my_joining;
 }
 
+size_t get_file_size(const char* const file_name)
+{
+	FILE* f = fopen(file_name, "r");
+	if (!f)
+		return 0;
+	long size = 0;
+	if (fseek(f, 0, SEEK_END) == 0)
+		size = ftell(f);
+	fclose(f);
+	return size < 0 ? 0 : size;
+}
+
 int main(int argc, const char* argv[])
 {
-	if (argc > 1 && freopen(argv[1], "r", stdin) == nullptr)
+	size_t file_size = MAX_SIZE;
+	if (argc > 1)
 	{
-		fputs("Error: Failed to open input file\n", stderr);
-		return 1;
+		file_size = get_file_size(argv[1]);
+		if (freopen(argv[1], "r", stdin) == nullptr)
+		{
+			fputs("Error: Failed to open input file\n", stderr);
+			return 1;
+		}
 	}
 
 	for (int i = 0; i < 256; ++i)
 			map_ordering[i] = ORDER_RTL;
 	map_ordering['\n'] = ORDER_CHECKPOINT;
 
-	uint8_t* buf = new uint8_t[MAX_SIZE];
-	size_t size = fread(buf, 1, MAX_SIZE, stdin);
+	fprintf(stderr, "file_size: %llu\n", file_size);
+	uint8_t* buf = new uint8_t[file_size];
+	size_t size = fread(buf, 1, file_size, stdin);
 
 	CharJoining prev_joining = JOINS_NONE;
 	uint8_t* data = buf;
 	uint8_t* context_start = data;
 	while (size >= 0)
 	{
-		uint8_t byte = *data;
+		const uint8_t byte = *data;
 		CharOrdering my_ordering = map_ordering[byte];
 		if (my_ordering == ORDER_CHECKPOINT || size == 0)
 		{
 			// Reverse the context
 			for (int i = 0; context_start + i < context_end - i; ++i)
 			{
-				uint8_t temp = context_end[-i];
+				const uint8_t temp = context_end[-i];
 				context_end[-i] = context_start[i];
 				context_start[i] = temp;
 			}
 #else
 	while (size > 0)
 	{
-		uint8_t byte = *data;
+		const uint8_t byte = *data;
 		prev_joining = print_byte(byte, prev_joining);
 		++data;
 		--size;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.