nis-util  1.0.D108
lib/rcstring/quote_c.cc
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or (at
00008 // your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License along
00016 // with this program. If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #include <lib/ac/ctype.h>
00020 
00021 #include <lib/rcstring/accumulator.h>
00022 
00023 
00024 rcstring
00025 rcstring::quote_c(void)
00026     const
00027 {
00028     rcstring_accumulator acc;
00029     acc.push_back('"');
00030     const char *cp = c_str();
00031     for (;;)
00032     {
00033         unsigned char c = *cp++;
00034         switch (c)
00035         {
00036         case '\0':
00037             acc.push_back('"');
00038             return acc.mkstr();
00039 
00040         case '\\':
00041             acc.push_back("\\\\");
00042             break;
00043 
00044         case '"':
00045             acc.push_back("\\\"");
00046             break;
00047 
00048         case '\a':
00049             acc.push_back("\\a");
00050             break;
00051 
00052         case '\b':
00053             acc.push_back("\\b");
00054             break;
00055 
00056         case '\f':
00057             acc.push_back("\\f");
00058             break;
00059 
00060         case '\n':
00061             acc.push_back("\\n");
00062             break;
00063 
00064         case '\r':
00065             acc.push_back("\\r");
00066             break;
00067 
00068         case '\t':
00069             acc.push_back("\\t");
00070             break;
00071 
00072         case '\v':
00073             acc.push_back("\\v");
00074             break;
00075 
00076         default:
00077             if (isprint(c))
00078                 acc.push_back(c);
00079             else
00080             {
00081                 acc.push_back('\\');
00082                 acc.push_back((unsigned char)('0' + (c >> 6)));
00083                 acc.push_back((unsigned char)('0' + ((c >> 3) & 7)));
00084                 acc.push_back((unsigned char)('0' + (c & 7)));
00085             }
00086             break;
00087         }
00088     }
00089 }
00090 
00091 
00092 // vim: set ts=8 sw=4 et :