00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <serverbrowser/ServerBrowserServerList.h>
00022 #include <algorithm>
00023
00024 ServerBrowserEntry::ServerBrowserEntry()
00025 {
00026 }
00027
00028 ServerBrowserEntry::~ServerBrowserEntry()
00029 {
00030 }
00031
00032 void ServerBrowserEntry::addAttribute(const std::string &name, const std::string &value)
00033 {
00034 attributes_[name] = value;
00035 }
00036
00037 const char *ServerBrowserEntry::getAttribute(const std::string &attrName)
00038 {
00039 static const char *empty = "";
00040
00041 std::map<std::string, std::string>::iterator itor =
00042 attributes_.find(attrName);
00043 if (itor == attributes_.end()) return empty;
00044
00045 return (*itor).second.c_str();
00046 }
00047
00048 ServerBrowserServerList::ServerBrowserServerList() :
00049 refreshId_(0)
00050 {
00051 vectorMutex_ = SDL_CreateMutex();
00052 }
00053
00054 ServerBrowserServerList::~ServerBrowserServerList()
00055 {
00056 SDL_DestroyMutex(vectorMutex_);
00057 }
00058
00059 struct lt_servers
00060 {
00061 lt_servers(const char *name) : name_(name)
00062 {
00063 }
00064
00065 bool isNumber(const char *value)
00066 {
00067 for (const char *a=value; *a; a++)
00068 {
00069 if ((*a < '0' || *a > '9') && *a != '/') return false;
00070 }
00071 return true;
00072 }
00073
00074 bool operator()(const ServerBrowserEntry &o1, const ServerBrowserEntry &o2)
00075 {
00076 std::string o1Value = ((ServerBrowserEntry &)o1).getAttribute(name_);
00077 std::string o2Value = ((ServerBrowserEntry &)o2).getAttribute(name_);
00078
00079 const char *v1 = o1Value.c_str();
00080 const char *v2 = o2Value.c_str();
00081
00082 if (isNumber(v1) && isNumber(v2))
00083 {
00084 int n1 = atoi(v1);
00085 int n2 = atoi(v2);
00086 return n1 < n2;
00087 }
00088
00089 return strcmp(v1, v2) < 0;
00090 }
00091
00092 protected:
00093 const char *name_;
00094 };
00095
00096 void ServerBrowserServerList::sortEntries(const std::string &name)
00097 {
00098 SDL_LockMutex(vectorMutex_);
00099 lt_servers sorter(name.c_str());
00100 std::sort(servers_.begin(), servers_.end(), sorter);
00101 refreshId_++;
00102 SDL_UnlockMutex(vectorMutex_);
00103 }
00104
00105 const char *ServerBrowserServerList::getEntryValue(int pos, const std::string &name)
00106 {
00107 static char buffer[256];
00108 buffer[0] = '\0';
00109
00110 SDL_LockMutex(vectorMutex_);
00111 if (pos >=0 && pos < (int) servers_.size())
00112 {
00113 ServerBrowserEntry &entry = servers_[pos];
00114 snprintf(buffer, sizeof(buffer), "%s", entry.getAttribute(name));
00115 }
00116 SDL_UnlockMutex(vectorMutex_);
00117
00118 return buffer;
00119 }
00120
00121 void ServerBrowserServerList::addEntryValue(int pos,
00122 const std::string &name, const std::string &value)
00123 {
00124 SDL_LockMutex(vectorMutex_);
00125 DIALOG_ASSERT(pos >=0 && pos < (int) servers_.size());
00126 ServerBrowserEntry &entry = servers_[pos];
00127 entry.addAttribute(name, value);
00128 refreshId_++;
00129 SDL_UnlockMutex(vectorMutex_);
00130 }
00131
00132 int ServerBrowserServerList::getNoEntries()
00133 {
00134 SDL_LockMutex(vectorMutex_);
00135 int size = (int) servers_.size();
00136 SDL_UnlockMutex(vectorMutex_);
00137
00138 return size;
00139 }
00140
00141 void ServerBrowserServerList::addEntry(ServerBrowserEntry &newEntry)
00142 {
00143 SDL_LockMutex(vectorMutex_);
00144 servers_.push_back(newEntry);
00145 refreshId_++;
00146 SDL_UnlockMutex(vectorMutex_);
00147 }
00148
00149 void ServerBrowserServerList::clear()
00150 {
00151 SDL_LockMutex(vectorMutex_);
00152 servers_.clear();
00153 refreshId_++;
00154 SDL_UnlockMutex(vectorMutex_);
00155 }
00156