Documentation

Generated on Thu Aug 31 00:02:30 2006

 

Utilities.cpp

Go to the documentation of this file.
00001 
00002 // $Id: Utilities.cpp,v 1.49 2005/04/06 18:06:28 klas Exp $
00003 //
00004 // Usage: Various functions that doesn't belong anywhere else.
00005 //
00006 //  A part of babyftpd, licensed under the GNU GPL, see the file
00007 //    LICENSE for complete information.
00009 
00010 #define UTILITIES_CPP
00011 #include "Utilities.h"
00012 
00013 class Utilities util;
00014 
00015 // function to remove line break at the end of a string
00016 void Utilities::strip_line_breaks(string &victim)
00017 {
00018   size_t last = victim.find_last_of('\n');
00019   if(last >= 0 && last < victim.size())
00020     victim = victim.erase(last);
00021 }
00022 
00023 // function to clean a string from:
00024 //      leading spaces
00025 //      trailing spaces
00026 //      spaces between words
00027 // returns number of spaces left in the string
00028 int Utilities::clean_string(string &victim)
00029 {
00030   int spaces = 0;
00031   
00032   // removing leading spaces...
00033   while(isspace(victim[0]))
00034     victim.erase(0, 1);
00035 
00036   // removing trailing spaces...
00037   while(isspace(victim[victim.size() - 1]))
00038     victim.erase(victim.size() - 1, 1);
00039   
00040   for(string::iterator victim_it = victim.begin(); victim_it != victim.end();
00041       ++victim_it)
00042   {
00043     if(isspace(*victim_it) || !isascii(*victim_it))
00044     {
00045       if(*victim_it == ' ')
00046         spaces++;
00047       if((isspace(*(victim_it - 1))) || !isascii(*victim_it))
00048       {
00049         if(*victim_it == ' ')
00050           spaces--;
00051         victim_it = victim.erase(victim_it);
00052         --victim_it;
00053       }
00054     }
00055   }
00056   return(spaces);
00057 }
00058 
00059 // function to convert an int to a string
00060 string Utilities::itos(long long int num)
00061 {
00062   string message;
00063   char buf[12] = {0};
00064 
00065   snprintf(buf, 12, "%lld", num);
00066   message = buf;
00067 
00068   return(message);
00069 }
00070 
00071 // function to parse a numeric permission (0755) to letters (-rwxr-xr-x)
00072 string Utilities::parse_perms_ls(mode_t perm)
00073 {
00074   string attr;
00075 
00076   attr = "----------";
00077 
00078   if(S_ISDIR(perm))
00079     attr[0] = 'd';
00080   else if(S_ISLNK(perm))
00081     attr[0] = 'l';
00082   else if(S_ISCHR(perm))
00083     attr[0] = 'c';
00084   else if(S_ISBLK(perm))
00085     attr[0] = 'b';
00086   else if(S_ISFIFO(perm))
00087     attr[0] = 'p';
00088   else if(S_ISSOCK(perm))
00089     attr[0] = 's';
00090 
00091   if(is_set(perm, S_IRUSR)) attr[1] = 'r';
00092   if(is_set(perm, S_IWUSR)) attr[2] = 'w';
00093   if(is_set(perm, S_IXUSR)) {
00094     if(is_set(perm, S_ISUID)) attr[3] = 's';
00095     else attr[3] = 'x';
00096   } else if(is_set(perm, S_ISUID)) attr[3] = 'S';
00097 
00098   if(is_set(perm, S_IRGRP)) attr[4] = 'r';
00099   if(is_set(perm, S_IWGRP)) attr[5] = 'w';
00100   if(is_set(perm, S_IXGRP)) {
00101     if(is_set(perm, S_ISGID)) attr[6] = 's';
00102     else attr[6] = 'x';
00103   } else if(is_set(perm, S_ISGID)) attr[6] = 'S';
00104 
00105   if(is_set(perm, S_IROTH)) attr[7] = 'r';
00106   if(is_set(perm, S_IWOTH)) attr[8] = 'w';
00107   if(is_set(perm, S_IXOTH)) {
00108     if(is_set(perm, S_ISVTX)) attr[9] = 't';
00109     else attr[9] = 'x';
00110   } else if(is_set(perm, S_ISVTX)) attr[9] = 'T';
00111 
00112   return(attr);
00113 }
00114 
00115 string Utilities::parse_perms_num(mode_t perm)
00116 {
00117   string attr;
00118 
00119   attr = "0000";
00120 
00121   if(is_set(perm, S_ISUID))
00122     attr[0] += 4;
00123   if(is_set(perm, S_ISGID))
00124     attr[0] += 2;
00125   if(is_set(perm, S_ISVTX))
00126     attr[0]++;
00127   if(is_set(perm, S_IRUSR))
00128     attr[1] += 4;
00129   if(is_set(perm, S_IWUSR))
00130     attr[1] += 2;
00131   if(is_set(perm, S_IXUSR))
00132     attr[1]++;
00133 
00134   if(is_set(perm, S_IRGRP))
00135     attr[2] += 4;
00136   if(is_set(perm, S_IWGRP))
00137     attr[2] += 2;
00138   if(is_set(perm, S_IXGRP))
00139     attr[2]++;
00140 
00141   if(is_set(perm, S_IROTH))
00142     attr[3] += 4;
00143   if(is_set(perm, S_IWOTH))
00144     attr[3] += 2;
00145   if(is_set(perm, S_IXOTH))
00146     attr[3]++;
00147 
00148   return(attr);
00149 }
00150 
00151 
00152 int Utilities::time_string(string &victim, string format,
00153     size_t len, time_t timep)
00154 {
00155   char *timestr = new char[len + 1];
00156   struct tm now;
00157   memset(timestr, 0, len + 1);
00158 
00159   if(timep == 0)
00160     timep = time(NULL);
00161   localtime_r(&timep, &now);
00162   int ret = strftime(timestr, len, format.c_str(), &now);
00163   victim = timestr;
00164 
00165   delete[] timestr;
00166   return(ret);
00167 }