csvrec

package module
v0.0.0-...-cb91201 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2019 License: Apache-2.0 Imports: 7 Imported by: 2

README

csvrec

GoDoc

Summary

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

Field Types

Struct Tags

Reading and Writing Files

Custom File Reading and Writing

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.

Example csvrec File

// 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

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

View Source
const FieldTag = "csvrecField"

FieldTag is the struct field tag for Fields

View Source
const RecordTag = "csvrecRec"

RecordTag is the struct field tag for Records

Variables

This section is empty.

Functions

func Bool2Str

func Bool2Str(flag bool) string

Bool2Str converts a boolean to a trimed string (containing a 1 or a 0) with a consistant format

func Bool2StrB

func Bool2StrB(flag bool) string

Bool2StrB converts a boolean to a trimed string (containing true or false) with a consistant format

func BoolList2Str

func BoolList2Str(vals []bool) (strs []string)

BoolList2Str converts a list of bools to a list of strings

func CreateFile

func CreateFile(fileName, suffix string, fileStruct interface{}) (err error)

CreateFile fills a struct with csvrec tags from a csvrec formated file

func Dup

func Dup(record []string) (new []string)

Dup creates a copy of a record

func Flt2Str

func Flt2Str(num float64) string

Flt2Str converts a float64 to a trimed string with a consistant format

func Flt2StrPrec

func Flt2StrPrec(num float64, format string) string

Flt2StrPrec converts a float64 to a trimed string with a consistant format

func Flt2Vec2Str

func Flt2Vec2Str(vec [2]float64) string

Flt2Vec2Str converts a [2]float64 array to a colon seperated string

func Flt2VecList2Str

func Flt2VecList2Str(vals [][2]float64) (strs []string)

Flt2VecList2Str converts a list of flt2vecs to a list of strings

func Flt3Vec2Str

func Flt3Vec2Str(vec [3]float64) string

Flt3Vec2Str converts a [3]float64 array to a colon seperated string

func Flt3VecList2Str

func Flt3VecList2Str(vals [][3]float64) (strs []string)

Flt3VecList2Str converts a list of flt3Vecs to a list of strings

func FltList2Str

func FltList2Str(vals []float64) (strs []string)

FltList2Str converts a list of flts to a list of strings

func FltMatrix2Str

func FltMatrix2Str(fltmat [][]float64) string

FltMatrix2Str writes a [][]float64 as a Golang literal string.

func FltMatrixList2Str

func FltMatrixList2Str(vals [][][]float64) (strs []string)

FltMatrixList2Str converts a list of floatMatrixs to a list of strings

func FltPair2Str

func FltPair2Str(pair [2]float64) string

FltPair2Str converts a float pair to a colon seperated string

func FltPairList2Str

func FltPairList2Str(vals [][2]float64) (strs []string)

FltPairList2Str converts a list of fltpairs to a list of strings

func FltRange2Str

func FltRange2Str(rng [3]float64) string

FltRange2Str converts a float pair to a colon seperated string

func FltRangeList2Str

func FltRangeList2Str(vals [][3]float64) (strs []string)

FltRangeList2Str converts a list of fltranges to a list of strings

func GetPercentMinMax

func GetPercentMinMax(minmax [2]float64, percent float64) (val float64)

GetPercentMinMax returns the value percent of the way from min(minmax[0]) to max(minmax[1])

func Int2Str

func Int2Str(num int) string

Int2Str converts and int to a trimed string with a consistant format

func Int2Vec2Str

func Int2Vec2Str(vec [2]int) string

Int2Vec2Str converts a [2]int array to a colon seperated string

func Int2VecList2Str

func Int2VecList2Str(vals [][2]int) (strs []string)

Int2VecList2Str converts a list of int2vecs to a list of strings

func Int3Vec2Str

func Int3Vec2Str(vec [3]int) string

Int3Vec2Str converts a [3]int array to a colon seperated string

func Int3VecList2Str

func Int3VecList2Str(vals [][3]int) (strs []string)

Int3VecList2Str converts a list of int3Vecs to a list of strings

func IntList2Str

func IntList2Str(vals []int) (strs []string)

IntList2Str converts a list of ints to a list of strings

func IntPair2Str

func IntPair2Str(pair [2]int) string

IntPair2Str converts a float pair to a colon seperated string

func IntPairList2Str

func IntPairList2Str(vals [][2]int) (strs []string)

IntPairList2Str converts a list of intpairs to a list of strings

func IntRange2Str

func IntRange2Str(rng [3]int) string

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

func IntRangeList2Str(vals [][3]int) (strs []string)

IntRangeList2Str converts a list of intranges to a list of strings

func NumRecFields

func NumRecFields(label string, rec []string, equality string, expected int) (err error)

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

func ParseFile(fileName string, fileStruct interface{}, finalErr *error) (newFile interface{})

ParseFile is a wrapper around ParseRecMap. ParseFile creates a record map from a csvrec formated file and passes it to ParseRecMap

func ParseRecMap

func ParseRecMap(recMap RecordMap, fileStruct interface{}, finalErr *error) (newFile interface{})

ParseRecMap fills a struct with csvrec tags from a record map

func ReadBool

func ReadBool(field string, finalErr *error) (val bool)

