Documentation ¶
Overview ¶
Package csvrec implements a library for reading and writing files where each line is a record. Each record is a comma seperated list of fields, with the first field being a record identifier. "//" at the begining of a line indicates the line is a comment. Blank lines are ignored.
For example a file might contain:
// Example File // Shotline, lx, ly, lz, dx, dy, dz, v Shotline, 971.998, -324.021, -207.936, 0.0, 0.0, 1.0, 100.0 Shotline, 1133.2, -594.528, -222.62, 0.0, 0.0, 1.0, 100.0 // Sphere, type, x, r, rs, dpcent, sdcent, dpsurf, sdsurf Sphere, shell, 5.0, 10.0, 10.0, 0.9, .16, 0.5, 0.16 // Cylinder, type, x1, ri1, ro1, dpc1, sdc1, dps1, sds1, x2, ri2, ro2, dpc2, sdc2, dps2, sds2 Cylinder, shell, 0.0, 1.0, 5.0, 0.9, 0.16, 0.9, 0.16, 50.0, 30.0, 40.0, 0.9, 0.16, 0.9, 0.16 // Nnet, type, thetaDir Nnet, shell, thetadir Nnet, crack, myCrackDamageThetas Nnet, thermal, myThermalThetas
Reading ¶
This file contains four types of records and multiple instances of some records. The function call ```recmap, err := ReadCsvRecordFile("examplefile") ``` would return a map where recmap["Shotline"] would return a [2][8]string with all the records. The functions ReadInt, ReadFloat, and ReadBool are provided as a short hand to convert individual fields to the desired type in a consistant manner.
Writing ¶
The WriteCsvRecordFile function takes writes a list of records ([][]string) to a file.
Index ¶
- Constants
- func Bool2Str(flag bool) string
- func Bool2StrB(flag bool) string
- func BoolList2Str(vals []bool) (strs []string)
- func CreateFile(fileName, suffix string, fileStruct interface{}) (err error)
- func Dup(record []string) (new []string)
- func Flt2Str(num float64) string
- func Flt2StrPrec(num float64, format string) string
- func Flt2Vec2Str(vec [2]float64) string
- func Flt2VecList2Str(vals [][2]float64) (strs []string)
- func Flt3Vec2Str(vec [3]float64) string
- func Flt3VecList2Str(vals [][3]float64) (strs []string)
- func FltList2Str(vals []float64) (strs []string)
- func FltMatrix2Str(fltmat [][]float64) string
- func FltMatrixList2Str(vals [][][]float64) (strs []string)
- func FltPair2Str(pair [2]float64) string
- func FltPairList2Str(vals [][2]float64) (strs []string)
- func FltRange2Str(rng [3]float64) string
- func FltRangeList2Str(vals [][3]float64) (strs []string)
- func GetPercentMinMax(minmax [2]float64, percent float64) (val float64)
- func Int2Str(num int) string
- func Int2Vec2Str(vec [2]int) string
- func Int2VecList2Str(vals [][2]int) (strs []string)
- func Int3Vec2Str(vec [3]int) string
- func Int3VecList2Str(vals [][3]int) (strs []string)
- func IntList2Str(vals []int) (strs []string)
- func IntPair2Str(pair [2]int) string
- func IntPairList2Str(vals [][2]int) (strs []string)
- func IntRange2Str(rng [3]int) string
- func IntRangeList2Str(vals [][3]int) (strs []string)
- func NumRecFields(label string, rec []string, equality string, expected int) (err error)
- func PadRecords(records [][]string)
- func ParseFile(fileName string, fileStruct interface{}, finalErr *error) (newFile interface{})
- func ParseRecMap(recMap RecordMap, fileStruct interface{}, finalErr *error) (newFile interface{})
- func ReadBool(field string, finalErr *error) (val bool)
- func ReadBoolList(fields []string, finalErr *error) (val []bool)
- func ReadCommaStr(fields []string, finalErr *error) (val string)
- func ReadFlt(field string, finalErr *error) (num float64)
- func ReadFlt2Vec(field string, finalErr *error) (vals [2]float64)
- func ReadFlt2VecList(fields []string, finalErr *error) (val [][2]float64)
- func ReadFlt3Vec(field string, finalErr *error) (vals [3]float64)
- func ReadFlt3VecList(fields []string, finalErr *error) (val [][3]float64)
- func ReadFltList(fields []string, finalErr *error) (val []float64)
- func ReadFltMatrix(str string, finalErr *error) [][]float64
- func ReadFltMatrixList(fields []string, finalErr *error) (val [][][]float64)
- func ReadFltPair(field string, finalErr *error) (vals [2]float64)
- func ReadFltPairList(fields []string, finalErr *error) (val [][2]float64)
- func ReadFltRange(field string, finalErr *error) (vals [3]float64)
- func ReadFltRangeList(fields []string, finalErr *error) (val [][3]float64)
- func ReadInt(field string, finalErr *error) (num int)
- func ReadInt2Vec(field string, finalErr *error) (vals [2]int)
- func ReadInt2VecList(fields []string, finalErr *error) (val [][2]int)
- func ReadInt3Vec(field string, finalErr *error) (vals [3]int)
- func ReadInt3VecList(fields []string, finalErr *error) (val [][3]int)
- func ReadIntList(fields []string, finalErr *error) (val []int)
- func ReadIntPair(field string, finalErr *error) (vals [2]int)
- func ReadIntPairList(fields []string, finalErr *error) (val [][2]int)
- func ReadIntRange(field string, finalErr *error) (vals [3]int)
- func ReadIntRangeList(fields []string, finalErr *error) (val [][3]int)
- func ReadStr(field string, finalErr *error) (val string)
- func ReadStrList(fields []string, finalErr *error) (val []string)
- func TrimList(strs []string) (new []string)
- func TrimSpace(records [][]string)
- func WriteCsvRecFile(name string, recMap RecordMap, recNames []string, padded []bool) (err error)
- func WriteFile(name string, recMap RecordMap) (err error)
- func WriteOrderedFile(name string, recMap RecordMap, recNames []string) (err error)
- func WriteSimpleFile(name string, recMap RecordMap, recNames []string) (err error)
- type RecordMap
Constants ¶
const FieldTag = "csvrecField"
FieldTag is the struct field tag for Fields
const RecordTag = "csvrecRec"
RecordTag is the struct field tag for Records
Variables ¶
This section is empty.
Functions ¶
func Bool2Str ¶
Bool2Str converts a boolean to a trimed string (containing a 1 or a 0) with a consistant format
func Bool2StrB ¶
Bool2StrB converts a boolean to a trimed string (containing true or false) with a consistant format
func BoolList2Str ¶
BoolList2Str converts a list of bools to a list of strings
func CreateFile ¶
CreateFile fills a struct with csvrec tags from a csvrec formated file
func Flt2StrPrec ¶
Flt2StrPrec converts a float64 to a trimed string with a consistant format
func Flt2Vec2Str ¶
Flt2Vec2Str converts a [2]float64 array to a colon seperated string
func Flt2VecList2Str ¶
Flt2VecList2Str converts a list of flt2vecs to a list of strings
func Flt3Vec2Str ¶
Flt3Vec2Str converts a [3]float64 array to a colon seperated string
func Flt3VecList2Str ¶
Flt3VecList2Str converts a list of flt3Vecs to a list of strings
func FltList2Str ¶
FltList2Str converts a list of flts to a list of strings
func FltMatrix2Str ¶
FltMatrix2Str writes a [][]float64 as a Golang literal string.
func FltMatrixList2Str ¶
FltMatrixList2Str converts a list of floatMatrixs to a list of strings
func FltPair2Str ¶
FltPair2Str converts a float pair to a colon seperated string
func FltPairList2Str ¶
FltPairList2Str converts a list of fltpairs to a list of strings
func FltRange2Str ¶
FltRange2Str converts a float pair to a colon seperated string
func FltRangeList2Str ¶
FltRangeList2Str converts a list of fltranges to a list of strings
func GetPercentMinMax ¶
GetPercentMinMax returns the value percent of the way from min(minmax[0]) to max(minmax[1])
func Int2Vec2Str ¶
Int2Vec2Str converts a [2]int array to a colon seperated string
func Int2VecList2Str ¶
Int2VecList2Str converts a list of int2vecs to a list of strings
func Int3Vec2Str ¶
Int3Vec2Str converts a [3]int array to a colon seperated string
func Int3VecList2Str ¶
Int3VecList2Str converts a list of int3Vecs to a list of strings
func IntList2Str ¶
IntList2Str converts a list of ints to a list of strings
func IntPair2Str ¶
IntPair2Str converts a float pair to a colon seperated string
func IntPairList2Str ¶
IntPairList2Str converts a list of intpairs to a list of strings
func IntRange2Str ¶
IntRange2Str converts a Integer Range to a colon seperated string of 3 integers unless they're all the same and then you get 1 integer
func IntRangeList2Str ¶
IntRangeList2Str converts a list of intranges to a list of strings
func NumRecFields ¶
NumRecFields returns the appropriate error message when given the name of a record type an equality string and the expected value
func PadRecords ¶
func PadRecords(records [][]string)
PadRecords pads the strings in a [][]string so that the columns match
func ParseFile ¶
ParseFile is a wrapper around ParseRecMap. ParseFile creates a record map from a csvrec formated file and passes it to ParseRecMap
func ParseRecMap ¶
ParseRecMap fills a struct with csvrec tags from a record map
func ReadBool ¶
ReadBool converts a string ("1", "0", "true", "false") to a bool and concatenates any errors occrued
func ReadBoolList ¶
ReadBoolList reads multiple bool records
func ReadCommaStr ¶
ReadCommaStr converts multiple fields into a string with commas
func ReadFlt2Vec ¶
ReadFlt2Vec converts a colon seperated string of exactly two floats to a [2]float64 array.
func ReadFlt2VecList ¶
ReadFlt2VecList reads multiple flt2Vec records
func ReadFlt3Vec ¶
ReadFlt3Vec converts a colon seperated string of exactly 3 floats (e.g. 1.23:4.56:7.89) to a [3]float64 array.
func ReadFlt3VecList ¶
ReadFlt3VecList reads multiple flt3Vec records
func ReadFltList ¶
ReadFltList reads multiple flt records
func ReadFltMatrix ¶
ReadFltMatrix takes a string representation of a [][]float64 literal and returns a type [][]float64.
func ReadFltMatrixList ¶
ReadFltMatrixList reads multiple flt matrix fields
func ReadFltPair ¶
ReadFltPair converts a colon seperated string of up to two floats to a [2]float64 array, if there is a single float then the array will have two of the same value.
func ReadFltPairList ¶
ReadFltPairList reads multiple fltPair records
func ReadFltRange ¶
ReadFltRange converts a colon seperated string of 1 (e.g. 1.23) or 3 floats (e.g. 1.23:4.56:7.89) to a [3]float64 array, if there is a single float then the array will have three of the same value.
func ReadFltRangeList ¶
ReadFltRangeList reads multiple fltRange records
func ReadInt2Vec ¶
ReadInt2Vec converts a colon seperated string of exactly two ints to a [2]int array.
func ReadInt2VecList ¶
ReadInt2VecList reads multiple int2Vec records
func ReadInt3Vec ¶
ReadInt3Vec converts a colon seperated string of exactly 3 ints to a [3]int array.
func ReadInt3VecList ¶
ReadInt3VecList reads multiple int3Vec records
func ReadIntList ¶
ReadIntList reads multiple int records
func ReadIntPair ¶
ReadIntPair converts a colon seperated string of up to two ints to a [2]int array, if there is a single int then the array will have two of the same value.
func ReadIntPairList ¶
ReadIntPairList reads multiple intPair records
func ReadIntRange ¶
ReadIntRange converts a colon seperated string of up to two ints to a [2]int array, if there is a single int then the array will have two of the same value.
func ReadIntRangeList ¶
ReadIntRangeList reads multiple intRange records
func ReadStrList ¶
ReadStrList reads multiple str records
func TrimSpace ¶
func TrimSpace(records [][]string)
TrimSpace trims the spaces from all the strings in a [][]string
func WriteCsvRecFile ¶
WriteCsvRecFile writes a csvrecord file, one record type at a time in the order given with or without padding each section.
func WriteOrderedFile ¶
WriteOrderedFile writes a csvrecord file, one record type at a time in the order given
Types ¶
type RecordMap ¶
RecordMap contains lists of all the records read or to be written, mapped to the record labels.
func ReadFileAndComments ¶
ReadFileAndComments reads a csv record file and its comments (lines starting with //) and
returns a map of the records.