Hi there!
Sorry for my grammar, English is my second language.
Just start learning go a week ago.
Before that C were my choice, basically because it is so fundamental, and has no dependencies.
But it was before i’ve got my first real world “problem”.
Go is great, in any way, now its my language of choice, although i miss some bracketless C constructions.
Now I’m working with simple tool that should parse lookup table from .cube file and after that, apply color corrections from it to an image. I were have to produce some results fast, and have done some thing, it is kinda working on some simple tests, though now I have some time to learn how to write code that would be readable and may be in canonical style.
I know it is a lot of “s###” ) and I’m not going to cry )) please be cruel, it is only part one).
The “Parser” :
‘’’
type CubeLUT struct {
title string
form string
size int
domain [6]float64
data []float64
}
func (cl *CubeLUT) Init(filename string) *CubeLUT {
var (
s scanner.Scanner
f *os.File
err error
)
if f, err = os.Open(filename); err != nil {
log.Fatal(err)
} else {
s.Init(f)
s.Whitespace ^= 1 << '\n' //don't skip new line
}
defer f.Close()
m := make(map[string]string)
d := make([]float64, 3)
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch s.TokenText() {
case "TITLE", "LUT_1D_SIZE", "LUT_3D_SIZE":
key := s.TokenText()
tok = s.Scan()
m[key] = s.TokenText()
case "\n":
case "#": \\skip comments
for s.TokenText() != "\n" {
tok = s.Scan()
}
case "DOMAIN_MIN", "DOMAIN_MAX":
key := s.TokenText()
for i := 0; i < 3; i++ {
keyi := key + [3]string{"_R", "_G", "_B"}[i]
tok = s.Scan()
m[keyi] = s.TokenText()
}
default:
p := [3]float64{}
for i := 0; i < 3; i++ {
p[i], err = strconv.ParseFloat(s.TokenText(), 64)
if err != nil {
log.Fatal(err)
}
tok = s.Scan()
}
d = append(d, p[:]...)
}
}
cl.data = d[3:]
cl.title = "undefined"
if v, ok := m["TITLE"]; ok == true {
cl.title = v
}
cl.form = "undefined"
m[cl.form] = "0"
if _, ok := m["LUT_1D_SIZE"]; ok == true {
cl.form = "LUT_1D_SIZE"
}
if _, ok := m["LUT_3D_SIZE"]; ok == true {
cl.form = "LUT_3D_SIZE"
}
if s, err := strconv.Atoi(m[cl.form]); err != nil {
log.Fatal(err)
} else {
cl.size = s
}
for j := 0; j < 2; j++ {
key := [2]string{"DOMAIN_MIN", "DOMAIN_MAX"}[j]
for i := 0; i < 3; i++ {
keyi := key + [3]string{"_R", "_G", "_B"}[i]
if v, ok := m[keyi]; ok == true {
d, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Fatal(err)
} else {
cl.domain[j*3+i] = d
}
} else {
cl.domain[j*3+i] = [6]float64{0, 0, 0, 1, 1, 1}[j*3+i]
}
}
}
‘’’