Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RocfluidMP/utilities/rocflo/makeflo/util.cpp
Go to the documentation of this file.
1 /* *******************************************************************
2  * Illinois Open Source License *
3  * *
4  * University of Illinois/NCSA *
5  * Open Source License *
6  * *
7  * Copyright@2008, University of Illinois. All rights reserved. *
8  * *
9  * Developed by: *
10  * *
11  * Center for Simulation of Advanced Rockets *
12  * *
13  * University of Illinois *
14  * *
15  * www.csar.uiuc.edu *
16  * *
17  * Permission is hereby granted, free of charge, to any person *
18  * obtaining a copy of this software and associated documentation *
19  * files (the "Software"), to deal with the Software without *
20  * restriction, including without limitation the rights to use, *
21  * copy, modify, merge, publish, distribute, sublicense, and/or *
22  * sell copies of the Software, and to permit persons to whom the *
23  * Software is furnished to do so, subject to the following *
24  * conditions: *
25  * *
26  * *
27  * @ Redistributions of source code must retain the above copyright *
28  * notice, this list of conditions and the following disclaimers. *
29  * *
30  * @ Redistributions in binary form must reproduce the above *
31  * copyright notice, this list of conditions and the following *
32  * disclaimers in the documentation and/or other materials *
33  * provided with the distribution. *
34  * *
35  * @ Neither the names of the Center for Simulation of Advanced *
36  * Rockets, the University of Illinois, nor the names of its *
37  * contributors may be used to endorse or promote products derived *
38  * from this Software without specific prior written permission. *
39  * *
40  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
41  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
42  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
43  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
44  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
46  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
47  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
48  *********************************************************************
49  * Please acknowledge The University of Illinois Center for *
50  * Simulation of Advanced Rockets in works and publications *
51  * resulting from this software or its derivatives. *
52  *********************************************************************/
53 /*
54 Determines the format of, and calls the appropriate
55 routine to read a CSAR fluid block mesh.
56 
57 Orion Sky Lawlor, olawlor@acm.org, 6/8/2001
58 */
59 #include <cstdio>
60 #include <strings.h>
61 #include <cctype>
62 #include <cstdlib>
63 #include <cstring>
64 #include "util.h"
65 
66 //Return true if a ends in suffix
67 bool endsWith(const char *a,const char *suffix)
68 {
69  int al=strlen(a);
70  int sl=strlen(suffix);
71  if (al<sl) return false;
72  const char *a_suff=&a[al-sl];
73  return 0==strcmp(a_suff,suffix);
74 }
75 
76 
77 //Return true if the indicated file exists and is readable
78 bool fileExists(const char *fName) {
79  FILE *f=fopen(fName,"rb");
80  if (f==NULL)
81  return false;
82  else {
83  fclose(f);
84  return true;
85  }
86 }
87 
88 //Find the numeric part of the file name, and increment it
89 // by one.
90 bool incrementAscii(char *cur)
91 {
92  //Starting from end, work backwards until we reach
93  // the first decimal digit.
94  int i=strlen(cur)-1;
95  while (i>=0 && !isdigit(cur[i])) i--;
96  if (i<0) return false; //No digits found
97 
98  //Carry loop
99  while (true) {
100  //Increment the current digit
101  cur[i]++;
102  if (cur[i]!='9'+1)
103  break; //No carry needed-- finished
104  //Otherwise, wrap around and carry to next digit
105  cur[i]='0';
106  i--;
107  if (i<0 || !isdigit(cur[i])) return false; //Too many carries
108  }
109  return true;
110 }
111 
112 
113 //Return the filename, padded for fortran use
114 // Returns a reference to a static internal buffer
115 // (NOT THREADSAFE)
116 const char *fortranifyString(const char *src)
117 {
118  const int bufLen=500;
119  static char buf[bufLen];
120  strcpy(buf,src);
121  //Pad with spaces
122  for (int i=strlen(buf);i<bufLen;i++) buf[i]=' ';
123  buf[bufLen-1]=0; //Zero-terminate for C use
124  return buf;
125 }
126 
127 //Replace this string's extention with the given new string
128 string replaceExtention(const string &a,const string &newSuffix)
129 {
130  string ret(a);
131  int dotLoc=ret.rfind(".");
132  if (dotLoc==-1) return ret.append(newSuffix);
133  return ret.erase(dotLoc).append(newSuffix);
134 }
135 
136 
137 
138 
139 
140 
bool fileExists(const char *fName)
bool incrementAscii(char *cur)
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
void int int int REAL REAL REAL const char * fName
Definition: write.cpp:76
bool endsWith(const char *a, const char *suffix)
string replaceExtention(const string &a, const string &newSuffix)
blockLoc i
Definition: read.cpp:79
const char * fortranifyString(const char *src)
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494