00001 00002 /* 00003 | Author: Michael Schmidt; 00004 | Gunnar Jehl 00005 | 00006 | ************************* SOFTWARE LICENSE AGREEMENT *********************** 00007 | This source code is published under the BSD License. 00008 | 00009 | See file 'LICENSE.txt' that comes with this distribution or 00010 | http://dbis.informatik.uni-freiburg.de/index.php?project=GCX/license.php 00011 | for the full license agreement. 00012 | 00013 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00014 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00015 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00016 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00017 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00018 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00019 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00020 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00021 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00022 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00023 | POSSIBILITY OF SUCH DAMAGE. 00024 | **************************************************************************** 00025 */ 00026 00035 #include "varname.h" 00036 #include "outputstream.h" 00037 #include <cstring> 00038 #include <cstdlib> 00039 00040 VarName *VarName::instance = NULL; 00041 00042 VarName *VarName::getInstance() { 00043 if (instance == NULL) { 00044 instance = new VarName(); 00045 } 00046 00047 return instance; 00048 } 00049 00050 VarName::VarName(): 00051 index(1) { 00052 char *root = new char[strlen(ROOTVAR) + 1]; 00053 00054 strcpy(root, ROOTVAR); 00055 varnames.push_back(root); 00056 } 00057 00058 VarName::~VarName() { 00059 for (unsigned i = 0; i < varnames.size(); i++) { 00060 delete[]varnames[i]; 00061 } 00062 } 00063 00064 unsigned VarName::insertVarname(const char *varname, bool force) { 00065 if (!force) { 00066 for (unsigned i = 0; i < varnames.size(); i++) { 00067 if (strcmp(varname, varnames[i]) == 0) { 00068 return i; 00069 } 00070 } 00071 } 00072 00073 char *var_cpy = new char[strlen(varname) + 1]; 00074 00075 strncpy(var_cpy, varname, strlen(varname)); 00076 var_cpy[strlen(varname)] = '\0'; 00077 00078 varnames.push_back(var_cpy); 00079 return varnames.size() - 1; 00080 } 00081 00082 void VarName::print(OutputStream & dos) { 00083 for (unsigned i = 0; i < varnames.size(); i++) { 00084 if (i == (varnames.size() - 1)) { 00085 dos << "$" << varnames[i]; 00086 } else { 00087 dos << "$" << varnames[i] << ", "; 00088 } 00089 } 00090 } 00091 00092 const char *VarName::getFreshVarname() { 00093 std::ostringstream o; 00094 o << "_" << index++; 00095 return strdup(o.str().c_str()); 00096 }