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 __self, __attr _data) 30 { 31 /* _data interpreted as buffer.__data__ */ 32 __fragment *data = _data.seqvalue; 33 unsigned int size = 0, i, j, n; 34 char *s; 35 __attr o; 36 37 /* Calculate the size of the string. */ 38 for (i = 0; i < data->size; i++) 39 size += __TOINT(__load_via_object(__VALUE(data->attrs[i]), __size__)); 40 41 /* Reserve space for a new string. */ 42 s = (char *) __ALLOCATE(size + 1, sizeof(char)); 43 44 /* Build a single string from the buffer contents. */ 45 for (i = 0, j = 0; i < data->size; i++) 46 { 47 o = __load_via_object(__VALUE(data->attrs[i]), __data__); 48 n = __TOINT(__load_via_object(__VALUE(data->attrs[i]), __size__)); 49 memcpy(s + j, o.strvalue, n); /* does not null terminate but final byte should be zero */ 50 j += n; 51 } 52 53 /* Return a new string. */ 54 return __new_str(s, size); 55 } 56 57 /* Module initialisation. */ 58 59 void __main_native_buffer() 60 { 61 }