1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // name: util_string.cpp
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
32 //-----------------------------------------------------------------------------
33 #include "util_string.h"
39 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 string
itoa( t_CKINT val
)
46 sprintf( buffer
, "%li", val
);
47 return string(buffer
);
52 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
56 string
ftoa( t_CKFLOAT val
, t_CKUINT precision
)
60 if( precision
> 32 ) precision
= 32;
61 sprintf( str
, "%%.%lif", precision
);
62 sprintf( buffer
, str
, val
);
63 return string(buffer
);
69 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 string
tolower( const string
& str
)
76 t_CKUINT len
= s
.length();
79 for( t_CKUINT i
= 0; i
< len
; i
++ )
80 if( s
[i
] >= 'A' && s
[i
] <= 'Z' )
89 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
93 string
toupper( const string
& str
)
96 t_CKUINT len
= s
.length();
99 for( t_CKUINT i
= 0; i
< len
; i
++ )
100 if( s
[i
] >= 'a' && s
[i
] <= 'z' )
109 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
113 string
trim( const string
& val
)
117 t_CKINT end
= val
.length() - 1;
120 for( start
= 0; start
< end
; start
++ )
123 if( val
[start
] != ' ' && val
[start
] != '\t' )
127 // if start > end, then all white space
128 if( start
> end
) return "";
131 for( ; end
>= start
; end
-- )
134 if( val
[end
] != ' ' && val
[end
] != '\t' )
139 assert( end
>= start
);
142 return val
.substr( start
, end
- start
+ 1 );
147 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
151 string
ltrim( const string
& val
)
155 t_CKINT end
= val
.length() - 1;
158 for( start
= 0; start
< end
; start
++ )
161 if( val
[start
] != ' ' && val
[start
] != '\t' )
165 // if start > end, then all white space
166 if( start
> end
) return "";
169 return val
.substr( start
, end
- start
+ 1 );
175 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
179 string
rtrim( const string
& val
)
183 t_CKINT end
= val
.length() - 1;
186 for( ; end
>= start
; end
-- )
189 if( val
[end
] != ' ' && val
[end
] != '\t' )
193 // if end < start, then all white space
194 if( end
< start
) return "";
197 return val
.substr( start
, end
- start
+ 1 );
203 //-----------------------------------------------------------------------------
204 // name: extract_args()
205 // desc: extract argument from format filename:arg1:arg2:etc
206 //-----------------------------------------------------------------------------
207 t_CKBOOL
extract_args( const string
& token
,
208 string
& filename
, vector
<string
> & args
)
216 t_CKINT prev_pos
= 0;
221 string s
= trim( token
);
224 t_CKBOOL scan
= FALSE
;
228 for( i
= 0; i
< s
.length(); i
++ )
238 buf
= new char[s
.length()+1];
239 mask
= new char[s
.length()];
243 for( i
= 0; i
< s
.length(); i
++ )
249 if( s
[i
] == '\\' && (i
+1) < s
.length() && s
[i
+1] == ':' )
267 for( i
= 0; i
< s
.length(); i
++ )
270 if( s
[i
] == ':' && ( !mask
|| !mask
[i
] ) )
281 filename
= s
.substr( prev_pos
, i
- prev_pos
);
283 args
.push_back( s
.substr( prev_pos
, i
- prev_pos
) );
290 // get the remainder, if any
291 if( prev_pos
< s
.length() )
295 filename
= s
.substr( prev_pos
, s
.length() - prev_pos
);
297 args
.push_back( s
.substr( prev_pos
, s
.length() - prev_pos
) );
302 SAFE_DELETE_ARRAY( buf
);
303 SAFE_DELETE_ARRAY( mask
);