| |
| /pliant/language/type/text/cstr.pli |
| |
| 1 |
# Copyright Hubert Tonneau hubert.tonneau@pliant.cx | |
| 2 |
# | |
| 3 |
# This program is free software; you can redistribute it and/or | |
| 4 |
# modify it under the terms of the GNU General Public License version 2 | |
| 5 |
# as published by the Free Software Foundation. | |
| 6 |
# | |
| 7 |
# This program is distributed in the hope that it will be useful, | |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 10 |
# GNU General Public License for more details. | |
| 11 |
# | |
| 12 |
# You should have received a copy of the GNU General Public License | |
| 13 |
# version 2 along with this program; if not, write to the Free Software | |
| 14 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 15 |
| |
| 16 |
scope "/pliant/language/" "/pliant/install/" | |
| 17 |
module "/pliant/install/ring1.pli" | |
| 18 |
| |
| 19 |
constant maxlen 2^20 | |
| 20 |
| |
| 21 |
| |
| 22 |
public | |
| 23 |
type CStr | |
| 24 |
field Address characters | |
| 25 |
| |
| 26 |
method sz len -> l | |
| 27 |
arg CStr sz ; arg Int l | |
| 28 |
var Address z := memory_search sz:characters maxlen "[0]":characters 1 | |
| 29 |
l := (cast z Int)-(cast sz:characters Int) | |
| 30 |
| |
| 31 |
function 'cast Str' sz -> s | |
| 32 |
arg CStr sz ; arg Str s | |
| 33 |
implicit | |
| 34 |
s set sz:characters sz:len false | |
| 35 |
((the_function 'cast Str' CStr -> Str) arg 1) maps := 1 | |
| 36 |
| |
| 37 |
export '. len' 'cast Str' | |
| 38 |
| |
| 39 |
| |
| 40 |
type AllocatedCStr | |
| 41 |
field Address characters | |
| 42 |
| |
| 43 |
method sz len -> l | |
| 44 |
arg AllocatedCStr sz ; arg Int l | |
| 45 |
var Address z := memory_search sz:characters maxlen "[0]":characters 1 | |
| 46 |
l := (cast z Int)-(cast sz:characters Int) | |
| 47 |
| |
| 48 |
function build sz | |
| 49 |
arg_w AllocatedCStr sz | |
| 50 |
sz characters := null | |
| 51 |
| |
| 52 |
function destroy sz | |
| 53 |
arg_w AllocatedCStr sz | |
| 54 |
memory_free sz:characters | |
| 55 |
| |
| 56 |
function copy src dest | |
| 57 |
arg AllocatedCStr src ; arg_w AllocatedCStr dest | |
| 58 |
memory_free dest:characters | |
| 59 |
var Int l := src len | |
| 60 |
dest characters := memory_allocate l+1 addressof:dest | |
| 61 |
memory_copy src:characters dest:characters l+1 | |
| 62 |
| |
| 63 |
function 'cast AllocatedCStr' s -> sz | |
| 64 |
arg Str s ; arg AllocatedCStr sz | |
| 65 |
implicit | |
| 66 |
memory_free sz:characters | |
| 67 |
var Int l := s len | |
| 68 |
sz characters := memory_allocate l+1 addressof:sz | |
| 69 |
memory_copy s:characters sz:characters l | |
| 70 |
memory_clear (sz:characters translate Byte l) 1 | |
| 71 |
| |
| 72 |
function 'cast CStr' s1 -> s2 | |
| 73 |
arg AllocatedCStr s1 ; arg CStr s2 | |
| 74 |
implicit | |
| 75 |
s2 characters := s1 characters | |
| 76 |
# now we specify that the CStr string is valid only as long as the | |
| 77 |
# AllocatedCStr is not changed, since it maps it's content | |
| 78 |
((the_function 'cast CStr' AllocatedCStr -> CStr) arg 1) maps := 1 | |
| 79 |
| |
| 80 |
export 'cast AllocatedCStr' 'cast CStr' | |
| |