diff --git a/ppm.odin b/ppm.odin index 3d53910..532d1de 100644 --- a/ppm.odin +++ b/ppm.odin @@ -1,6 +1,6 @@ +#+vet explicit-allocators package main -import "core:fmt" import "core:os" import "core:strconv" import "core:strings" @@ -17,7 +17,6 @@ Ppm_Pixel :: struct { } Ppm_file :: struct { - file: os.File, //TODO: use this instead perhaps? data: []u8, magic_num: PPM_MAGIC_NUMBER, width: int, @@ -26,7 +25,14 @@ Ppm_file :: struct { pixels: []Ppm_Pixel, } -load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) { +load_ppm :: proc( + ppm_file: ^Ppm_file, + filename: string, + allocator := context.allocator, +) -> ( + bool, + os.Error, +) { if ppm_file == nil || len(filename) == 0 { return false, .Not_Exist } @@ -36,7 +42,7 @@ load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) { //if file is valid, opemn but check error too! err: os.Error - ppm_file.data, err = os.read_entire_file_from_path(filename, context.temp_allocator) + ppm_file.data, err = os.read_entire_file_from_path(filename, allocator) if err != nil { return false, err } @@ -44,11 +50,11 @@ load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) { } //TODO: handle errors and err return type (success: bool?, err: ErrorType?) -ppm_parse :: proc(ppm_file: ^Ppm_file) { +ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) { magic_num_found := false w_h_found := false max_col_val_found := false - sb := strings.builder_make(allocator = context.temp_allocator) + sb := strings.builder_make(allocator) defer strings.builder_destroy(&sb) it := string(ppm_file.data) @@ -66,20 +72,16 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) { continue } - w_h_parts := strings.split(line, " ") + w_h_parts := strings.split(line, " ", allocator) if !w_h_found && len(w_h_parts) == 2 { ppm_file.width, _ = strconv.parse_int(w_h_parts[0]) ppm_file.height, _ = strconv.parse_int(w_h_parts[1]) - ppm_file.pixels = make( - []Ppm_Pixel, - ppm_file.width * ppm_file.height, - context.temp_allocator, - ) + ppm_file.pixels = make([]Ppm_Pixel, ppm_file.width * ppm_file.height, allocator) w_h_found = true continue } - max_val_part := strings.split(line, " ") + max_val_part := strings.split(line, " ", allocator) if !max_col_val_found && len(max_val_part) == 1 { ppm_file.max_col_val, _ = strconv.parse_int(max_val_part[0]) max_col_val_found = true @@ -87,7 +89,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) { } if magic_num_found && w_h_found && max_col_val_found { - for value in strings.split(line, " ") { + for value in strings.split(line, " ", allocator) { strings.write_string(&sb, value) strings.write_string(&sb, " ") } @@ -97,7 +99,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) { all_values := strings.to_string(sb) index := 0 - pixel_values := strings.split(all_values, " ") + pixel_values := strings.split(all_values, " ", allocator) for i := 0; i < len(pixel_values); i += 3 { if index < len(ppm_file.pixels) {