aboutsummaryrefslogtreecommitdiff
path: root/core/encoding
diff options
context:
space:
mode:
Diffstat (limited to 'core/encoding')
-rw-r--r--core/encoding/csv/doc.odin35
-rw-r--r--core/encoding/hxa/hxa_os.odin34
-rw-r--r--core/encoding/hxa/read.odin15
-rw-r--r--core/encoding/hxa/write.odin18
-rw-r--r--core/encoding/ini/ini.odin26
-rw-r--r--core/encoding/ini/ini_os.odin20
-rw-r--r--core/encoding/xml/xml_os.odin18
-rw-r--r--core/encoding/xml/xml_reader.odin26
8 files changed, 105 insertions, 87 deletions
diff --git a/core/encoding/csv/doc.odin b/core/encoding/csv/doc.odin
index 50b8e3d1a..1fb685602 100644
--- a/core/encoding/csv/doc.odin
+++ b/core/encoding/csv/doc.odin
@@ -16,14 +16,15 @@ Example:
r.reuse_record_buffer = true // Without it you have to each of the fields within it
defer csv.reader_destroy(&r)
- csv_data, ok := os.read_entire_file(filename)
- if ok {
+ csv_data, csv_err := os.read_entire_file(filename, context.allocator)
+ defer delete(csv_data)
+
+ if csv_err == nil {
csv.reader_init_with_string(&r, string(csv_data))
} else {
- fmt.printfln("Unable to open file: %v", filename)
+ fmt.eprintfln("Unable to open file: %v. Error: %v", filename, csv_err)
return
}
- defer delete(csv_data)
for r, i, err in csv.iterator_next(&r) {
if err != nil { /* Do something with error */ }
@@ -39,16 +40,16 @@ Example:
r: csv.Reader
r.trim_leading_space = true
r.reuse_record = true // Without it you have to delete(record)
- r.reuse_record_buffer = true // Without it you have to each of the fields within it
+ r.reuse_record_buffer = true // Without it you have to delete each of the fields within it
defer csv.reader_destroy(&r)
handle, err := os.open(filename)
+ defer os.close(handle)
if err != nil {
- fmt.eprintfln("Error opening file: %v", filename)
+ fmt.eprintfln("Unable to open file: %v. Error: %v", filename, err)
return
}
- defer os.close(handle)
- csv.reader_init(&r, os.stream_from_handle(handle))
+ csv.reader_init(&r, handle.stream)
for r, i in csv.iterator_next(&r) {
for f, j in r {
@@ -64,21 +65,23 @@ Example:
r.trim_leading_space = true
defer csv.reader_destroy(&r)
- csv_data, ok := os.read_entire_file(filename)
- if ok {
- csv.reader_init_with_string(&r, string(csv_data))
- } else {
- fmt.printfln("Unable to open file: %v", filename)
+ csv_data, csv_err := os.read_entire_file(filename, context.allocator)
+ defer delete(csv_data, context.allocator)
+ if err != nil {
+ fmt.eprintfln("Unable to open file: %v. Error: %v", filename, csv_err)
return
}
- defer delete(csv_data)
+ csv.reader_init_with_string(&r, string(csv_data))
records, err := csv.read_all(&r)
if err != nil { /* Do something with CSV parse error */ }
defer {
- for rec in records {
- delete(rec)
+ for record in records {
+ for field in record {
+ delete(field)
+ }
+ delete(record)
}
delete(records)
}
diff --git a/core/encoding/hxa/hxa_os.odin b/core/encoding/hxa/hxa_os.odin
new file mode 100644
index 000000000..17ad94819
--- /dev/null
+++ b/core/encoding/hxa/hxa_os.odin
@@ -0,0 +1,34 @@
+#+build !freestanding
+#+build !js
+package encoding_hxa
+
+import "core:os"
+
+read_from_file :: proc(filename: string, print_error := false, allocator := context.allocator, loc := #caller_location) -> (file: File, err: Read_Error) {
+ context.allocator = allocator
+
+ data, data_err := os.read_entire_file(filename, allocator, loc)
+ if data_err != nil {
+ err = .Unable_To_Read_File
+ delete(data, allocator)
+ return
+ }
+ file, err = read(data, filename, print_error, allocator)
+ file.backing = data
+ return
+}
+
+write_to_file :: proc(filepath: string, file: File) -> (err: Write_Error) {
+ required := required_write_size(file)
+ buf, alloc_err := make([]byte, required)
+ if alloc_err == .Out_Of_Memory {
+ return .Failed_File_Write
+ }
+ defer delete(buf)
+
+ write_internal(&Writer{data = buf}, file)
+ if os.write_entire_file(filepath, buf) != nil {
+ err =.Failed_File_Write
+ }
+ return
+}
diff --git a/core/encoding/hxa/read.odin b/core/encoding/hxa/read.odin
index 6dde16848..1721bf7fc 100644
--- a/core/encoding/hxa/read.odin
+++ b/core/encoding/hxa/read.odin
@@ -1,7 +1,6 @@
package encoding_hxa
import "core:fmt"
-import "core:os"
import "core:mem"
Read_Error :: enum {
@@ -11,20 +10,6 @@ Read_Error :: enum {
Unable_To_Read_File,
}
-read_from_file :: proc(filename: string, print_error := false, allocator := context.allocator, loc := #caller_location) -> (file: File, err: Read_Error) {
- context.allocator = allocator
-
- data, ok := os.read_entire_file(filename, allocator, loc)
- if !ok {
- err = .Unable_To_Read_File
- delete(data, allocator, loc)
- return
- }
- file, err = read(data, filename, print_error, allocator, loc)
- file.backing = data
- return
-}
-
read :: proc(data: []byte, filename := "<input>", print_error := false, allocator := context.allocator, loc := #caller_location) -> (file: File, err: Read_Error) {
Reader :: struct {
filename: string,
diff --git a/core/encoding/hxa/write.odin b/core/encoding/hxa/write.odin
index 5bb950e81..cbf9c7cb6 100644
--- a/core/encoding/hxa/write.odin
+++ b/core/encoding/hxa/write.odin
@@ -1,7 +1,6 @@
package encoding_hxa
-import "core:os"
-import "core:mem"
+import "core:mem"
Write_Error :: enum {
None,
@@ -9,21 +8,6 @@ Write_Error :: enum {
Failed_File_Write,
}
-write_to_file :: proc(filepath: string, file: File) -> (err: Write_Error) {
- required := required_write_size(file)
- buf, alloc_err := make([]byte, required)
- if alloc_err == .Out_Of_Memory {
- return .Failed_File_Write
- }
- defer delete(buf)
-
- write_internal(&Writer{data = buf}, file)
- if !os.write_entire_file(filepath, buf) {
- err =.Failed_File_Write
- }
- return
-}
-
write :: proc(buf: []byte, file: File) -> (n: int, err: Write_Error) {
required := required_write_size(file)
if len(buf) < required {
diff --git a/core/encoding/ini/ini.odin b/core/encoding/ini/ini.odin
index a119b0f2e..8bf6c6c9a 100644
--- a/core/encoding/ini/ini.odin
+++ b/core/encoding/ini/ini.odin
@@ -1,13 +1,12 @@
// Reader and writer for a variant of the `.ini` file format with `key = value` entries in `[sections]`.
package encoding_ini
-import "base:runtime"
-import "base:intrinsics"
-import "core:strings"
-import "core:strconv"
-import "core:io"
-import "core:os"
-import "core:fmt"
+import "base:runtime"
+import "base:intrinsics"
+import "core:strings"
+import "core:strconv"
+import "core:io"
+import "core:fmt"
_ :: fmt
Options :: struct {
@@ -120,17 +119,6 @@ load_map_from_string :: proc(src: string, allocator: runtime.Allocator, options
return
}
-load_map_from_path :: proc(path: string, allocator: runtime.Allocator, options := DEFAULT_OPTIONS) -> (m: Map, err: runtime.Allocator_Error, ok: bool) {
- data := os.read_entire_file(path, allocator) or_return
- defer delete(data, allocator)
- m, err = load_map_from_string(string(data), allocator, options)
- ok = err == nil
- defer if !ok {
- delete_map(m)
- }
- return
-}
-
save_map_to_string :: proc(m: Map, allocator: runtime.Allocator) -> (data: string) {
b := strings.builder_make(allocator)
_, _ = write_map(strings.to_writer(&b), m)
@@ -191,4 +179,4 @@ write_map :: proc(w: io.Writer, m: Map) -> (n: int, err: io.Error) {
section_index += 1
}
return
-}
+} \ No newline at end of file
diff --git a/core/encoding/ini/ini_os.odin b/core/encoding/ini/ini_os.odin
new file mode 100644
index 000000000..22c6bf7b3
--- /dev/null
+++ b/core/encoding/ini/ini_os.odin
@@ -0,0 +1,20 @@
+#+build !freestanding
+#+build !js
+package encoding_ini
+
+import "base:runtime"
+import "core:os"
+
+load_map_from_path :: proc(path: string, allocator: runtime.Allocator, options := DEFAULT_OPTIONS) -> (m: Map, err: runtime.Allocator_Error, ok: bool) {
+ data, data_err := os.read_entire_file(path, allocator)
+ defer delete(data, allocator)
+ if data_err != nil {
+ return
+ }
+ m, err = load_map_from_string(string(data), allocator, options)
+ ok = err == nil
+ defer if !ok {
+ delete_map(m)
+ }
+ return
+}
diff --git a/core/encoding/xml/xml_os.odin b/core/encoding/xml/xml_os.odin
new file mode 100644
index 000000000..1e94572c6
--- /dev/null
+++ b/core/encoding/xml/xml_os.odin
@@ -0,0 +1,18 @@
+#+build !freestanding
+#+build !js
+package encoding_xml
+
+import "core:os"
+
+// Load an XML file
+load_from_file :: proc(filename: string, options := DEFAULT_OPTIONS, error_handler := default_error_handler, allocator := context.allocator) -> (doc: ^Document, err: Error) {
+ context.allocator = allocator
+ options := options
+
+ data, data_err := os.read_entire_file(filename, allocator)
+ if data_err != nil { return {}, .File_Error }
+
+ options.flags += { .Input_May_Be_Modified }
+
+ return parse_bytes(data, options, filename, error_handler, allocator)
+}
diff --git a/core/encoding/xml/xml_reader.odin b/core/encoding/xml/xml_reader.odin
index 8f8fffe14..6d068466b 100644
--- a/core/encoding/xml/xml_reader.odin
+++ b/core/encoding/xml/xml_reader.odin
@@ -9,13 +9,12 @@ package encoding_xml
- Jeroen van Rijn: Initial implementation.
*/
-import "core:bytes"
-import "core:encoding/entity"
-import "base:intrinsics"
-import "core:mem"
-import "core:os"
-import "core:strings"
-import "base:runtime"
+import "base:runtime"
+import "core:bytes"
+import "core:encoding/entity"
+import "base:intrinsics"
+import "core:mem"
+import "core:strings"
likely :: intrinsics.expect
@@ -373,19 +372,6 @@ parse_string :: proc(data: string, options := DEFAULT_OPTIONS, path := "", error
parse :: proc { parse_string, parse_bytes }
-// Load an XML file
-load_from_file :: proc(filename: string, options := DEFAULT_OPTIONS, error_handler := default_error_handler, allocator := context.allocator) -> (doc: ^Document, err: Error) {
- context.allocator = allocator
- options := options
-
- data, data_ok := os.read_entire_file(filename)
- if !data_ok { return {}, .File_Error }
-
- options.flags += { .Input_May_Be_Modified }
-
- return parse_bytes(data, options, filename, error_handler, allocator)
-}
-
destroy :: proc(doc: ^Document, allocator := context.allocator) {
context.allocator = allocator
if doc == nil { return }