forked from cgrates/cgrates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixedwidth.go
66 lines (57 loc) · 2.09 KB
/
fixedwidth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2013 ITsysCOM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package cdrexporter
import (
"fmt"
"strconv"
)
type DeanCdrWriter struct{}
func (dcw *DeanCdrWriter) Write(cdr []string) error {
return nil
}
// Used as generic function logic for various fields
// Attributes
// source - the base source
// maxLen - the maximum field lenght
// stripAllowed - whether we allow stripping of chars in case of source bigger than the maximum allowed
// lStrip - if true, strip from beginning of the string
// lPadding - if true, add chars at the beginning of the string
// paddingChar - the character wich will be used to fill the existing
func filterField(source string, maxLen int, stripAllowed, lStrip, lPadding, padWithZero bool) (string, error) {
if len(source) == maxLen { // the source is exactly the maximum length
return source, nil
}
if len(source) > maxLen { //the source is bigger than allowed
if !stripAllowed {
return "", fmt.Errorf("source %s is bigger than the maximum allowed length %s", source, maxLen)
}
if !lStrip {
return source[:maxLen], nil
} else {
diffIndx := len(source) - maxLen
return source[diffIndx:], nil
}
} else { //the source is smaller as the maximum allowed
paddingString := "%"
if padWithZero {
paddingString += "0" // it will not work for rPadding but this is not needed
}
if !lPadding {
paddingString += "-"
}
paddingString += strconv.Itoa(maxLen) + "s"
return fmt.Sprintf(paddingString, source), nil
}
}