-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtyped_ubf.go
1868 lines (1474 loc) · 45.8 KB
/
typed_ubf.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @brief Unified Buffer Format (UBF) - Key value protocol buffer support
*
* @file typed_ubf.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 <errno.h>
#include <xatmi.h>
#include <string.h>
#include <stdlib.h>
#include <ubf.h>
#include <ndebug.h>
#include <odebug.h>
#include <oubf.h>
#include <oatmi.h>
#include <sys_unix.h>
#include <ndrstandard.h>
//Get the UBF Error code
static int WrapBerror(TPCONTEXT_T *p_ctx) {
return OBerror(p_ctx);
}
//Get the value with buffer allocation
static char * c_Bget_str (UBFH * p_ub, BFLDID bfldid, BFLDOCC occ,
BFLDLEN *len, int *err_code)
{
char *ret = malloc(NDRX_MSGSIZEMAX);
*len = NDRX_MSGSIZEMAX;
*err_code = 0;
if (NULL==ret)
{
*err_code = 1; //memory error
}
else
{
if (0!=Bget (p_ub, bfldid, occ, ret, len))
{
*err_code = 2; //Buffer
free(ret);
ret = NULL;
}
}
return ret;
}
//Get integer size
static int c_sizeof_BFLDID(void)
{
return sizeof(BFLDID);
}
//Go proxy function for expression evaluator
extern long go_expr_callback_proxy(char *buf, char *funcname);
//Proxy function for expression callback
static long c_expr_callback_proxy(UBFH *p_ub, char *funcname)
{
//Call the service entry
return go_expr_callback_proxy((char *)p_ub, funcname);
}
static int c_proxy_Bboolsetcbf(TPCONTEXT_T *p_ctx, char *funcname)
{
return OBboolsetcbf(p_ctx, funcname, c_expr_callback_proxy);
}
//Will run the Btreefree in temp context
static void go_Btreefree(char *ptr)
{
// Allocate new context + set it...
TPCONTEXT_T c = tpnewctxt(0, 1);
Btreefree(ptr);
tpfreectxt(c);
}
//Reset location infos
static void reset_loc_info(Bfld_loc_info_t *loc)
{
memset((void *)loc, 0, sizeof(Bfld_loc_info_t));
}
typedef struct bfprintcb_data bfprintcb_data_t;
struct bfprintcb_data
{
TPCONTEXT_T *p_ctx;
char *buf;
long cur_offset;
long size;
};
//Callback for writting data to
//@param buffer output buffer to write
//@param datalen printed data including EOS
//@param dataptr1 custom data pointer, in this case bfprintcb_data_t
//@return EXSUCCEED/EXFAIL
static int bfprintcb_writef(char **buffer, long datalen, void *dataptr1,
int *do_write, FILE *outf, BFLDID fid)
{
int ret = EXSUCCEED;
bfprintcb_data_t *data = (bfprintcb_data_t *)dataptr1;
//-1 for skipping the EOS
if (datalen > (data->size - data->cur_offset)-1)
{
OUBF_LOG(data->p_ctx, log_error, "Output buffer full: free %ld, new data: %ld",
(long)((data->size - data->cur_offset)-1), datalen);
EXFAIL_OUT(ret);
}
//now copy off the data
if (data->cur_offset>0)
{
data->cur_offset--;
}
memcpy(data->buf + data->cur_offset, *buffer, datalen);
data->cur_offset+=datalen;
out:
return ret;
}
//Print UBF buffer to allocated string
//@param p_ctx ATMI Context
//@param p_ub buffer to print
//@return ptr to C allocate string with print data terminated with EOS
// or NULL in case of error.
static char * BPrintStrC(TPCONTEXT_T *p_ctx, UBFH * p_ub)
{
bfprintcb_data_t data;
memset(&data, 0, sizeof(data));
//Allocate the buffer, so lets allocate some heavy buffer for the string..
//Also note that while we do not use the buffer, the good os actually won't
//allocate any real resources. Thus I guess no problem here
//just a virtual memory...
data.size = Bsizeof(p_ub) * MAXTIDENT;
data.buf = malloc(data.size);
data.p_ctx = p_ctx;
if (NULL==data.buf)
{
int err = errno;
OUBF_LOG(p_ctx, log_error, "Failed to allocate %ld bytes for print buffer: %s",
data.size, strerror(err));
goto out;
}
if (EXSUCCEED!=OBfprintcb(p_ctx, p_ub, bfprintcb_writef, (char *)&data))
{
int err = errno;
OUBF_LOG(p_ctx, log_error, "Failed to print to buffer / callback fail!");
free(data.buf);
data.buf = NULL;
goto out;
}
out:
return data.buf;
}
extern void c_copy_data_to_go(const void *go_data, char *c_data, long nbytes);
extern void c_copy_data_to_c(char *c_data, const void *go_data, long nbytes);
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
//Field types
const (
BFLD_MIN = 0
BFLD_SHORT = 0
BFLD_LONG = 1
BFLD_CHAR = 2
BFLD_FLOAT = 3
BFLD_DOUBLE = 4
BFLD_STRING = 5
BFLD_CARRAY = 6
BFLD_MAX = 6
BFLD_INT = 7 /* used for views only */
)
//Error codes
const (
BMINVAL = 0 /* min error */
BERFU0 = 1
BALIGNERR = 2
BNOTFLD = 3
BNOSPACE = 4
BNOTPRES = 5
BBADFLD = 6
BTYPERR = 7
BEUNIX = 8
BBADNAME = 9
BMALLOC = 10
BSYNTAX = 11
BFTOPEN = 12
BFTSYNTAX = 13
BEINVAL = 14
BERFU1 = 15
BBADTBL = 16
BBADVIEW = 17
BVFSYNTAX = 18
BVFOPEN = 19
BBADACM = 20
BNOCNAME = 21
BEBADOP = 22
BMAXVAL = 22 /* max error */
)
const (
BBADFLDID = 0
BFIRSTFLDID = 0
)
///////////////////////////////////////////////////////////////////////////////////
// Buffer def, typedefs
///////////////////////////////////////////////////////////////////////////////////
//UBF Buffer
type TypedUBF struct {
Buf *ATMIBuf
}
//Return The ATMI buffer to caller
func (u *TypedUBF) GetBuf() *ATMIBuf {
return u.Buf
}
//Compiled Expression Tree
type ExprTree struct {
//All object which have finalizer we need nop func to defer so that
//during the c call the GC does not collect the object...
gcoff int
c_ptr *C.char
}
//Field location infos
type BFldLocInfo struct {
loc C.Bfld_loc_info_t
}
///////////////////////////////////////////////////////////////////////////////////
// Error Handlers
///////////////////////////////////////////////////////////////////////////////////
//ATMI Error type
type ubfError struct {
code int
message string
}
//ATMI error interface
type UBFError interface {
Error() string
Code() int
Message() string
}
//Generate UBF error, read the codes
func (ac *ATMICtx) NewUBFError() UBFError {
var err ubfError
err.code = int(C.WrapBerror(&ac.c_ctx))
err.message = C.GoString(C.OBstrerror(&ac.c_ctx, C.WrapBerror(&ac.c_ctx)))
return err
}
//Build a custom error
//@param err Error buffer to build
//@param code Error code to setup
//@param msg Error message
func NewCustomUBFError(code int, msg string) UBFError {
var err ubfError
err.code = code
err.message = msg
return err
}
//Standard error interface
func (e ubfError) Error() string {
return fmt.Sprintf("%d: %s", e.code, e.message)
}
//code getter
func (e ubfError) Code() int {
return e.code
}
//message getter
func (e ubfError) Message() string {
return e.message
}
///////////////////////////////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////////////////////////////
type UBFExprFunc func(buf *TypedUBF, funcname string) int64
var exprfuncmap map[string]UBFExprFunc //callback mapping for UBF expression functions to go
///////////////////////////////////////////////////////////////////////////////////
// UBF API
///////////////////////////////////////////////////////////////////////////////////
//Do nothing, to trick the GC
func (expr *ExprTree) nop() int {
expr.gcoff++
return expr.gcoff
}
//Get the field len
//@param fldid Field ID
//@param occ Field occurance
//@return FIeld len, UBF error
func (u *TypedUBF) BLen(bfldid int, occ int) (int, UBFError) {
ret := C.OBlen(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid), C.BFLDOCC(occ))
if FAIL == ret {
return FAIL, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return int(ret), nil
}
//Delete the field from buffer
//@param fldid Field ID
//@param occ Field occurance
//@return UBF error
func (u *TypedUBF) BDel(bfldid int, occ int) UBFError {
ret := C.OBdel(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid), C.BFLDOCC(occ))
if FAIL == ret {
return u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return nil
}
//Make a project copy of the fields (leave only those in array)
//@return UBF error
func (u *TypedUBF) BProj(fldlist []int) UBFError {
c_fldidsize := int(C.c_sizeof_BFLDID())
c_val := C.malloc(C.size_t(c_fldidsize * (len(fldlist) + 1)))
if nil == c_val {
return NewCustomUBFError(BEUNIX, "Cannot alloc memory")
}
defer C.free(c_val)
var i int
for i = 0; i < len(fldlist); i++ {
*(*C.BFLDID)(unsafe.Pointer(uintptr(c_val) + uintptr(i*c_fldidsize))) =
C.BFLDID(fldlist[i])
}
//Set last field to BBADFLDID
*(*C.BFLDID)(unsafe.Pointer(uintptr(c_val) + uintptr(i*c_fldidsize))) = C.BFLDID(BBADFLDID)
if ret := C.OBproj(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)),
(*C.BFLDID)(unsafe.Pointer(c_val))); ret != SUCCEED {
return u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return nil
}
//Make a project copy of the fields (leave only those in array)
//The terminator in the fildlist array are not required. The list shall
//contain fields only to copy. See *Bprojcpy(3)* for more details.
//NOTE! The dest buffer is erased before copying new data to
//@param dest is destination buffer
//@param src is source buffer to copy from
//@param fldlist list of fields to copy
//@return UBF error
func (ac *ATMICtx) BProjCpy(dest *TypedUBF, src *TypedUBF, fldlist []int) UBFError {
c_fldidsize := int(C.c_sizeof_BFLDID())
c_val := C.malloc(C.size_t(c_fldidsize * (len(fldlist) + 1)))
if nil == c_val {
return NewCustomUBFError(BEUNIX, "Cannot alloc memory")
}
defer C.free(c_val)
var i int
for i = 0; i < len(fldlist); i++ {
*(*C.BFLDID)(unsafe.Pointer(uintptr(c_val) + uintptr(i*c_fldidsize))) =
C.BFLDID(fldlist[i])
}
//Set last field to BBADFLDID
*(*C.BFLDID)(unsafe.Pointer(uintptr(c_val) + uintptr(i*c_fldidsize))) = C.BFLDID(BBADFLDID)
if ret := C.OBprojcpy(&ac.c_ctx, (*C.UBFH)(unsafe.Pointer(dest.Buf.C_ptr)),
(*C.UBFH)(unsafe.Pointer(src.Buf.C_ptr)),
(*C.BFLDID)(unsafe.Pointer(c_val))); ret != SUCCEED {
return ac.NewUBFError()
}
dest.Buf.nop()
src.Buf.nop()
ac.nop()
return nil
}
//Return field ID
//@param fldnm Field name
//@return Field ID, UBF error
func (ac *ATMICtx) BFldId(fldnm string) (int, UBFError) {
c_fldnm := C.CString(fldnm)
defer C.free(unsafe.Pointer(c_fldnm))
ret := C.OBfldid(&ac.c_ctx, c_fldnm)
if FAIL == ret {
return BBADFLDID, ac.NewUBFError()
}
ac.nop()
return int(ret), nil
}
//Get field name
//@param bfldid Field ID
//@return Field name (or "" if error), UBF error
func (ac *ATMICtx) BFname(bfldid int) (string, UBFError) {
ret := C.OBfname(&ac.c_ctx, C.BFLDID(bfldid))
if nil == ret {
return "", ac.NewUBFError()
}
ac.nop()
return C.GoString(ret), nil
}
//Check for field presence in buffer
//@param fldid Field ID
//@param occ Field occurance
//@return true/false present/not present
func (u *TypedUBF) BPres(bfldid int, occ int) bool {
ret := C.OBpres(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)),
C.BFLDID(bfldid), C.BFLDOCC(occ))
if 1 == ret {
return true
}
u.Buf.nop()
return false
}
//Copy buffer
//@param dest Destination UBF buffer
//@param src Source UBF buffer
//@return UBF error
func (ac *ATMICtx) BCpy(dest *TypedUBF, src *TypedUBF) UBFError {
if ret := C.OBcpy(&ac.c_ctx, (*C.UBFH)(unsafe.Pointer(dest.Buf.C_ptr)),
(*C.UBFH)(unsafe.Pointer(src.Buf.C_ptr))); SUCCEED != ret {
return ac.NewUBFError()
}
dest.Buf.nop()
src.Buf.nop()
ac.nop()
return nil
}
//Iterate over the buffer
//NOTE: This is not multiple context safe. It stores iteration state internally
//@param first TRUE start iteration, FALSE continue iteration
//@return Field ID, Field Occurrance, UBF Error
func (u *TypedUBF) BNext(first bool) (int, int, UBFError) {
var fldid C.BFLDID
var occ C.BFLDOCC
if first {
fldid = BFIRSTFLDID
} else {
//Get next saved in internal ptr in library
fldid = FAIL
}
if ret := C.OBnext(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)),
&fldid, &occ, nil, nil); 1 != ret {
return FAIL, FAIL, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return int(fldid), int(occ), nil
}
//Initialize/re-initialize UBF buffer
//@param u UBF buffer
//@param ulen lenght of the buffer
//@return UBF error
func (ac *ATMICtx) BInit(u *TypedUBF, ulen int64) UBFError {
if ret := C.OBinit(&ac.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)),
C.BFLDLEN(ulen)); SUCCEED != ret {
return ac.NewUBFError()
}
ac.nop()
u.Buf.nop()
return nil
}
//Allocate the UBF buffer
//@param size Buffer size in bytes
//@return UBF Handler, ATMI Error
func (ac *ATMICtx) UBFAlloc(size int64) (TypedUBF, ATMIError) {
var err ATMIError
var buf TypedUBF
buf.Buf, err = ac.TpAlloc("UBF", "", size)
ac.nop()
return buf, err
}
//Get the UBF Handler
func (ac *ATMICtx) CastToUBF(abuf *ATMIBuf) (*TypedUBF, ATMIError) {
var buf TypedUBF
//TODO: Check the buffer type!
buf.Buf = abuf
abuf.nop()
ac.nop()
return &buf, nil
}
//Get the field form buffer. This returns the interface to underlaying type
//@param bfldid Field ID
//@param occ Occurrance
//@return interface to value, UBF error
func (u *TypedUBF) BGet(bfldid int, occ int) (interface{}, UBFError) {
/* Determinte the type of the buffer */
switch u.Buf.Ctx.BFldType(bfldid) {
case BFLD_SHORT:
var c_val C.short
if ret := C.OBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return int16(c_val), nil
case BFLD_LONG:
var c_val C.long
if ret := C.OBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return int64(c_val), nil
case BFLD_CHAR: /* This is single byte... */
var c_val C.char
if ret := C.OBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return byte(c_val), nil
case BFLD_FLOAT:
var c_val C.float
if ret := C.OBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return float32(c_val), nil
case BFLD_DOUBLE:
var c_val C.double
if ret := C.OBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
return float64(c_val), nil
case BFLD_STRING:
var c_len C.BFLDLEN
c_val := C.malloc(C.size_t(ATMIMsgSizeMax()))
c_len = C.BFLDLEN(ATMIMsgSizeMax())
if nil == c_val {
return nil, NewCustomUBFError(BEUNIX, "Cannot alloc memory")
}
defer C.free(c_val)
if ret := C.OBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(c_val), &c_len); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return C.GoString((*C.char)(c_val)), nil
case BFLD_CARRAY:
var c_len C.BFLDLEN
c_val := C.malloc(C.size_t(ATMIMsgSizeMax()))
c_len = C.BFLDLEN(ATMIMsgSizeMax())
if nil == c_val {
return nil, NewCustomUBFError(BEUNIX, "Cannot alloc memory")
}
defer C.free(c_val)
if ret := C.OBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(c_val), &c_len); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
g_val := make([]byte, c_len)
/*
for i := 0; i < int(c_len); i++ {
g_val[i] = byte(*(*C.char)(unsafe.Pointer(uintptr(c_val) + uintptr(i))))
}
*/
if c_len > 0 {
C.c_copy_data_to_go(unsafe.Pointer(&g_val[0]), (*C.char)(c_val), C.long(c_len))
}
u.Buf.nop()
return g_val, nil
}
/* Default case... */
return nil, NewCustomUBFError(BEINVAL, "Invalid field")
}
//Return int16 value from buffer
//@param bfldid Field ID
//@param occ Occurrance
//@return int16 val, UBF error
func (u *TypedUBF) BGetInt16(bfldid int, occ int) (int16, UBFError) {
var c_val C.short
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil, BFLD_SHORT); ret != SUCCEED {
return 0, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return int16(c_val), nil
}
//Return int64 value from buffer
//@param bfldid Field ID
//@param occ Occurrance
//@return int64 val, UBF error
func (u *TypedUBF) BGetInt64(bfldid int, occ int) (int64, UBFError) {
var c_val C.long
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil, BFLD_LONG); ret != SUCCEED {
return 0, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return int64(c_val), nil
}
//Return int (basicaly C long (int64) casted to) value from buffer
//@param bfldid Field ID
//@param occ Occurrance
//@return int64 val, UBF error
func (u *TypedUBF) BGetInt(bfldid int, occ int) (int, UBFError) {
var c_val C.long
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil, BFLD_LONG); ret != SUCCEED {
return 0, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return int(c_val), nil
}
//Return byte (c char) value from buffer
//@param bfldid Field ID
//@param occ Occurrance
//@return byte val, UBF error
func (u *TypedUBF) BGetByte(bfldid int, occ int) (byte, UBFError) {
var c_val C.char
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil, BFLD_CHAR); ret != SUCCEED {
return 0, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return byte(c_val), nil
}
//Get float value from UBF buffer, see CBget(3)
//@param bfldid Field ID
//@param occ Occurrance
//@return float, UBF error
func (u *TypedUBF) BGetFloat32(bfldid int, occ int) (float32, UBFError) {
var c_val C.float
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil, BFLD_FLOAT); ret != SUCCEED {
return 0, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return float32(c_val), nil
}
//Get double value
//@param bfldid Field ID
//@param occ Occurrance
//@return double, UBF error
func (u *TypedUBF) BGetFloat64(bfldid int, occ int) (float64, UBFError) {
var c_val C.double
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), nil, BFLD_DOUBLE); ret != SUCCEED {
return 0, u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return float64(c_val), nil
}
//Get string value
//@param bfldid Field ID
//@param occ Occurrance
//@return string val, UBF error
func (u *TypedUBF) BGetString(bfldid int, occ int) (string, UBFError) {
var c_len C.BFLDLEN
c_val := C.malloc(C.size_t(ATMIMsgSizeMax()))
c_len = C.BFLDLEN(ATMIMsgSizeMax())
if nil == c_val {
return "", NewCustomUBFError(BEUNIX, "Cannot alloc memory")
}
defer C.free(c_val)
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(c_val), &c_len, BFLD_STRING); ret != SUCCEED {
return "", u.Buf.Ctx.NewUBFError()
}
u.Buf.nop()
return C.GoString((*C.char)(c_val)), nil
}
//Get string value
//@param bfldid Field ID
//@param occ Occurrance
//@return string val, UBF error
func (u *TypedUBF) BGetByteArr(bfldid int, occ int) ([]byte, UBFError) {
var c_len C.BFLDLEN
c_val := C.malloc(C.size_t(ATMIMsgSizeMax()))
c_len = C.BFLDLEN(ATMIMsgSizeMax())
if nil == c_val {
return nil, NewCustomUBFError(BEUNIX, "Cannot alloc memory")
}
defer C.free(c_val)
if ret := C.OCBget(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
C.BFLDOCC(occ), (*C.char)(c_val), &c_len, BFLD_CARRAY); ret != SUCCEED {
return nil, u.Buf.Ctx.NewUBFError()
}
g_val := make([]byte, c_len)
/*
for i := 0; i < int(c_len); i++ {
g_val[i] = byte(*(*C.char)(unsafe.Pointer(uintptr(c_val) + uintptr(i))))
}
*/
if c_len > 0 {
C.c_copy_data_to_go(unsafe.Pointer(&g_val[0]), (*C.char)(c_val), C.long(c_len))
}
u.Buf.nop()
return g_val, nil
}
//Change field in buffer
//@param bfldid Field ID
//@param ival Input value
//@return UBF Error
func (u *TypedUBF) BChg(bfldid int, occ int, ival interface{}) UBFError {
return u.BChgCombined(bfldid, occ, ival, false)
}
//Fast add of filed to buffer (assuming buffer not changed and adding the same
//type of field. NOTE ! Types must be matched with UBF field type
//@param bfldid field id to add
//@param ival value to add
//@param loc location data (last saved or new data) - initialized by first flag
//@param first set to true, if 'loc' is not inialised
//@return UBF error or nil
func (u *TypedUBF) BAddFast(bfldid int, ival interface{}, loc *BFldLocInfo, first bool) UBFError {
if nil == loc {
return NewCustomUBFError(BEINVAL, "loc cannot be nil!")
}
if first {
C.reset_loc_info(&loc.loc)
}
fldtyp := u.GetBuf().Ctx.BFldType(bfldid)
switch ival.(type) {
case int8,
int16,
uint8,
uint16:
//Validate type code
var val int16
if BFLD_SHORT != fldtyp {
return NewCustomUBFError(BEINVAL, fmt.Sprintf("expected BFLD_SHORT got: %d",
fldtyp))
}
switch ival.(type) {
case int8:
val = int16(ival.(int8))
case int16:
val = int16(ival.(int16))
case uint8:
val = int16(ival.(uint8))
case uint16:
val = int16(ival.(uint16))
}
c_val := C.short(val)
if ret := C.OBaddfast(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
(*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), 0, &loc.loc); ret != SUCCEED {
return u.Buf.Ctx.NewUBFError()
}
break
case int32,
int,
uint,
int64,
uint32,
uint64:
/* Cast the value to integer... */
var val int64
if BFLD_LONG != fldtyp {
return NewCustomUBFError(BEINVAL, fmt.Sprintf("expected BFLD_LONG got: %d",
fldtyp))
}
switch ival.(type) {
case int:
val = int64(ival.(int))
case int32:
val = int64(ival.(int32))
case int64:
val = int64(ival.(int64))
case uint:
val = int64(ival.(uint))
case uint32:
val = int64(ival.(uint32))
case uint64:
val = int64(ival.(uint64))
}
c_val := C.long(val)
if ret := C.OBaddfast(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
(*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), 0, &loc.loc); ret != SUCCEED {
return u.Buf.Ctx.NewUBFError()
}
case float32:
if BFLD_FLOAT != fldtyp {
return NewCustomUBFError(BEINVAL, fmt.Sprintf("expected BFLD_FLOAT got: %d",
fldtyp))
}
fval := ival.(float32)
c_val := C.float(fval)
if ret := C.OBaddfast(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
(*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), 0, &loc.loc); ret != SUCCEED {
return u.Buf.Ctx.NewUBFError()
}
case float64:
if BFLD_DOUBLE != fldtyp {
return NewCustomUBFError(BEINVAL, fmt.Sprintf("expected BFLD_DOUBLE got: %d",
fldtyp))
}
dval := ival.(float64)
c_val := C.double(dval)
if ret := C.OBaddfast(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)), C.BFLDID(bfldid),
(*C.char)(unsafe.Pointer(unsafe.Pointer(&c_val))), 0, &loc.loc); ret != SUCCEED {
return u.Buf.Ctx.NewUBFError()
}
case string:
if BFLD_STRING != fldtyp && BFLD_CHAR != fldtyp {
return NewCustomUBFError(BEINVAL, fmt.Sprintf("expected BFLD_STRING or "+
"BFLD_CHAR but got: %d",
fldtyp))