]> gitweb.factorcode.org Git - factor.git/blob - core/alien/strings/strings.factor
put a method on M\ tuple string>alien that calls underlying>>
[factor.git] / core / alien / strings / strings.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays sequences kernel kernel.private accessors math
4 alien.accessors byte-arrays io io.encodings io.encodings.utf8
5 io.encodings.utf16n io.streams.byte-array io.streams.memory system
6 system.private alien strings combinators namespaces init ;
7 IN: alien.strings
8
9 GENERIC# alien>string 1 ( c-ptr encoding -- string/f )
10
11 M: c-ptr alien>string
12     [ <memory-stream> ] [ <decoder> ] bi*
13     "\0" swap stream-read-until drop ;
14
15 M: object alien>string
16     [ underlying>> ] dip alien>string ;
17
18 M: f alien>string
19     drop ;
20
21 ERROR: invalid-c-string string ;
22
23 : check-string ( string -- )
24     0 over memq? [ invalid-c-string ] [ drop ] if ;
25
26 GENERIC# string>alien 1 ( string encoding -- byte-array )
27
28 M: c-ptr string>alien drop ;
29
30 M: string string>alien
31     over check-string
32     <byte-writer>
33     [ stream-write ]
34     [ 0 swap stream-write1 ]
35     [ stream>> >byte-array ]
36     tri ;
37
38 M: tuple string>alien drop underlying>> ;
39
40 HOOK: alien>native-string os ( alien -- string )
41
42 M: windows alien>native-string utf16n alien>string ;
43
44 M: unix alien>native-string utf8 alien>string ;
45
46 HOOK: native-string>alien os ( string -- alien )
47
48 M: windows native-string>alien utf16n string>alien ;
49
50 M: unix native-string>alien utf8 string>alien ;
51
52 : dll-path ( dll -- string )
53     path>> alien>native-string ;
54
55 HOOK: string>symbol* os ( str/seq -- alien )
56
57 M: winnt string>symbol* utf8 string>alien ;
58
59 M: wince string>symbol* utf16n string>alien ;
60
61 M: unix string>symbol* utf8 string>alien ;
62
63 GENERIC: string>symbol ( str -- alien )
64
65 M: string string>symbol string>symbol* ;
66
67 M: sequence string>symbol [ string>symbol* ] map ;
68
69 [
70     8 getenv utf8 alien>string string>cpu \ cpu set-global
71     9 getenv utf8 alien>string string>os \ os set-global
72 ] "alien.strings" add-init-hook
73