00001 #ifndef _XI_UTIL_H
00002 #define _XI_UTIL_H
00003
00004 #include <iostream>
00005 #include <string.h>
00006 #include <stdlib.h>
00007 #include <stdio.h>
00008 #include "conv-config.h"
00009
00010 namespace xi {
00011
00012 #if CMK_ISATTY_DECL
00013 #ifdef __cplusplus
00014 extern "C" int isatty (int );
00015 #endif
00016 #endif
00017
00018 #define SZ 1024
00019
00020 class XStr {
00021 private:
00022 char *s;
00023 unsigned int len, blklen;
00024 void initTo(const char *_s);
00025 void operator=(const XStr &str);
00026 public:
00027
00028
00029
00030
00031 void append(const char *_s);
00032 void append(char c);
00033
00034
00035 void print(int indent) {
00036 for (int i=0; i<indent; i++) std::cout << " ";
00037 std::cout << get_string();
00038 }
00040 void line_append(const char c);
00042 void line_append_padding(const char c, int lineWidth=80);
00043
00044 void replace (const char a, const char b);
00045 public:
00046 XStr();
00047 XStr(const char *_s);
00048 XStr(const XStr &_s);
00049 ~XStr() { delete[] s; }
00050 char *get_string(void) const { return s; }
00051 const char *get_string_const(void) const { return s; }
00052
00053
00054 char *charstar(void) const { return get_string(); }
00055
00056 operator char *() {return get_string();}
00057
00058 int operator==(XStr &s2) const {return 0==strcmp(s,s2.s);}
00059 int operator!=(XStr &s2) const {return 0!=strcmp(s,s2.s);}
00060 int operator==(const char *s2) const {return 0==strcmp(s,s2);}
00061 int operator!=(const char *s2) const {return 0!=strcmp(s,s2);}
00062
00063 XStr operator+ (const XStr &s2) const {XStr ret(*this);ret.append(s2.s); return ret;}
00064
00065 XStr& operator << (const char *_s) { append(_s); return *this;}
00066
00067 XStr& operator << (char c) { append(c); return *this;}
00068 XStr& operator << (int i) ;
00069 XStr& operator << (const XStr& x) { append(x.get_string_const()); return *this; }
00070 XStr& operator << (const XStr* x) { append(x->get_string_const()); return *this; }
00071 void spew(const char*b, const char *a1 = 0, const char *a2 = 0,
00072 const char *a3 = 0, const char *a4 = 0, const char *a5 = 0);
00073 };
00074
00075 #define endx "\n"
00076
00077 class Printable {
00078 public:
00079 virtual void print(XStr& str) = 0;
00080
00081 operator XStr () {XStr ret;print(ret);return ret;}
00082
00083 friend XStr & operator << (XStr &str,Printable &p) {p.print(str);return str;}
00084 friend XStr & operator << (XStr &str,Printable *p) {p->print(str);return str;}
00085 };
00086
00087 void templateGuardBegin(bool templateOnly, XStr &str);
00088 void templateGuardEnd(XStr &str);
00089
00090 }
00091
00092 #endif