Skip to content

Commit 95f3bce

Browse files
committed
add license, rename and separate main, function and struct
1 parent 303f1d6 commit 95f3bce

File tree

4 files changed

+331
-316
lines changed

4 files changed

+331
-316
lines changed

func.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

csvtogsi_test.go func_test.go

File renamed without changes.

main.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// csvtogsi for leica . Convert an Exel .csv file to a leica .gsi file.
6+
package main
7+
8+
import (
9+
"bytes"
10+
"flag"
11+
"fmt"
12+
"io/ioutil"
13+
)
14+
15+
func main() {
16+
file := flag.String("file", "test.csv", "Set the file to import")
17+
flag.Parse()
18+
// ReadInput .
19+
Input, err := ioutil.ReadFile(*file)
20+
if err != nil { // check for error
21+
fmt.Println(err)
22+
}
23+
r := ExelCsvNewReader(bytes.NewReader(Input))
24+
// func (r *Reader) ReadAll() (records [][]string, err error)
25+
out, err := r.ReadAll()
26+
if err != nil { // check for error
27+
fmt.Println(err)
28+
}
29+
// fmt.Println(out)
30+
for i, tt := range out {
31+
if i != 0 { // ignore first line
32+
ligne := i
33+
out := fmt.Sprint("*11", FixLen(ligne, 4), // '*110001'
34+
"+", FixStringLen(RemoveComa(tt[:1]), 16), // '+0000000000005001'
35+
" 81..10+", FixStringLen(RemoveComa(tt[1:2]), 16), // ' 81..10+0000000793905635'
36+
" 82..10+", FixStringLen(RemoveComa(tt[2:3]), 16), // ' 82..10+0000000087528582'
37+
" 83..10+", FixStringLen(RemoveComa(tt[3:4]), 16)) // ' 83..10+0000000000210660'
38+
fmt.Println(out)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)