10 KiB
module csv
Contents
- Constants
- csv_reader
- csv_reader_from_string
- csv_sequential_reader
- decode
- new_reader
- new_reader_from_file
- new_writer
- CellValue
- Reader
- Writer
- ColumType
- GetCellConfig
- GetHeaderConf
- HeaderItem
- RandomAccessReader
- RandomAccessReaderConfig
- ReaderConfig
- SequentialReader
- SequentialReaderConfig
- WriterConfig
Constants
const endline_cr_len = 1
endline lengths
const endline_crlf_len = 2
const ram_csv = 1
Type of read buffer
const file_csv = 0
csv_reader
fn csv_reader(cfg RandomAccessReaderConfig) !&RandomAccessReader
csv_reader create a random access csv reader
csv_reader_from_string
fn csv_reader_from_string(in_str string) !&RandomAccessReader
csv_reader_from_string create a csv reader from a string
csv_sequential_reader
fn csv_sequential_reader(cfg SequentialReaderConfig) !&SequentialReader
csv_sequential_reader creates a sequential csv reader
decode
fn decode[T](data string) []T
decode csv to struct
new_reader
fn new_reader(data string, config ReaderConfig) &Reader
new_reader initializes a Reader with string data to parse and, optionally, a custom delimiter.
new_reader_from_file
fn new_reader_from_file(csv_file_path string, config ReaderConfig) !&Reader
new_reader_from_file create a csv reader from a file
new_writer
fn new_writer(config WriterConfig) &Writer
new_writer returns a reference to a Writer
CellValue
type CellValue = f32 | int | string
Reader
read
fn (mut r Reader) read() ![]string
read reads a row from the CSV data. If successful, the result holds an array of each column's data.
Writer
write
fn (mut w Writer) write(record []string) !bool
write writes a single record
str
fn (mut w Writer) str() string
str returns the writer contents
ColumType
enum ColumType {
string = 0
int = 1
f32 = 2
}
GetCellConfig
struct GetCellConfig {
pub:
x int
y int
}
GetHeaderConf
struct GetHeaderConf {
pub:
header_row int // row where to inspect the header
}
HeaderItem
struct HeaderItem {
pub mut:
label string
column int
htype ColumType = .string
}
RandomAccessReader
struct RandomAccessReader {
pub mut:
index i64
f os.File
f_len i64
is_bom_present bool
start_index i64
end_index i64 = -1
end_line u8 = `\n`
end_line_len int = endline_cr_len // size of the endline rune \n = 1, \r\n = 2
separator u8 = `,` // comma is the default separator
separator_len int = 1 // size of the separator rune
quote u8 = `"` // double quote is the standard quote char
quote_remove bool // if true clear the cell from the quotes
comment u8 = `#` // every line that start with the quote char is ignored
default_cell string = '*' // return this string if out of the csv boundaries
empty_cell string = '#' // retunrn this if empty cell
// ram buffer
mem_buf_type u32 // buffer type 0=File,1=RAM
mem_buf voidptr // buffer used to load chars from file
mem_buf_size i64 // size of the buffer
mem_buf_start i64 = -1 // start index in the file of the read buffer
mem_buf_end i64 = -1 // end index in the file of the read buffer
// csv map for quick access
create_map_csv bool = true // flag to enable the csv map creation
csv_map [][]i64
// header
header_row int = -1 // row index of the header in the csv_map
header_list []HeaderItem // list of the header item
header_map map[string]int // map from header label to column index
}
dispose_csv_reader
fn (mut cr RandomAccessReader) dispose_csv_reader()
dispose_csv_reader release the resources used by the csv_reader
copy_configuration
fn (mut cr RandomAccessReader) copy_configuration(src_cr RandomAccessReader)
copy_configuration copies the configuration from another csv RandomAccessReader this function is a helper for using the RandomAccessReader in multi threaded applications pay attention to the free process
map_csv
fn (mut cr RandomAccessReader) map_csv() !
map_csv create an index of whole csv file to consent random access to every cell in the file
get_row
fn (mut cr RandomAccessReader) get_row(y int) ![]string
get_row get a row from the CSV file as a string array
get_cell
fn (mut cr RandomAccessReader) get_cell(cfg GetCellConfig) !string
get_cell read a single cel nd return a string
get_cellt
fn (mut cr RandomAccessReader) get_cellt(cfg GetCellConfig) !CellValue
get_cellt read a single cell and return a sum type CellValue
build_header_dict
fn (mut cr RandomAccessReader) build_header_dict(cfg GetHeaderConf) !
build_header_dict infer the header, it use the first available row in not row number is passesd it try to infer the type of column using the first available row after the header By default all the column are of the string type
rows_count
fn (mut cr RandomAccessReader) rows_count() !i64
rows_count count the rows in the csv between start_index and end_index
RandomAccessReaderConfig
struct RandomAccessReaderConfig {
pub:
scr_buf voidptr // pointer to the buffer of data
scr_buf_len i64 // if > 0 use the RAM pointed from scr_buf as source of data
file_path string
start_index i64
end_index i64 = -1
mem_buf_size int = 1024 * 64 // default buffer size 64KByte
separator u8 = `,`
comment u8 = `#` // every line that start with the quote char is ignored
default_cell string = '*' // return this string if out of the csv boundaries
empty_cell string // return this string if empty cell
end_line_len int = endline_cr_len // size of the endline rune
quote u8 = `"` // double quote is the standard quote char
quote_remove bool // if true clear the cell from the quotes
create_map_csv bool = true // if true make the map of the csv file
}
ReaderConfig
struct ReaderConfig {
pub:
delimiter u8 = `,`
comment u8 = `#`
}
SequentialReader
struct SequentialReader {
pub mut:
index i64
f os.File
f_len i64
is_bom_present bool
start_index i64
end_index i64 = -1
end_line u8 = `\n`
end_line_len int = endline_cr_len // size of the endline rune \n = 1, \r\n = 2
separator u8 = `,` // comma is the default separator
separator_len int = 1 // size of the separator rune
quote u8 = `"` // double quote is the standard quote char
comment u8 = `#` // every line that start with the quote char is ignored
default_cell string = '*' // return this string if out of the csv boundaries
empty_cell string = '#' // retunrn this if empty cell
// ram buffer
mem_buf_type u32 // buffer type 0=File,1=RAM
mem_buf voidptr // buffer used to load chars from file
mem_buf_size i64 // size of the buffer
mem_buf_start i64 = -1 // start index in the file of the read buffer
mem_buf_end i64 = -1 // end index in the file of the read buffer
ch_buf []u8 = []u8{cap: 1024}
// error management
row_count i64
col_count i64
}
dispose_csv_reader
fn (mut cr SequentialReader) dispose_csv_reader()
dispose_csv_reader release the resources used by the csv_reader
has_data
fn (mut cr SequentialReader) has_data() i64
has_data return the bytes available for future readings
get_next_row
fn (mut cr SequentialReader) get_next_row() ![]string
get_next_row get the next row from the CSV file as a string array
SequentialReaderConfig
struct SequentialReaderConfig {
pub:
scr_buf voidptr // pointer to the buffer of data
scr_buf_len i64 // if > 0 use the RAM pointed by scr_buf as source of data
file_path string
start_index i64
end_index i64 = -1
mem_buf_size int = 1024 * 64 // default buffer size 64KByte
separator u8 = `,`
comment u8 = `#` // every line that start with the comment char is ignored
default_cell string = '*' // return this string if out of the csv boundaries
empty_cell string // return this string if empty cell
end_line_len int = endline_cr_len // size of the endline rune
quote u8 = `"` // double quote is the standard quote char
}
WriterConfig
struct WriterConfig {
pub:
use_crlf bool
delimiter u8 = `,`
}