StringAlias.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 }