forked from rainliu/sip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeStampParser.go
91 lines (73 loc) · 2.06 KB
/
TimeStampParser.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package parser
import (
"sip/core"
"sip/header"
"strconv"
)
/** SIPParser for TimeStamp header.
*/
type TimeStampParser struct {
HeaderParser
}
/** Creates a new instance of TimeStampParser
* @param timeStamp the header to parse
*/
func NewTimeStampParser(timeStamp string) *TimeStampParser {
this := &TimeStampParser{}
this.HeaderParser.super(timeStamp)
return this
}
/** Constructor
* @param lexer the lexer to use to parse the header
*/
func NewTimeStampParserFromLexer(lexer core.Lexer) *TimeStampParser {
this := &TimeStampParser{}
this.HeaderParser.superFromLexer(lexer)
return this
}
/** parse the String message
* @return SIPHeader (TimeStamp object)
* @throws SIPParseException if the message does not respect the spec.
*/
func (this *TimeStampParser) Parse() (sh header.Header, ParseException error) {
timeStamp := header.NewTimeStamp()
var ch byte
var firstNumber, secondNumber int
lexer := this.GetLexer()
this.HeaderName(TokenTypes_TIMESTAMP)
timeStamp.SetHeaderName(core.SIPHeaderNames_TIMESTAMP)
lexer.SPorHT()
if firstNumber, ParseException = lexer.Number(); ParseException != nil {
return nil, ParseException
}
var ts float64
if ch, _ = lexer.LookAheadK(0); ch == '.' {
lexer.Match('.')
if secondNumber, ParseException = lexer.Number(); ParseException != nil {
return nil, ParseException
}
s := strconv.Itoa(firstNumber) + "." + strconv.Itoa(secondNumber)
ts, _ = strconv.ParseFloat(s, 32)
} else {
ts = float64(firstNumber)
}
timeStamp.SetTimeStamp(float32(ts))
lexer.SPorHT()
if ch, _ = lexer.LookAheadK(0); ch != '\n' {
if firstNumber, ParseException = lexer.Number(); ParseException != nil {
return nil, ParseException
}
if ch, _ = lexer.LookAheadK(0); ch == '.' {
lexer.Match('.')
if secondNumber, ParseException = lexer.Number(); ParseException != nil {
return nil, ParseException
}
s := strconv.Itoa(firstNumber) + "." + strconv.Itoa(secondNumber)
ts, _ = strconv.ParseFloat(s, 32)
} else {
ts = float64(firstNumber)
}
timeStamp.SetDelay(float32(ts))
}
return timeStamp, nil
}