00001 #ifndef _XI_UTIL_H
00002 #define _XI_UTIL_H
00003
00004 #include <iostream>
00005 #include <sstream>
00006 #include <string>
00007 #include <vector>
00008
00009 #ifndef XI_LIBRARY
00010 #include "conv-config.h"
00011 #endif
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015
00016 #define XLAT_ERROR(...) \
00017 do { \
00018 if (xi::num_errors++ == xi::MAX_NUM_ERRORS) { \
00019 exit(1); \
00020 } else { \
00021 pretty_msg("error", __VA_ARGS__); \
00022 } \
00023 } while (0)
00024
00025 #define XLAT_ERROR_NOCOL(str, line) XLAT_ERROR((str), -1, -1, (line), (line))
00026
00027 #define XLAT_NOTE(str, line) pretty_msg("note", (str), -1, -1, (line), (line))
00028
00029 extern unsigned int lineno;
00030
00031 namespace xi {
00032
00033 extern void pretty_msg(std::string type, std::string msg, int first_col = -1,
00034 int last_col = -1, int first_line = -1, int last_line = -1);
00035
00036 extern const int MAX_NUM_ERRORS;
00037 extern int num_errors;
00038
00039 extern std::vector<std::string> inputBuffer;
00040
00041 #if CMK_ISATTY_DECL
00042 #ifdef __cplusplus
00043 extern "C" int isatty(int);
00044 #endif
00045 #endif
00046
00047 #define SZ 1024
00048
00049 class XStr {
00050 private:
00051 char* s;
00052 unsigned int len, blklen;
00053 void initTo(const char* _s);
00054 void operator=(const XStr& str);
00055 public:
00056
00057
00058
00059
00060 void append(const char* _s);
00061 void append(char c);
00062
00063
00064 void print(int indent) {
00065 for (int i = 0; i < indent; i++) std::cout << " ";
00066 std::cout << get_string();
00067 }
00069 void line_append(const char c);
00072 void line_append_padding(const char c, int lineWidth = 80);
00073
00074 void replace(const char a, const char b);
00075
00076 public:
00077 XStr();
00078 XStr(const char* _s);
00079 XStr(const XStr& _s);
00080 ~XStr() { delete[] s; }
00081 void clear();
00082 char* get_string(void) const { return s; }
00083 const char* get_string_const(void) const { return s; }
00084
00085
00086 char* charstar(void) const { return get_string(); }
00087
00088 operator char*() { return get_string(); }
00089 size_t length() const { return len; }
00090
00091 int operator==(XStr& s2) const { return 0 == strcmp(s, s2.s); }
00092 int operator!=(XStr& s2) const { return 0 != strcmp(s, s2.s); }
00093 int operator==(const char* s2) const { return 0 == strcmp(s, s2); }
00094 int operator!=(const char* s2) const { return 0 != strcmp(s, s2); }
00095
00096 XStr operator+(const XStr& s2) const {
00097 XStr ret(*this);
00098 ret.append(s2.s);
00099 return ret;
00100 }
00101
00102 XStr& operator<<(const char* _s) {
00103 append(_s);
00104 return *this;
00105 }
00106
00107 XStr& operator<<(char c) {
00108 append(c);
00109 return *this;
00110 }
00111 XStr& operator<<(int i);
00112 XStr& operator<<(const XStr& x) {
00113 append(x.get_string_const());
00114 return *this;
00115 }
00116 XStr& operator<<(const XStr* x) {
00117 append(x->get_string_const());
00118 return *this;
00119 }
00120 void spew(const char* b, const char* a1 = 0, const char* a2 = 0, const char* a3 = 0,
00121 const char* a4 = 0, const char* a5 = 0);
00122 };
00123
00124 #define endx "\n"
00125
00126 class Printable {
00127 public:
00128 virtual void print(XStr& str) = 0;
00129
00130 operator XStr() {
00131 XStr ret;
00132 print(ret);
00133 return ret;
00134 }
00135
00136 virtual ~Printable() {}
00137 friend XStr& operator<<(XStr& str, Printable& p) {
00138 p.print(str);
00139 return str;
00140 }
00141 friend XStr& operator<<(XStr& str, Printable* p) {
00142 p->print(str);
00143 return str;
00144 }
00145 };
00146
00147 void templateGuardBegin(bool templateOnly, XStr& str);
00148 void templateGuardEnd(XStr& str);
00149
00150 inline void indentBy(XStr& s, int num) {
00151 for (int i = 0; i < num; i++) s << " ";
00152 }
00153
00154 class TVarList;
00155 XStr generateTemplateSpec(TVarList* tspec, bool printDefault = true);
00156
00157 typedef enum {
00158 forAll = 0,
00159 forIndividual = 1,
00160 forSection = 2,
00161 forPython = 3,
00162 forIndex = -1
00163 } forWhom;
00164
00165 const char* forWhomStr(forWhom w);
00166
00167
00168
00169 void die(const char* why, int line = -1);
00170
00171 char* fortranify(const char* s, const char* suff1 = "", const char* suff2 = "",
00172 const char* suff3 = "");
00173
00174 void templateGuardBegin(bool templateOnly, XStr& str);
00175 void templateGuardEnd(XStr& str);
00176
00177 std::string addLineNumbers(char* str, const char* filename);
00178 extern void sanitizeComments(std::string& code);
00179 extern void sanitizeStrings(std::string& code);
00180 extern void desanitizeCode(std::string& code);
00181
00182 }
00183
00184 namespace Prefix {
00185
00186 extern const char* Proxy;
00187 extern const char* ProxyElement;
00188 extern const char* ProxySection;
00189 extern const char* Message;
00190 extern const char* Index;
00191 extern const char* Python;
00192
00193 }
00194
00195 #endif