00001
00002
00003
00004 #include "common.h"
00005 #include <string>
00006 #include <time.h>
00007 #include <stdlib.h>
00008 #include "ui.h"
00009 #include "logic.h"
00010
00011 using namespace std;
00012
00013 volatile bool user_interrupt = false;
00014
00015 const char* fityk_version_line = "# Fityk script. Fityk version: " VERSION;
00016
00017 vector<int> range_vector(int l, int u)
00018 {
00019 vector<int> v(u - l);
00020 for (int i = l; i < u; i++)
00021 v[i - l] = i;
00022 return v;
00023 }
00024
00025 string time_now()
00026 {
00027 time_t const t = time(0);
00028 return ctime (&t);
00029 }
00030
00031 bool is_double(const string& s) {
00032 char const *c = s.c_str();
00033 char *endptr;
00034 strtod(c, &endptr);
00035 if (c == endptr)
00036 return false;
00037 while (isspace(*endptr))
00038 endptr++;
00039 return (*endptr == 0);
00040 }
00041
00042 bool is_int(const string& s) {
00043 const char *c = s.c_str();
00044 char *endptr;
00045 strtol(c, &endptr, 10);
00046 if (c == endptr)
00047 return false;
00048 while (isspace(*endptr))
00049 endptr++;
00050 return (*endptr == 0);
00051 }
00052
00053 void replace_all(string &s, const string &old, const string &new_)
00054 {
00055 string::size_type pos = 0;
00056 while ((pos = s.find(old, pos)) != string::npos) {
00057 s.replace(pos, old.size(), new_);
00058 pos += new_.size();
00059 }
00060 }
00061
00062
00063
00064 void replace_words(string &t, const string &old_word, const string &new_word)
00065 {
00066 string::size_type pos = 0;
00067 while ((pos=t.find(old_word, pos)) != string::npos) {
00068 int k = old_word.size();
00069 if ((pos == 0
00070 || !(isalnum(t[pos-1]) || t[pos-1]=='_' || t[pos-1]=='$'))
00071 && (pos+k==t.size() || !(isalnum(t[pos+k]) || t[pos+k]=='_'))) {
00072 t.replace(pos, k, new_word);
00073 pos += new_word.size();
00074 }
00075 else
00076 pos++;
00077 }
00078 }
00079
00080
00081 string::size_type
00082 find_matching_bracket(const string& formula, string::size_type left_pos)
00083 {
00084 if (left_pos == string::npos)
00085 return string::npos;
00086 assert(left_pos < formula.size());
00087 char opening = formula[left_pos],
00088 closing = 0;
00089 if (opening == '(')
00090 closing = ')';
00091 else if (opening == '[')
00092 closing = ']';
00093 else if (opening == '{')
00094 closing = '}';
00095 else
00096 assert(0);
00097 int level = 1;
00098 for (size_t p = left_pos+1; p < formula.size() && level > 0; ++p) {
00099 if (formula[p] == closing) {
00100 if (level == 1)
00101 return p;
00102 --level;
00103 }
00104 else if (formula[p] == opening)
00105 ++level;
00106 }
00107 throw ExecuteError("Matching bracket `" + S(closing) + "' not found.");
00108 }
00109
00110
00111 bool match_glob(const char* name, const char* pattern)
00112 {
00113 while (*pattern != '\0') {
00114 if (*pattern == '*') {
00115 if (pattern[1] == '\0')
00116 return true;
00117 const char *here = name;
00118 while (*name != '\0')
00119 ++name;
00120 while (name != here) {
00121 if (match_glob(name, pattern))
00122 return true;
00123 --name;
00124 }
00125 }
00126 else {
00127 if (*name != *pattern)
00128 return false;
00129 ++name;
00130 }
00131 ++pattern;
00132 }
00133
00134 return *name == '\0';
00135 }
00136