From 783697d4dfaf1036e8169977d1a040ba92b68fce Mon Sep 17 00:00:00 2001 From: MrBounty Date: Thu, 17 Oct 2024 19:38:08 +0200 Subject: [PATCH] Created config.zig and BUFFERSIZE --- src/config.zig | 1 + src/fileEngine.zig | 16 +++++++++------- src/main.zig | 4 +++- 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 src/config.zig diff --git a/src/config.zig b/src/config.zig new file mode 100644 index 0000000..41bfb19 --- /dev/null +++ b/src/config.zig @@ -0,0 +1 @@ +pub const BUFFER_SIZE = 1024 * 50; // Line limit when parsing file diff --git a/src/fileEngine.zig b/src/fileEngine.zig index ceb5b1a..149530e 100644 --- a/src/fileEngine.zig +++ b/src/fileEngine.zig @@ -12,6 +12,8 @@ const SchemaTokenizer = @import("tokenizers/schema.zig").Tokenizer; const SchemaToken = @import("tokenizers/schema.zig").Token; const AdditionalData = @import("stuffs/additionalData.zig").AdditionalData; +const BUFFER_SIZE = @import("config.zig").BUFFER_SIZE; + // TODO: Use those errors everywhere in this file const FileEngineError = error{ SchemaFileNotFound, @@ -35,7 +37,7 @@ pub const FileEngine = struct { pub fn init(allocator: Allocator, path: []const u8) FileEngine { const path_to_ZipponDB_dir = path; - var schema_buf = allocator.alloc(u8, 1024 * 50) catch @panic("Cant allocate the schema buffer"); + var schema_buf = allocator.alloc(u8, BUFFER_SIZE) catch @panic("Cant allocate the schema buffer"); defer allocator.free(schema_buf); const len: usize = FileEngine.readSchemaFile(allocator, path_to_ZipponDB_dir, schema_buf) catch 0; @@ -180,7 +182,7 @@ pub const FileEngine = struct { /// Request a path to a schema file and then create the struct folder /// TODO: Check if some data already exist and if so ask if the user want to delete it and make a backup pub fn initDataFolder(self: *FileEngine, path_to_schema_file: []const u8) FileEngineError!void { - var schema_buf = self.allocator.alloc(u8, 1024 * 50) catch @panic("Cant allocate the schema buffer"); + var schema_buf = self.allocator.alloc(u8, BUFFER_SIZE) catch @panic("Cant allocate the schema buffer"); defer self.allocator.free(schema_buf); const file = std.fs.cwd().openFile(path_to_schema_file, .{}) catch return FileEngineError.SchemaFileNotFound; @@ -240,7 +242,7 @@ pub const FileEngine = struct { }; defer file.close(); - var output: [1024 * 50]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file + var output: [BUFFER_SIZE]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file var output_fbs = std.io.fixedBufferStream(&output); const writer = output_fbs.writer(); @@ -362,7 +364,7 @@ pub const FileEngine = struct { }; defer file.close(); - var output: [1024 * 50]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file + var output: [BUFFER_SIZE]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file var output_fbs = std.io.fixedBufferStream(&output); const writer = output_fbs.writer(); @@ -427,7 +429,7 @@ pub const FileEngine = struct { }; defer file.close(); - var output: [1024 * 50]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file + var output: [BUFFER_SIZE]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file var output_fbs = std.io.fixedBufferStream(&output); const writer = output_fbs.writer(); @@ -626,7 +628,7 @@ pub const FileEngine = struct { }; defer new_file.close(); - var output: [1024 * 50]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file + var output: [BUFFER_SIZE]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file var output_fbs = std.io.fixedBufferStream(&output); const writer = output_fbs.writer(); @@ -778,7 +780,7 @@ pub const FileEngine = struct { }; defer new_file.close(); - var output: [1024 * 50]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file + var output: [BUFFER_SIZE]u8 = undefined; // Maybe need to increase that as it limit the size of a line in a file var output_fbs = std.io.fixedBufferStream(&output); const writer = output_fbs.writer(); diff --git a/src/main.zig b/src/main.zig index 3619451..b1e1033 100644 --- a/src/main.zig +++ b/src/main.zig @@ -9,6 +9,8 @@ const ziqlParser = @import("ziqlParser.zig").Parser; const utils = @import("stuffs/utils.zig"); const send = @import("stuffs/utils.zig").send; +const BUFFER_SIZE = @import("config.zig").BUFFER_SIZE; + const State = enum { expect_main_command, expect_query, @@ -53,7 +55,7 @@ pub fn main() !void { std.debug.print("No ZIPONDB_PATH environment variable found, please use the command:\n db use path/to/db \nor\n db new /path/to/dir\n", .{}); } - const line_buf = try allocator.alloc(u8, 1024 * 50); // TODO: Remove the size limitation + const line_buf = try allocator.alloc(u8, BUFFER_SIZE); // TODO: Remove the size limitation defer allocator.free(line_buf); while (true) {