Skip to content

encoding.csv #

Reader example

import encoding.csv

data := 'x,y\na,b,c\n'
mut parser := csv.new_reader(data)
// read each line
for {
    items := parser.read() or { break }
    println(items)
}

It prints:

['x', 'y']
['a', 'b', 'c']

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

fn csv_reader #

fn csv_reader(cfg RandomAccessReaderConfig) !&RandomAccessReader

csv_reader create a random access csv reader

fn csv_reader_from_string #

fn csv_reader_from_string(in_str string) !&RandomAccessReader

csv_reader_from_string create a csv reader from a string

fn csv_sequential_reader #

fn csv_sequential_reader(cfg SequentialReaderConfig) !&SequentialReader

csv_sequential_reader creates a sequential csv reader

fn decode #

fn decode[T](data string) []T

decode csv to struct

fn 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.

fn 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

fn new_writer #

fn new_writer(config WriterConfig) &Writer

new_writer returns a reference to a Writer

fn (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.

fn (Writer) write #

fn (mut w Writer) write(record []string) !bool

write writes a single record

fn (Writer) str #

fn (mut w Writer) str() string

str returns the writer contents

enum ColumType #

enum ColumType {
	string = 0
	int    = 1
	f32    = 2
}

struct GetCellConfig #

@[params]
struct GetCellConfig {
pub:
	x int
	y int
}

struct GetHeaderConf #

@[params]
struct GetHeaderConf {
pub:
	header_row int // row where to inspect the header
}

struct HeaderItem #

struct HeaderItem {
pub mut:
	label  string
	column int
	htype  ColumType = .string
}

struct 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
	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
}

fn (RandomAccessReader) dispose_csv_reader #

fn (mut cr RandomAccessReader) dispose_csv_reader()

dispose_csv_reader release the resources used by the csv_reader

fn (RandomAccessReader) 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

fn (RandomAccessReader) get_row #

fn (mut cr RandomAccessReader) get_row(y int) ![]string

get_row get a row from the CSV file as a string array

fn (RandomAccessReader) get_cell #

fn (mut cr RandomAccessReader) get_cell(cfg GetCellConfig) !string

get_cell read a single cel nd return a string

fn (RandomAccessReader) get_cellt #

fn (mut cr RandomAccessReader) get_cellt(cfg GetCellConfig) !CellValue

get_cellt read a single cell and return a sum type CellValue

fn (RandomAccessReader) 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

fn (RandomAccessReader) rows_count #

fn (mut cr RandomAccessReader) rows_count() !i64

rows_count count the rows in the csv between start_index and end_index

struct RandomAccessReaderConfig #

@[params]
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
}

struct ReaderConfig #

@[params]
struct ReaderConfig {
pub:
	delimiter u8 = `,`
	comment   u8 = `#`
}

struct 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
}

fn (SequentialReader) dispose_csv_reader #

fn (mut cr SequentialReader) dispose_csv_reader()

dispose_csv_reader release the resources used by the csv_reader

fn (SequentialReader) has_data #

fn (mut cr SequentialReader) has_data() i64

has_data return the bytes available for future readings

fn (SequentialReader) 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

struct SequentialReaderConfig #

@[params]
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
}

struct WriterConfig #

@[params]
struct WriterConfig {
pub:
	use_crlf  bool
	delimiter u8 = `,`
}