1/* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4 *
5 * std_string.i
6 *
7 * SWIG typemaps for std::string
8 * ----------------------------------------------------------------------------- */
9
10// ------------------------------------------------------------------------
11// std::string is typemapped by value
12// This can prevent exporting methods which return a string
13// in order for the user to modify it.
14// However, I think I'll wait until someone asks for it...
15// ------------------------------------------------------------------------
16
17// %include <exception.i>
18%warnfilter(404) std::string;
19%warnfilter(404) std::wstring;
20
21%{
22#include <string>
23// #include <vector>
24// using std::vector;
25
26using std::string;
27
28
29%}
30
31// %include <std_vector.i>
32
33// %naturalvar std::string;
34// %naturalvar std::wstring;
35
36namespace std {
37    typedef unsigned long size_t;
38    typedef signed long ptrdiff_t;
39
40    template <class charT> class basic_string {
41    public:
42	typedef charT *pointer;
43	typedef charT &reference;
44	typedef const charT &const_reference;
45	typedef size_t size_type;
46	typedef ptrdiff_t difference_type;
47	basic_string();
48	basic_string( charT *str );
49	size_type size();
50	charT operator []( int pos ) const;
51	charT *c_str() const;
52	basic_string<charT> &operator = ( const basic_string &ws );
53	basic_string<charT> &operator = ( const charT *str );
54	basic_string<charT> &append( const basic_string<charT> &other );
55	basic_string<charT> &append( const charT *str );
56	void push_back( charT c );
57	void clear();
58	void reserve( size_type t );
59	void resize( size_type n, charT c = charT() );
60	int compare( const basic_string<charT> &other ) const;
61	int compare( const charT *str ) const;
62	basic_string<charT> &insert( size_type pos,
63				     const basic_string<charT> &str );
64	size_type find( const basic_string<charT> &other, int pos = 0 ) const;
65	size_type find( charT c, int pos = 0 ) const;
66	%extend {
67	    bool operator == ( const basic_string<charT> &other ) const {
68		return self->compare( other ) == 0;
69	    }
70	    bool operator != ( const basic_string<charT> &other ) const {
71		return self->compare( other ) != 0;
72	    }
73	    bool operator < ( const basic_string<charT> &other ) const {
74		return self->compare( other ) == -1;
75	    }
76	    bool operator > ( const basic_string<charT> &other ) const {
77		return self->compare( other ) == 1;
78	    }
79	    bool operator <= ( const basic_string<charT> &other ) const {
80		return self->compare( other ) != 1;
81	    }
82	    bool operator >= ( const basic_string<charT> &other ) const {
83		return self->compare( other ) != -1;
84	    }
85
86	}
87    };
88
89    %template(string) basic_string<char>;
90    %template(wstring) basic_string<wchar_t>;
91
92    %apply char * { string };
93    %apply wchar_t * { wstring };
94
95    typedef basic_string<char> string;
96    typedef basic_string<wchar_t> wstring;
97
98    // automatically convert constant std::strings to cl:strings
99    %typemap(ctype) string "char *";
100    %typemap(in) string "$1.assign($input);";
101    %typemap(out) string "$result = (char *)(&$1)->c_str();";
102    %typemap(lisptype) string "cl:string";
103    %typemap(lout) string "(cl::setq ACL_ffresult $body)";
104
105    %typemap(ctype) const string *"char *";
106    %typemap(in) const string * "$1.assign($input);";
107    %typemap(out) const string * "$result = (char *)($1)->c_str();";
108    %typemap(lisptype) const string * "cl:string";
109    %typemap(lout) const string * "(cl::setq ACL_ffresult $body)";
110
111    %typemap(ctype) wstring "wchar_t *";
112    %typemap(in) wstring "$1.assign($input);";
113    %typemap(out) wstring "$result = (wchar_t *)(&$1)->c_str();";
114    %typemap(lisptype) wstring "cl:string";
115    %typemap(lout) wstring "(cl::setq ACL_ffresult (excl:native-to-string $body
116:external-format #+little-endian :fat-le #-little-endian :fat))";
117
118    %typemap(ctype) const wstring *"char *";
119    %typemap(in) const wstring * "$1.assign($input);";
120    %typemap(out) const wstring * "$result = (char *)($1)->c_str();";
121    %typemap(lisptype) const wstring * "cl:string";
122    %typemap(lout) const wstring * "(cl::setq ACL_ffresult $body)";
123
124    /* Overloading check */
125//     %typemap(in) string {
126//         if (caml_ptr_check($input))
127//             $1.assign((char *)caml_ptr_val($input,0),
128// 			 caml_string_len($input));
129//         else
130//             SWIG_exception(SWIG_TypeError, "string expected");
131//     }
132
133//     %typemap(in) const string & (std::string temp) {
134//         if (caml_ptr_check($input)) {
135//             temp.assign((char *)caml_ptr_val($input,0),
136// 			   caml_string_len($input));
137//             $1 = &temp;
138//         } else {
139//             SWIG_exception(SWIG_TypeError, "string expected");
140//         }
141//     }
142
143//     %typemap(in) string & (std::string temp) {
144//         if (caml_ptr_check($input)) {
145//             temp.assign((char *)caml_ptr_val($input,0),
146// 			   caml_string_len($input));
147//             $1 = &temp;
148//         } else {
149//             SWIG_exception(SWIG_TypeError, "string expected");
150//         }
151//     }
152
153//     %typemap(in) string * (std::string *temp) {
154//         if (caml_ptr_check($input)) {
155//             temp = new std::string((char *)caml_ptr_val($input,0),
156// 				   caml_string_len($input));
157//             $1 = temp;
158//         } else {
159//             SWIG_exception(SWIG_TypeError, "string expected");
160//         }
161//     }
162
163//     %typemap(free) string * (std::string *temp) {
164// 	delete temp;
165//     }
166
167//    %typemap(argout) string & {
168//	caml_list_append(swig_result,caml_val_string_len((*$1).c_str(),
169//							 (*$1).size()));
170//    }
171
172//    %typemap(directorout) string {
173//	$result.assign((char *)caml_ptr_val($input,0),
174//		       caml_string_len($input));
175//    }
176
177//    %typemap(out) string {
178//        $result = caml_val_string_len($1.c_str(),$1.size());
179//    }
180
181//    %typemap(out) string * {
182//	$result = caml_val_string_len((*$1).c_str(),(*$1).size());
183//    }
184}
185
186// #ifdef ENABLE_CHARPTR_ARRAY
187// char **c_charptr_array( const std::vector <string > &str_v );
188
189// %{
190//   SWIGEXT char **c_charptr_array( const std::vector <string > &str_v ) {
191//     char **out = new char *[str_v.size() + 1];
192//     out[str_v.size()] = 0;
193//     for( int i = 0; i < str_v.size(); i++ ) {
194//       out[i] = (char *)str_v[i].c_str();
195//     }
196//     return out;
197//   }
198// %}
199// #endif
200
201// #ifdef ENABLE_STRING_VECTOR
202// %template (StringVector) std::vector<string >;
203
204// %insert(ml) %{
205//   (* Some STL convenience items *)
206
207//   let string_array_to_vector sa =
208//     let nv = _new_StringVector C_void in
209//       array_to_vector nv (fun x -> C_string x) sa ; nv
210
211//   let c_string_array ar =
212//     _c_charptr_array (string_array_to_vector ar)
213// %}
214
215// %insert(mli) %{
216//   val c_string_array: string array -> c_obj
217// %}
218// #endif
219