libStatGen Software  1
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 
00045 int StringAlias::GetAliases(StringArray & list) const
00046 {
00047     if(lookup.Entries() == 0)
00048     {
00049         return 0;
00050     }
00051 
00052     int edits = 0;
00053     for(int i = 0; i < list.Length(); i++)
00054     {
00055         int index = lookup.Integer(list[i]);
00056         if(index >= 0)
00057         {
00058             list[i] = aliases[index];
00059             edits++;
00060         }
00061     }
00062     return edits;
00063 }
00064 
00065 
00066 bool StringAlias::ReadFromFile(const char * filename)
00067 {
00068     IFILE input = ifopen(filename, "rt");
00069 
00070     if (input == NULL)
00071         return false;
00072 
00073     ReadFromFile(input);
00074 
00075     ifclose(input);
00076 
00077     return true;
00078 }
00079 
00080 bool StringAlias::ReadFromFile(IFILE & input)
00081 {
00082     StringArray lines, tokens;
00083     lines.Read(input);
00084 
00085     for (int j = 0; j < lines.Length(); j++)
00086     {
00087         tokens.ReplaceTokens(lines[j]);
00088 
00089         if (tokens.Length() != 2) continue;
00090 
00091         SetAlias(tokens[0], tokens[1]);
00092     }
00093 
00094     return true;
00095 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends