Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

util.cpp

Go to the documentation of this file.
00001 // util.cpp
00002 //
00003 // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
00004 //
00005 // Miscellaneous useful functions.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 
00012 #include "util.h"
00013 
00014 using namespace std;
00015 
00016 
00017 int compareIgnoringCase(const string& s1, const string& s2)
00018 {
00019     string::const_iterator i1 = s1.begin();
00020     string::const_iterator i2 = s2.begin();
00021 
00022     while (i1 != s1.end() && i2 != s2.end())
00023     {
00024         if (toupper(*i1) != toupper(*i2))
00025             return (toupper(*i1) < toupper(*i2)) ? -1 : 1;
00026         ++i1;
00027         ++i2;
00028     }
00029 
00030     return s2.size() - s1.size();
00031 }
00032 
00033 
00034 int compareIgnoringCase(const string& s1, const string& s2, int n)
00035 {
00036     string::const_iterator i1 = s1.begin();
00037     string::const_iterator i2 = s2.begin();
00038 
00039     while (i1 != s1.end() && i2 != s2.end() && n > 0)
00040     {
00041         if (toupper(*i1) != toupper(*i2))
00042             return (toupper(*i1) < toupper(*i2)) ? -1 : 1;
00043         ++i1;
00044         ++i2;
00045         n--;
00046     }
00047 
00048     if (n > 0)
00049         return s2.size() - s1.size();
00050     else
00051         return 0;
00052 }
00053 
00054 
00055 bool CompareIgnoringCasePredicate::operator()(const string& s1,
00056                                               const string& s2) const
00057 {
00058     return compareIgnoringCase(s1, s2) < 0;
00059 }

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1