StringAlias.cpp

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "StringAlias.h"
00019 #include "InputFile.h"
00020 
00021 void StringAlias::SetAlias(String & string, String & alias)
00022 {
00023     int index = lookup.Integer(string);
00024 
00025     if (index < 0)
00026     {
00027         aliases.Push(alias);
00028         lookup.SetInteger(string, aliases.Length() - 1);
00029     }
00030     else
00031         aliases[index] = alias;
00032 }
00033 
00034 const String & StringAlias::GetAlias(const String & string) const
00035 {
00036     int index = lookup.Integer(string);
00037 
00038     if (index < 0)
00039         return string;
00040     else
00041         return aliases[index];
00042 }
00043 
00044 bool StringAlias::ReadFromFile(const char * filename)
00045 {
00046     IFILE input = ifopen(filename, "rt");
00047 
00048     if (input == NULL)
00049         return false;
00050 
00051     ReadFromFile(input);
00052 
00053     ifclose(input);
00054 
00055     return true;
00056 }
00057 
00058 bool StringAlias::ReadFromFile(IFILE & input)
00059 {
00060     StringArray lines, tokens;
00061     lines.Read(input);
00062 
00063     for (int j = 0; j < lines.Length(); j++)
00064     {
00065         tokens.ReplaceTokens(lines[j]);
00066 
00067         if (tokens.Length() != 2) continue;
00068 
00069         SetAlias(tokens[0], tokens[1]);
00070     }
00071 
00072     return true;
00073 }
Generated on Tue Aug 23 18:19:05 2011 for libStatGen Software by  doxygen 1.6.3