Changeset 565 for CMT/HEAD/source/cmt_system.cxx
- Timestamp:
- Apr 13, 2011, 1:28:08 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
CMT/HEAD/source/cmt_system.cxx
r561 r565 2188 2188 if (text.size () == 0) return result; 2189 2189 2190 int allocated = 3 * text.size (); // if EACH character of text quoted with " or '2190 size_t allocated = 3 * text.size (); // if EACH character of text quoted with " or ' 2191 2191 char* const buffer = (char*) malloc (allocated + 1); 2192 if (0 == buffer) 2193 { 2194 char num[32]; sprintf (num, "%u", allocated + 1); 2195 cmt_string msg ("Cannot allocate "); 2196 msg += num; 2197 msg += " bytes"; 2198 cmt_string e = errno ? ": " + cmt_string (strerror (errno)) : ""; 2199 CmtMessage::error (msg + e); 2200 exit (EXIT_FAILURE); 2201 } 2192 2202 char* b (buffer); 2193 2203 … … 2244 2254 result = buffer; 2245 2255 free (buffer); 2256 return result; 2257 } 2258 2259 //---------------------------------------------------------- 2260 // Mangle text so that it consists solely of 2261 // letters, digits, and underscores 2262 // the first character being not digit 2263 // (any other characters are replaced with character '_'). 2264 // The out can be used as shell variable name. 2265 // Return the number of characters replaced with character '_'. 2266 //---------------------------------------------------------- 2267 int CmtSystem::mangle (const cmt_string& text, cmt_string& out) 2268 { 2269 int result (0); 2270 size_t allocated = text.size (); 2271 //allocated = 3000000000; 2272 char* const buffer = (char*) malloc (allocated + 1); 2273 if (0 == buffer) 2274 { 2275 char num[32]; sprintf (num, "%u", allocated + 1); 2276 cmt_string msg ("Cannot allocate "); 2277 msg += num; 2278 msg += " bytes"; 2279 cmt_string e = errno ? ": " + cmt_string (strerror (errno)) : ""; 2280 CmtMessage::error (msg + e); 2281 exit (EXIT_FAILURE); 2282 } 2283 char* b (buffer); 2284 const char* t = text.c_str (); 2285 while (*t) 2286 { 2287 if (isalnum (*t) || *t == '_') 2288 *b++ = *t++; 2289 else 2290 { 2291 *b++ = '_'; t++; 2292 result++; 2293 } 2294 } 2295 *b = '\0'; 2296 if (0 != allocated && isdigit (*buffer)) 2297 { 2298 *buffer = '_'; 2299 result++; 2300 } 2301 out = buffer; 2302 free (buffer); 2303 return result; 2304 } 2305 2306 cmt_string CmtSystem::mangle (const cmt_string& text) 2307 { 2308 cmt_string result; 2309 mangle (text, result); 2246 2310 return result; 2247 2311 }
Note: See TracChangeset
for help on using the changeset viewer.