mirror of
https://github.com/odin-lang/Odin.git
synced 2026-01-08 14:03:14 +00:00
core:os -> core:os/os for CSV, INI, and XML
Also had to vendor `core:encoding/ini` into `core:os/os2` for the user directories on *nix, as it used that package to read `~/.config/user-dirs.dirs`, causing an import cycle.
This commit is contained in:
@@ -6,7 +6,7 @@ Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:encoding/csv"
|
||||
import "core:os"
|
||||
import os "core:os/os2"
|
||||
|
||||
// Requires keeping the entire CSV file in memory at once
|
||||
iterate_csv_from_string :: proc(filename: string) {
|
||||
@@ -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.printfln("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("Error %v opening file: %v", err, filename)
|
||||
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,24 @@ Example:
|
||||
r.trim_leading_space = true
|
||||
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, context.allocator)
|
||||
if csv_err == nil {
|
||||
csv.reader_init_with_string(&r, string(csv_data))
|
||||
} else {
|
||||
fmt.printfln("Unable to open file: %v", filename)
|
||||
fmt.printfln("Unable to open file: %v. Error: %v", filename, csv_err)
|
||||
return
|
||||
}
|
||||
defer delete(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)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// 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 os "core:os/os2"
|
||||
import "core:fmt"
|
||||
_ :: fmt
|
||||
|
||||
Options :: struct {
|
||||
@@ -121,8 +121,11 @@ load_map_from_string :: proc(src: string, allocator: runtime.Allocator, options
|
||||
}
|
||||
|
||||
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
|
||||
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 {
|
||||
@@ -191,4 +194,4 @@ write_map :: proc(w: io.Writer, m: Map) -> (n: int, err: io.Error) {
|
||||
section_index += 1
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,13 @@ 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 os "core:os/os2"
|
||||
import "core:strings"
|
||||
|
||||
likely :: intrinsics.expect
|
||||
|
||||
@@ -377,8 +377,8 @@ load_from_file :: proc(filename: string, options := DEFAULT_OPTIONS, error_handl
|
||||
context.allocator = allocator
|
||||
options := options
|
||||
|
||||
data, data_ok := os.read_entire_file(filename)
|
||||
if !data_ok { return {}, .File_Error }
|
||||
data, data_err := os.read_entire_file(filename, allocator)
|
||||
if data_err != nil { return {}, .File_Error }
|
||||
|
||||
options.flags += { .Input_May_Be_Modified }
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#+build !windows
|
||||
package os2
|
||||
|
||||
import "base:intrinsics"
|
||||
import "base:runtime"
|
||||
import "core:strings"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user