ReadBool converts a string ("1", "0", "true", "false") to a bool and concatenates any errors occrued

func ReadBoolList

func ReadBoolList(fields []string, finalErr *error) (val []bool)

ReadBoolList reads multiple bool records

func ReadCommaStr

func ReadCommaStr(fields []string, finalErr *error) (val string)

ReadCommaStr converts multiple fields into a string with commas

func ReadFlt

func ReadFlt(field string, finalErr *error) (num float64)

ReadFlt converts a string to a float64 and concatenates any errors occrued

func ReadFlt2Vec

func ReadFlt2Vec(field string, finalErr *error) (vals [2]float64)

ReadFlt2Vec converts a colon seperated string of exactly two floats to a [2]float64 array.

func ReadFlt2VecList

func ReadFlt2VecList(fields []string, finalErr *error) (val [][2]float64)

ReadFlt2VecList reads multiple flt2Vec records

func ReadFlt3Vec

func ReadFlt3Vec(field string, finalErr *error) (vals [3]float64)

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

func ReadFlt3VecList(fields []string, finalErr *error) (val [][3]float64)

ReadFlt3VecList reads multiple flt3Vec records

func ReadFltList

func ReadFltList(fields []string, finalErr *error) (val []float64)

ReadFltList reads multiple flt records

func ReadFltMatrix

func ReadFltMatrix(str string, finalErr *error) [][]float64

ReadFltMatrix takes a string representation of a [][]float64 literal and returns a type [][]float64.

func ReadFltMatrixList

func ReadFltMatrixList(fields []string, finalErr *error) (val [][][]float64)

ReadFltMatrixList reads multiple flt matrix fields

func ReadFltPair

func ReadFltPair(field string, finalErr *error) (vals [2]float64)

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

func ReadFltPairList(fields []string, finalErr *error) (val [][2]float64)

ReadFltPairList reads multiple fltPair records

func ReadFltRange

func ReadFltRange(field string, finalErr *error) (vals [3]float64)

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

func ReadFltRangeList(fields []string, finalErr *error) (val [][3]float64)

ReadFltRangeList reads multiple fltRange records

func ReadInt

func ReadInt(field string, finalErr *error) (num int)

ReadInt converts an string to an int and concatenates any errors occrued

func ReadInt2Vec

func ReadInt2Vec(field string, finalErr *error) (vals [2]int)

ReadInt2Vec converts a colon seperated string of exactly two ints to a [2]int array.

func ReadInt2VecList

func ReadInt2VecList(fields []string, finalErr *error) (val [][2]int)

ReadInt2VecList reads multiple int2Vec records

func ReadInt3Vec

func ReadInt3Vec(field string, finalErr *error) (vals [3]int)

ReadInt3Vec converts a colon seperated string of exactly 3 ints to a [3]int array.

func ReadInt3VecList

func ReadInt3VecList(fields []string, finalErr *error) (val [][3]int)

ReadInt3VecList reads multiple int3Vec records

func ReadIntList

func ReadIntList(fields []string, finalErr *error) (val []int)

ReadIntList reads multiple int records

func ReadIntPair

func ReadIntPair(field string, finalErr *error) (vals [2]int)

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

func ReadIntPairList(fields []string, finalErr *error) (val [][2]int)

ReadIntPairList reads multiple intPair records

func ReadIntRange

func ReadIntRange(field string, finalErr *error) (vals [3]int)

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

func ReadIntRangeList(fields []string, finalErr *error) (val [][3]int)

ReadIntRangeList reads multiple intRange records

func ReadStr

func ReadStr(field string, finalErr *error) (val string)

ReadStr converts a string to a trimmed string and concatenates any errors occrued

func ReadStrList

func ReadStrList(fields []string, finalErr *error) (val []string)

ReadStrList reads multiple str records

func TrimList

func TrimList(strs []string) (new []string)

TrimList trims the spaces from a list of strings

func TrimSpace

func TrimSpace(records [][]string)

TrimSpace trims the spaces from all the strings in a [][]string

func WriteCsvRecFile

func WriteCsvRecFile(name string, recMap RecordMap, recNames []string, padded []bool) (err error)

WriteCsvRecFile writes a csvrecord file, one record type at a time in the order given with or without padding each section.

func WriteFile

func WriteFile(name string, recMap RecordMap) (err error)

WriteFile writes a csvrecord file given a map of record lists

func WriteOrderedFile

func WriteOrderedFile(name string, recMap RecordMap, recNames []string) (err error)

WriteOrderedFile writes a csvrecord file, one record type at a time in the order given

func WriteSimpleFile

func WriteSimpleFile(name string, recMap RecordMap, recNames []string) (err error)

WriteSimpleFile writes a csvrecord file, one record type at a time in the order given, with no padding except for a one space after the comma

Types

type RecordMap

type RecordMap map[string][][]string

RecordMap contains lists of all the records read or to be written, mapped to the record labels.

func ReadFile

func ReadFile(name string) (recordMap RecordMap, err error)

ReadFile reads a csv record file and returns a map of the records

func ReadFileAndComments

func ReadFileAndComments(name string) (recordMap RecordMap, err error)

ReadFileAndComments reads a csv record file and its comments (lines starting with //) and

returns a map of the records.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL