Files
herolib/aiprompts/v_core/encoding/csv.md
2025-09-02 07:28:13 +02:00

10 KiB

module csv

Contents

Constants

const endline_cr_len = 1

endline lengths

[Return to contents]

const endline_crlf_len = 2

[Return to contents]

const ram_csv = 1

Type of read buffer

[Return to contents]

const file_csv = 0

[Return to contents]

csv_reader

fn csv_reader(cfg RandomAccessReaderConfig) !&RandomAccessReader

csv_reader create a random access csv reader

[Return to contents]

csv_reader_from_string

fn csv_reader_from_string(in_str string) !&RandomAccessReader

csv_reader_from_string create a csv reader from a string

[Return to contents]

csv_sequential_reader

fn csv_sequential_reader(cfg SequentialReaderConfig) !&SequentialReader

csv_sequential_reader creates a sequential csv reader

[Return to contents]

decode

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

decode csv to struct

[Return to contents]

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.

[Return to contents]

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

[Return to contents]

new_writer

fn new_writer(config WriterConfig) &Writer

new_writer returns a reference to a Writer

[Return to contents]

CellValue

type CellValue = f32 | int | string

[Return to contents]

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.

[Return to contents]

Writer

write

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

write writes a single record

[Return to contents]

str

fn (mut w Writer) str() string

str returns the writer contents

[Return to contents]

ColumType

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

[Return to contents]

GetCellConfig

struct GetCellConfig {
pub:
	x int
	y int
}

[Return to contents]

GetHeaderConf

struct GetHeaderConf {
pub:
	header_row int // row where to inspect the header
}

[Return to contents]

HeaderItem

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

[Return to contents]

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
}

[Return to contents]

dispose_csv_reader

fn (mut cr RandomAccessReader) dispose_csv_reader()

dispose_csv_reader release the resources used by the csv_reader

[Return to contents]

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

[Return to contents]

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

[Return to contents]

get_row

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

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

[Return to contents]

get_cell

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

get_cell read a single cel nd return a string

[Return to contents]

get_cellt

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

get_cellt read a single cell and return a sum type CellValue

[Return to contents]

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

[Return to contents]

rows_count

fn (mut cr RandomAccessReader) rows_count() !i64

rows_count count the rows in the csv between start_index and end_index

[Return to contents]

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
}

[Return to contents]

ReaderConfig

struct ReaderConfig {
pub:
	delimiter u8 = `,`
	comment   u8 = `#`
}

[Return to contents]

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
}

[Return to contents]

dispose_csv_reader

fn (mut cr SequentialReader) dispose_csv_reader()

dispose_csv_reader release the resources used by the csv_reader

[Return to contents]

has_data

fn (mut cr SequentialReader) has_data() i64

has_data return the bytes available for future readings

[Return to contents]

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

[Return to contents]

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
}

[Return to contents]

WriterConfig

struct WriterConfig {
pub:
	use_crlf  bool
	delimiter u8 = `,`
}

[Return to contents]

Powered by vdoc. Generated on: 2 Sep 2025 07:18:04