1 /* Native functions for buffer operations. 2 3 Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> 4 5 This program is free software; you can redistribute it and/or modify it under 6 the terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3 of the License, or (at your option) any later 8 version. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 details. 14 15 You should have received a copy of the GNU General Public License along with 16 this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include <string.h> /* strcmp, memcpy */ 20 #include "native/common.h" 21 #include "types.h" 22 #include "exceptions.h" 23 #include "ops.h" 24 #include "progconsts.h" 25 #include "progops.h" 26 #include "progtypes.h" 27 #include "main.h" 28 29 __attr __fn_native_buffer_buffer_str(__attr __args[]) 30 { 31 __attr * const _data = &__args[1]; 32 /* _data interpreted as buffer */ 33 __fragment *data = _data->seqvalue; 34 unsigned int size = 0, i, j, n; 35 char *s; 36 __attr o; 37 38 /* Calculate the size of the string. */ 39 for (i = 0; i < data->size; i++) 40 size += strlen(__load_via_object(data->attrs[i].value, __pos___data__).strvalue); 41 42 /* Reserve space for a new string. */ 43 s = (char *) __ALLOCATE(size + 1, sizeof(char)); 44 45 /* Build a single string from the buffer contents. */ 46 for (i = 0, j = 0; i < data->size; i++) 47 { 48 o = __load_via_object(data->attrs[i].value, __pos___data__); 49 n = strlen(o.strvalue); 50 memcpy(s + j, o.strvalue, n); /* does not null terminate but final byte should be zero */ 51 j += n; 52 } 53 54 /* Return a new string. */ 55 return __new_str(s); 56 } 57 58 /* Module initialisation. */ 59 60 void __main_native_buffer() 61 { 62 }