Beispiel #1
0
//
// WriteRawRow will write row data using separator `sep` for each record.
//
func (writer *Writer) WriteRawRow(row *tabula.Row, sep, esc []byte) (e error) {
	if sep == nil {
		sep = []byte(DefSeparator)
	}
	if esc == nil {
		esc = []byte(DefEscape)
	}

	v := []byte{}
	for x, rec := range *row {
		if x > 0 {
			v = append(v, sep...)
		}

		recV := rec.Bytes()

		if rec.Type() == tabula.TString {
			recV, _ = tekstus.BytesEncapsulate(sep, recV, esc, nil)
		}

		v = append(v, recV...)
	}

	v = append(v, DefEOL)

	_, e = writer.BufWriter.Write(v)

	_ = writer.Flush()

	return e
}
Beispiel #2
0
func testEncasulateToken(t *testing.T, token, line, leftcap, rightcap,
	exp []byte) {

	newline, changed := tekstus.BytesEncapsulate(token, line, leftcap, rightcap)

	assert(t, string(exp), string(newline), changed)
}
Beispiel #3
0
/*
WriteRow dump content of Row to file using format in metadata.
*/
func (writer *Writer) WriteRow(row *tabula.Row, recordMd []MetadataInterface) (
	e error,
) {
	nRecord := row.Len()
	v := []byte{}
	esc := []byte(DefEscape)

	for i := range writer.OutputMetadata {
		md := writer.OutputMetadata[i]

		// find the input index based on name on record metadata.
		rIdx, mdMatch := FindMetadata(&md, recordMd)

		// No input metadata matched? skip it too.
		if rIdx >= nRecord {
			continue
		}

		// If input column is ignored, continue to next record.
		if mdMatch != nil && mdMatch.GetSkip() {
			continue
		}

		recV := (*row)[rIdx].Bytes()
		lq := md.GetLeftQuote()

		if "" != lq {
			v = append(v, []byte(lq)...)
		}

		rq := md.GetRightQuote()
		sep := md.GetSeparator()

		// Escape the escape character itself.
		if md.T == tabula.TString {
			recV, _ = tekstus.BytesEncapsulate(esc, recV, esc, nil)
		}

		// Escape the right quote in field content before writing it.
		if "" != rq && md.T == tabula.TString {
			recV, _ = tekstus.BytesEncapsulate([]byte(rq), recV,
				esc, nil)
		} else {
			// Escape the separator
			if "" != sep && md.T == tabula.TString {
				recV, _ = tekstus.BytesEncapsulate([]byte(sep),
					recV, esc, nil)
			}
		}

		v = append(v, recV...)

		if "" != rq {
			v = append(v, []byte(rq)...)
		}

		if "" != sep {
			v = append(v, []byte(sep)...)
		}
	}

	v = append(v, DefEOL)

	_, e = writer.BufWriter.Write(v)

	return e
}