Ignore:
Timestamp:
Apr 13, 2011, 1:28:08 PM (13 years ago)
Author:
rybkin
Message:

See C.L. 448

File:
1 edited

Legend:

Unmodified
Added
Removed
  • CMT/HEAD/source/cmt_system.cxx

    r561 r565  
    21882188  if (text.size () == 0) return result;
    21892189
    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 '
    21912191  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    }
    21922202  char* b (buffer);
    21932203
     
    22442254  result = buffer;
    22452255  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//----------------------------------------------------------
     2267int 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
     2306cmt_string CmtSystem::mangle (const cmt_string& text)
     2307{
     2308  cmt_string result;
     2309  mangle (text, result);
    22462310  return result;
    22472311}
Note: See TracChangeset for help on using the changeset viewer.