|
| 1 | +// Copyright 2011 The csvtogsi Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/csv" |
| 9 | + "io" |
| 10 | + "log" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +// for debugging |
| 16 | +const debug debugging = true // or flip to false |
| 17 | + |
| 18 | +type debugging bool |
| 19 | + |
| 20 | +func (d debugging) Printf(format string, args ...interface{}) { |
| 21 | + if d { |
| 22 | + log.Printf(format, args...) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// ExelCsvNewReader returns a new csv.Reader that reads from r with comma cet to ';'. |
| 27 | +func ExelCsvNewReader(r io.Reader) *csv.Reader { |
| 28 | + // func NewReader(r io.Reader) *Reader |
| 29 | + set := csv.NewReader(r) |
| 30 | + set.Comma = ';' // set Comma to ; |
| 31 | + return set |
| 32 | +} |
| 33 | + |
| 34 | +// RemoveComa returns a string that reads from slice with added 0 digit to always have 'X,XXX'. |
| 35 | +func RemoveComa(slice []string) string { |
| 36 | + if strings.Contains(slice[0], ",") == true { |
| 37 | + c := strings.SplitAfter(slice[0], ",") |
| 38 | + for len(c[1]) < 3 { |
| 39 | + // add 0 to end of string |
| 40 | + s1 := c[1] + strings.Repeat("0", 3-len(c[1])) |
| 41 | + out := strings.Replace(c[0], ",", "", 1) + s1 |
| 42 | + debug.Printf("RemoveComa if for: slice[0] %v ; c[0] %v ; c[1] %v ; s1 %v ; out %v ", slice[0], c[0], c[1], s1, out) |
| 43 | + return out |
| 44 | + } |
| 45 | + } |
| 46 | + out := strings.Replace(slice[0], ",", "", 1) |
| 47 | + debug.Printf("RemoveComa: slice[0] %v ; out %v ", slice[0], out) |
| 48 | + return out |
| 49 | +} |
| 50 | + |
| 51 | +// FixLen returns a string that reads from i with added ... digit to the start to always have x digit. |
| 52 | +func FixLen(i int, x int) string { |
| 53 | + s := strconv.Itoa(i) |
| 54 | + out := FixStringLen(s, x) |
| 55 | + debug.Printf("FixLen: %v ", out) |
| 56 | + return out |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +// FixStringLen returns a string that reads from slice with added ... digit to the start to always have x digit. |
| 61 | +func FixStringLen(slice string, x int) string { |
| 62 | + if len(slice) < x { |
| 63 | + for len(slice) < x { |
| 64 | + // add 0 to start of string |
| 65 | + s1 := strings.Repeat("0", x-len(slice)) + slice |
| 66 | + out := s1 |
| 67 | + debug.Printf("FixStringLen if for: %v ", out) |
| 68 | + return out |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + out := slice |
| 73 | + debug.Printf("FixStringLen: %v ", out) |
| 74 | + return out |
| 75 | + |
| 76 | +} |
0 commit comments