30 ret += (msg[1] -
'0') * 100;
31 ret += (msg[2] -
'0') * 10 ;
32 ret += (msg[3] -
'0') ;
45 if (fn.empty())
return false;
80 sprintf(buf,
"%d", i);
88 if (sscanf(s.c_str(),
"%d", &i) == 1)
91 throw MCS_ERROR(MSG_CONVERSION_STRING_INT);
98 if (sscanf(s.c_str(),
"%d", &i) == 1)
108 sprintf(buf,
"%f", f);
116 return string(
"true");
118 return string(
"false");
128 for (i=0; i<vec.size(); i++)
135 string mcs::subst(
string s,
string what,
string with,
int op)
137 string s2 =
"(" + what + string(
")");
144 if (op & MCS_SUBST_LEADING)
148 pcrecpp::RE re(templ,
149 pcrecpp::RE_Options().set_dollar_endonly(
true));
152 pcrecpp::RE re(
"\\\\");
153 re.GlobalReplace(
"\\\\\\\\", &with);
155 re.GlobalReplace(with, &s);
174 return subst(s,
"\t",
"");
197 pcrecpp::RE pre(
"^\\Q" + sep +
"\\E+");
198 pre.GlobalReplace(
"", &s);
200 pcrecpp::StringPiece input(s);
201 pcrecpp::RE re(
"([^\\Q" + sep +
"\\E]+)\\Q" + sep +
"\\E*");
204 while (re.Consume(&input, &s))
213 const char* buf = (
const char*) pvoid;
215 unsigned int i, j, count;
224 sprintf(lbuf,
"%06d ", i);
227 for (j=0; j<count; j++) {
228 sprintf(lbuf,
"%02X", buf[i+j]);
234 for (j=0; j<count; j++) {
235 if (isprint(buf[i+j]))
236 sprintf(lbuf,
"%c", buf[i+j]);
252 mode_t mask = umask (0);
261 if (perm == MCS_MKDIR_UMASK)
262 perm = (S_IRWXU | S_IRWXG | S_IRWXO) & ~
read_umask();
264 if (mkdir(path.csz, perm))
266 throw MCS_ERROR(MSG_CANT_MKDIR, path.csz);
278 s =
"rm -rf " + path;
282 throw MCS_ERROR(MSG_CANT_RMDIR, path.csz);
297 if (fn.empty())
return;
299 dp = opendir (fn.c_str());
301 while ((ep = readdir (dp))) {
303 if ( r !=
"." && r !=
".." )
323 static char* appendFileNameToPath(
char* OLDNAME,
char* NEWNAME)
328 p = strrchr(OLDNAME,
'/');
336 buf = (
char*) malloc(strlen(NEWNAME) + strlen(p) + 2);
338 strcpy(buf, NEWNAME);
340 if (buf[strlen(buf) - 1] !=
'/') {
341 buf[strlen(buf) + 1] = 0;
342 buf[strlen(buf)] =
'/';
345 strcpy(buf + strlen(buf), p);
352 #define BUFSIZE 16384 359 if ((outf = fopen(NEWNAME,
"wb")) == NULL) {
360 if (errno == EISDIR) {
361 char* b = appendFileNameToPath(OLDNAME, NEWNAME);
362 int ret =
copy(OLDNAME, b);
367 fprintf(stderr,
"Error opening file %s: %s\n", NEWNAME,
373 if ((inf = fopen(OLDNAME,
"rb")) == NULL) {
375 fprintf(stderr,
"Error opening file %s: %s\n", OLDNAME,
381 while (! feof(inf)) {
382 nread = fread(buf,
sizeof(
char), BUFSIZE, inf);
383 fwrite(buf,
sizeof(
char), nread, outf);
400 ret = rename(OLDNAME, NEWNAME);
407 buf = appendFileNameToPath(OLDNAME, NEWNAME);
408 ret =
move(OLDNAME, buf);
414 if (NEWNAME[strlen(NEWNAME) - 1] !=
'/') {
415 buf = appendFileNameToPath(OLDNAME, NEWNAME);
416 ret =
move(OLDNAME, buf);
423 ret =
copy(OLDNAME, NEWNAME);
425 if (unlink(OLDNAME) == -1) {
426 fprintf(stderr,
"Error deleting file %s: %s\n", OLDNAME,
436 fprintf(stderr,
"Error renaming %s ==> %s: %s\n", OLDNAME, NEWNAME,
450 const char* mcs::B64_Codec::cb64=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
451 const char* mcs::B64_Codec::cd64=
"|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
472 if ((! bufAllocated) ||
473 (bufsize < Length)) {
475 bufAllocated = (
char*) realloc(bufAllocated, bufsize);
476 memset(bufAllocated, 0, bufsize);
478 this->buf_out = bufAllocated;
496 return ((
bool) (pin >= EOIB));
502 unsigned char ret = precData[0];
503 precData[0] = precData[1];
504 precData[1] = precData[2];
505 precData[2] = precData[3];
516 char* mcs::B64_Codec::buffer()
519 unsigned int mcs::B64_Codec::bufUsed()
520 {
return pout - buf_out; }
524 unsigned int linesize)
526 unsigned int rescue = 0;
527 unsigned char in[3], out[4];
530 int origLength = Length;
537 rescue = (precDataCount + Length) % 3;
543 EOIB = buf_in + Length;
549 buf_out = par_buf_out;
552 requireSpace(origLength * 2);
553 buf_out = bufAllocated;
559 if (linesize < MCS_B64_MIN_LINE)
560 linesize = MCS_B64_MIN_LINE;
561 this->linesize = linesize;
569 in[i] = (
unsigned char) readData();
576 encodeblock(in, out, len);
582 if (blocksout >= (linesize/4)) {
591 for (
unsigned int i=0; i<rescue; i++)
592 precData[i] = readData();
594 precDataCount = rescue;
604 return pout - buf_out;
611 unsigned char in[4], out[3], v, d;
615 EOIB = buf_in + Length;
621 buf_out = par_buf_out;
624 requireSpace(Length);
625 buf_out = bufAllocated;
636 v = (
unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
641 if (precDataCount == 0)
649 decodeblock(in, out);
650 for(i=0; i<len-1; i++)
661 decodeblock(in, out);
662 for(i=0; i<len-1; i++)
669 return pout - buf_out;
674 out[0] = cb64[ in[0] >> 2 ];
675 out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
676 out[2] = (
unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] :
'=');
677 out[3] = (
unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] :
'=');
682 out[ 0 ] = (
unsigned char ) (in[0] << 2 | in[1] >> 4);
683 out[ 1 ] = (
unsigned char ) (in[1] << 4 | in[2] >> 2);
684 out[ 2 ] = (
unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
691 int mcs::Select(
int fd,
unsigned int sec_timeout,
unsigned int usec_timeout,
int op)
694 struct timeval timeout;
699 timeout.tv_sec = sec_timeout;
700 timeout.tv_usec = usec_timeout;
702 if (op == MCS_SELECT_READ)
703 ret = select(FD_SETSIZE, &
set, NULL, NULL, &timeout);
705 ret = select(FD_SETSIZE, NULL, &
set, NULL, &timeout);
713 int mcs::Select(
int fd[],
int nfd,
unsigned int sec_timeout,
unsigned int usec_timeout,
int op)
716 struct timeval timeout;
720 for (i=0; i<nfd; i++) {
721 FD_SET (fd[i], &
set);
725 timeout.tv_sec = sec_timeout;
726 timeout.tv_usec = usec_timeout;
728 if (op == MCS_SELECT_READ)
729 ret = select(FD_SETSIZE, &
set, NULL, NULL, &timeout);
731 ret = select(FD_SETSIZE, NULL, &
set, NULL, &timeout);
733 for (i=0; i<nfd; i++)
734 fd[i] = FD_ISSET(i, &
set);
void requireSpace(unsigned int Length)
Allocate "Length" bytes in internal buffer.
mode_t read_umask()
Read the file creation mask of the current process.
string btos(bool b)
Convert a boolean to a string containing "true" or "false".
string itos(int i)
Convert an integer to a string.
unsigned char readData()
Read one byte from input buffer.
bool File_Dir_Exist(string fn, unsigned int &size)
Check if a file or directory exists.
#define MCS_SUBST_LEADING
To be used with subst(), substitute only if "what" is at the beginning. See subst().
string Pwd()
Return the current working dir.
#define MCS_SUBST_TRAILING
To be used with subst(), substitute only if "what" is at the end. See subst().
A dynamic array of Data objects.
int copy(char *OLDNAME, char *NEWNAME)
Copy a file from OLDNAME to NEWNAME.
unsigned int decode(char *buf_in, int Length, char *par_buf_out=NULL)
Decode a block of data.
int move(char *OLDNAME, char *NEWNAME)
Move a file from OLDNAME to NEWNAME.
void freeBuffer()
Free internal buffer.
void ls2Record(string fn, Record &v)
Fill a vector with a list of file.
unsigned int encode(char *buf_in, int Length, char *par_buf_out=NULL, unsigned int linesize=72)
Encode a block of data.
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
bool eob()
Tell if we are at the end of input buffer.
string vtos(vector< string > vec)
Join a vector of strings in a single string using newlines.
string remTrailing(string &s, const char *p)
Remove any trailing character "p".
int stoi(string s)
Convert a string to an integer.
void writeData(unsigned char d)
Write one byte to output buffer.
void encodeblock(unsigned char in[3], unsigned char out[4], int len)
Encode a block of 3 binary bytes into 4 'Base64' bytes.
vector< string > split(string s, string sep=" ")
Split a string into tokens.
string remTabs(string s)
Remove any tab.
Main include file for all MCS based applications.
void decodeblock(unsigned char in[4], unsigned char out[3])
Decode a block of 4'Base64' bytes into 3 binary bytes.
void addField(Data *d)
Wrapper around Dynamic_Array.push.
string chomp(string s)
Remove any trailing newlines.
int extractCode(const char *msg)
Extract the numeric code from a server reply.
A general purpose data type.
string subst(string s, string what, string with, int op=0)
Perform substitutions on a string.
bool rmDir(string path, enum ThrowExceptions throwexc=THROW)
Removes a directory.
ThrowExceptions
Values to be used with throwexc parameters.
string remLeading(string &s, const char *p)
Remove any leading character "p".
string trim(string s)
Remove any leading or trailing blanks.
string hexDump(const void *buf, unsigned int size)
Return a string with an hex dump of the buffer pointed by "buf", with a length of "size"...
string dtos(double f)
Convert an floating point number to a string.
bool mkDir(string path, mode_t perm=0, enum ThrowExceptions throwexc=THROW)
Create a directory.
#define MCS_SUBST_QUOTE_WITH
To be used with subst(), doubles each backslash in "with". See subst().
Namespace for MCS library.