gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
StringUtils.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <regex>
9 #if defined(__CYGWIN__)
10 #include <sys/cygwin.h>
11 #endif
12 #include "StringUtils.h"
13 #include "GmshMessage.h"
14 #include "OS.h"
15 
16 void SwapBytes(char *array, int size, int n)
17 {
18  char *x = new char[size];
19  for(int i = 0; i < n; i++) {
20  char *a = &array[i * size];
21  memcpy(x, a, size);
22  for(int c = 0; c < size; c++) a[size - 1 - c] = x[c];
23  }
24  delete[] x;
25 }
26 
27 std::string ExtractDoubleQuotedString(const char *str, int len)
28 {
29  char *c = strstr((char *)str, "\"");
30  if(!c) return "";
31  std::string ret;
32  for(int i = 1; i < len; i++) {
33  if(c[i] == '"' || c[i] == EOF || c[i] == '\n' || c[i] == '\r') break;
34  ret.push_back(c[i]);
35  }
36  return ret;
37 }
38 
39 std::string SanitizeTeXString(const char *in, int equation)
40 {
41  // if there is a '$' or a '\' in the string, assume the author knows
42  // what he's doing:
43  if(strstr(in, "$") || strstr(in, "\\")) return std::string(in);
44 
45  std::string out;
46 
47  if(equation) out.push_back('$');
48 
49  // otherwise, escape the following special characters:
50  char bad[8] = {'%', '^', '#', '%', '&', '_', '{', '}'};
51  while(*in) {
52  for(std::size_t i = 0; i < sizeof(bad); i++) {
53  if(*in == bad[i]) {
54  out.push_back('\\');
55  break;
56  }
57  }
58  out.push_back(*in++);
59  }
60 
61  if(equation) out.push_back('$');
62  return out;
63 }
64 
65 std::string FixWindowsPath(const std::string &in)
66 {
67 #if defined(__CYGWIN__)
68  char tmp[1024];
69  //cygwin_conv_to_win32_path(in.c_str(), tmp);
70  cygwin_conv_path(CCP_POSIX_TO_WIN_A, in.c_str(), tmp, 1024);
71  return std::string(tmp);
72 #else
73  return in;
74 #endif
75 }
76 
77 std::string FixRelativePath(const std::string &reference, const std::string &in)
78 {
79  if(in.empty()) return "";
80 
81  if(in[0] == '/' || in[0] == '\\' ||
82  (in.size() > 3 && in[1] == ':' && (in[2] == '/' || in[2] == '\\'))) {
83  // do nothing: 'in' is an absolute path
84  return in;
85  }
86  else {
87  // append 'in' to the path of the reference file
88  std::vector<std::string> split = SplitFileName(reference);
89  return split[0] + in;
90  }
91 }
92 
93 std::vector<std::string> SplitFileName(const std::string &fileName)
94 {
95  // JFR DO NOT CHANGE TO std::vector<std::string> s(3), it segfaults while
96  // destructor si called
97  std::vector<std::string> s;
98  s.resize(3);
99  if(fileName.size()) {
100  // returns [path, baseName, extension]
101  int idot = (int)fileName.find_last_of('.');
102  int islash = (int)fileName.find_last_of("/\\");
103  if(idot == (int)std::string::npos) idot = -1;
104  if(islash == (int)std::string::npos) islash = -1;
105  if(idot > 0) s[2] = fileName.substr(idot);
106  if(islash > 0) s[0] = fileName.substr(0, islash + 1);
107  s[1] =
108  fileName.substr(s[0].size(), fileName.size() - s[0].size() - s[2].size());
109  }
110  return s;
111 }
112 
113 std::string GetFileNameWithoutPath(const std::string &fileName)
114 {
115  std::vector<std::string> s = SplitFileName(fileName);
116  return s[1] + s[2];
117 }
118 
119 std::string ConvertFileToString(const std::string &fileName)
120 {
121  FILE *fp = Fopen(fileName.c_str(), "r");
122  if(!fp) return "";
123  std::string out;
124  char str[256];
125  while(!feof(fp) && fgets(str, sizeof(str), fp)) out += str;
126  fclose(fp);
127  return out;
128 }
129 
130 void ReplaceSubStringInPlace(const std::string &olds, const std::string &news,
131  std::string &str)
132 {
133  while(1) {
134  int pos = (int)str.find(olds.c_str());
135  if(pos == (int)std::string::npos) break;
136  str.replace(pos, olds.size(), news.c_str());
137  }
138 }
139 
140 std::string ReplaceSubString(const std::string &olds, const std::string &news,
141  const std::string &str)
142 {
143  std::string copy(str);
144  ReplaceSubStringInPlace(olds, news, copy);
145  return copy;
146 }
147 
148 void ConvertToHTML(std::string &str)
149 {
150  ReplaceSubStringInPlace("<", "&lt;", str);
151  ReplaceSubStringInPlace(">", "&gt;", str);
152  ReplaceSubStringInPlace("\n\n", "<p>", str);
153  ReplaceSubStringInPlace("\n", "<br>", str);
154 }
155 
156 bool SplitOptionName(const std::string &fullName, std::string &category,
157  std::string &name, int &index)
158 {
159  std::string::size_type d = fullName.find_first_of('.');
160  if(d == std::string::npos) {
161  name = fullName;
162  return false;
163  }
164  category = fullName.substr(0, d);
165  std::string::size_type b1 = fullName.find_first_of('[');
166  std::string::size_type b2 = fullName.find_last_of(']');
167  if(b1 != std::string::npos && b2 != std::string::npos) {
168  std::string id = fullName.substr(b1 + 1, b2 - b1 - 1);
169  index = atoi(id.c_str());
170  category = fullName.substr(0, b1);
171  name = fullName.substr(d + 1, b1 - d);
172  }
173  else {
174  index = 0;
175  name = fullName.substr(d + 1);
176  }
177  Msg::Debug("Decoded option name '%s' . '%s' (index %d)", category.c_str(),
178  name.c_str(), index);
179  return true;
180 }
181 
182 std::string RemoveWhiteSpace(const std::string &s)
183 {
184  std::regex r("\\s+");
185  return std::regex_replace(s, r, "");
186 }
187 
188 static std::string getNextTokenInString(const std::string &msg,
189  std::string::size_type &first,
190  char separator)
191 {
192  if(first == std::string::npos) return "";
193  std::string::size_type last = msg.find_first_of(separator, first);
194  std::string next("");
195  if(last == std::string::npos) {
196  next = msg.substr(first);
197  first = last;
198  }
199  else if(first == last) {
200  next = "";
201  first = last + 1;
202  }
203  else {
204  next = msg.substr(first, last - first);
205  first = last + 1;
206  }
207  return next;
208 }
209 
210 std::vector<std::string> SplitString(const std::string &msg, char separator,
211  bool removeWhiteSpace)
212 {
213  std::vector<std::string> out;
214  std::string::size_type first = 0;
215  while(first != std::string::npos) {
216  if(removeWhiteSpace)
217  out.push_back(
218  RemoveWhiteSpace(getNextTokenInString(msg, first, separator)));
219  else
220  out.push_back(getNextTokenInString(msg, first, separator));
221  }
222  return out;
223 }
224 
225 bool IsOnelabName(const std::string &name, std::string &cleanName)
226 {
227  std::string::size_type n = name.find('/');
228  if(name.empty() || n == std::string::npos) return false;
229 
230  if(name.size() > 1 && name[0] == '/')
231  cleanName = name.substr(1);
232  else
233  cleanName = name;
234 
235  return true;
236 }
SplitFileName
std::vector< std::string > SplitFileName(const std::string &fileName)
Definition: StringUtils.cpp:93
SplitOptionName
bool SplitOptionName(const std::string &fullName, std::string &category, std::string &name, int &index)
Definition: StringUtils.cpp:156
GetFileNameWithoutPath
std::string GetFileNameWithoutPath(const std::string &fileName)
Definition: StringUtils.cpp:113
OS.h
Msg::Debug
static void Debug(const char *fmt,...)
Definition: GmshMessage.cpp:752
c
static double c(int i, int j, fullMatrix< double > &CA, const std::vector< SPoint3 > &P, const std::vector< SPoint3 > &Q)
Definition: discreteFrechetDistance.cpp:15
ConvertFileToString
std::string ConvertFileToString(const std::string &fileName)
Definition: StringUtils.cpp:119
FixWindowsPath
std::string FixWindowsPath(const std::string &in)
Definition: StringUtils.cpp:65
SanitizeTeXString
std::string SanitizeTeXString(const char *in, int equation)
Definition: StringUtils.cpp:39
GmshMessage.h
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
ConvertToHTML
void ConvertToHTML(std::string &str)
Definition: StringUtils.cpp:148
SwapBytes
void SwapBytes(char *array, int size, int n)
Definition: StringUtils.cpp:16
getNextTokenInString
static std::string getNextTokenInString(const std::string &msg, std::string::size_type &first, char separator)
Definition: StringUtils.cpp:188
FixRelativePath
std::string FixRelativePath(const std::string &reference, const std::string &in)
Definition: StringUtils.cpp:77
ReplaceSubStringInPlace
void ReplaceSubStringInPlace(const std::string &olds, const std::string &news, std::string &str)
Definition: StringUtils.cpp:130
ReplaceSubString
std::string ReplaceSubString(const std::string &olds, const std::string &news, const std::string &str)
Definition: StringUtils.cpp:140
ExtractDoubleQuotedString
std::string ExtractDoubleQuotedString(const char *str, int len)
Definition: StringUtils.cpp:27
StringUtils.h
b1
const double b1
Definition: GaussQuadratureHex.cpp:14
picojson::copy
void copy(const std::string &s, Iter oi)
Definition: picojson.h:510
SplitString
std::vector< std::string > SplitString(const std::string &msg, char separator, bool removeWhiteSpace)
Definition: StringUtils.cpp:210
picojson::array
value::array array
Definition: picojson.h:194
RemoveWhiteSpace
std::string RemoveWhiteSpace(const std::string &s)
Definition: StringUtils.cpp:182
IsOnelabName
bool IsOnelabName(const std::string &name, std::string &cleanName)
Definition: StringUtils.cpp:225