-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtyped_string.go
136 lines (111 loc) · 3.61 KB
/
typed_string.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* @brief Plain text IPC buffer support
*
* @file typed_string.go
*/
/* -----------------------------------------------------------------------------
* Enduro/X Middleware Platform for Distributed Transaction Processing
* Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
* Copyright (C) 2017-2019, Mavimax, Ltd. All Rights Reserved.
* This software is released under one of the following licenses:
* LGPL or Mavimax's license for commercial use.
* See LICENSE file for full text.
*
* C (as designed by Dennis Ritchie and later authors) language code is licensed
* under Enduro/X Modified GNU Affero General Public License, version 3.
* See LICENSE_C file for full text.
* -----------------------------------------------------------------------------
* LGPL license:
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License, version 3 as published
* by the Free Software Foundation;
*
* 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 Lesser General Public License, version 3
* for more details.
*
* You should have received a copy of the Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -----------------------------------------------------------------------------
* A commercial use license is available from Mavimax, Ltd
* -----------------------------------------------------------------------------
*/
package atmi
/*
#cgo pkg-config: atmisrvinteg
#include <xatmi.h>
#include <string.h>
#include <stdlib.h>
#include <ubf.h>
*/
import "C"
import "unsafe"
//UBF Buffer
type TypedString struct {
Buf *ATMIBuf
}
//Return The ATMI buffer to caller
func (u *TypedString) GetBuf() *ATMIBuf {
return u.Buf
}
//Allocate new string buffer
//@param s - source string
func (ac *ATMICtx) NewString(gs string) (*TypedString, ATMIError) {
var buf TypedString
c_val := C.CString(gs)
defer C.free(unsafe.Pointer(c_val))
size := int64(C.strlen(c_val) + 1) /* 1 for EOS. */
if ptr, err := ac.TpAlloc("STRING", "", size); nil != err {
return nil, err
} else {
buf.Buf = ptr
C.strcpy(buf.Buf.C_ptr, c_val)
buf.Buf.TpSetCtxt(ac)
return &buf, nil
}
}
//Get the String Handler from ATMI Buffer
func (ac *ATMICtx) CastToString(abuf *ATMIBuf) (*TypedString, ATMIError) {
var buf TypedString
buf.Buf = abuf
return &buf, nil
}
//Get the string value out from buffer
//@return String value
func (s *TypedString) GetString() string {
ret := C.GoString(s.Buf.C_ptr)
s.Buf.nop()
return ret
}
//Set the string to the buffer
//@param str String value
func (s *TypedString) SetString(gs string) ATMIError {
c_val := C.CString(gs)
defer C.free(unsafe.Pointer(c_val))
new_size := int64(C.strlen(c_val) + 1) /* 1 for EOS. */
if cur_size, err := s.Buf.Ctx.TpTypes(s.Buf, nil, nil); nil != err {
return err
} else {
if cur_size >= new_size {
C.strcpy(s.Buf.C_ptr, c_val)
} else if err := s.Buf.TpRealloc(new_size); nil != err {
return err
} else {
C.strcpy(s.Buf.C_ptr, c_val)
}
}
s.Buf.nop()
return nil
}
///////////////////////////////////////////////////////////////////////////////////
// Wrappers for memory management
///////////////////////////////////////////////////////////////////////////////////
func (u *TypedString) TpRealloc(size int64) ATMIError {
return u.Buf.TpRealloc(size)
}
/* vim: set ts=4 sw=4 et smartindent: */