1. Ebrahim Mohammadi
  2. iransystem

Commits

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

  • Participants
  • Parent commits 10306af
  • Branches default

Comments (0)

Files changed (1)

File iransystem-to-utf8.cpp

View file
 #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;