Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members
UserList.cpp
00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: UserList.cpp (S H A R E D) * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 /*********************************************************************** 00045 * Video Session Migration System, 2004 * 00046 * Author: Klaus Schoeffmann * 00047 ************************************************************************/ 00048 00049 00050 #include "UserList.hpp" 00051 00052 using namespace std; 00053 00054 00055 /***********************************************************************/ 00056 UserList::UserList() 00057 {} 00058 00059 /***********************************************************************/ 00060 UserList::~UserList() 00061 { 00062 Globals::sdebug << "\nUserList::~UserList() cleanup..."; 00063 vector<User*>::iterator iter = list.begin(); 00064 while (iter != list.end()) 00065 { 00066 User *usr = *iter; 00067 delete usr; 00068 iter++; 00069 } 00070 list.clear(); 00071 Globals::sdebug << "\nUserList::~UserList() done!"; 00072 } 00073 00074 /***********************************************************************/ 00075 void UserList::addUser(User* u) 00076 { 00077 list.push_back(u); 00078 } 00079 00080 /***********************************************************************/ 00081 User* UserList::getUserByName(const char *username) 00082 { 00083 uint i; 00084 for (i=0; i < list.size(); i++) 00085 { 00086 User *user = list[i]; 00087 if (strcmp(user->getUsername(), username) == 0) return user; 00088 } 00089 00090 return NULL; 00091 } 00092 00093 /***********************************************************************/ 00094 User* UserList::getUserById(const uint userId) 00095 { 00096 uint i; 00097 for (i=0; i < list.size(); i++) 00098 { 00099 User *user = list[i]; 00100 if (user->getUserId() == userId) return user; 00101 } 00102 00103 return NULL; 00104 } 00105 00106 /***********************************************************************/ 00107 vector<User*> UserList::getUsers() 00108 { 00109 return list; 00110 } 00111 00112 /***********************************************************************/ 00113 void UserList::readUsersFromFile(const char *filename) 00114 { 00115 char line[READLINE_MAXLEN], *buffer; 00116 char *username, *password; 00117 uint userId; 00118 uint usersCount = 0; 00119 00120 //open file of users 00121 ifstream fp (filename); 00122 if (!fp) 00123 { 00124 cerr << "\nUserList::readUsersFromFile(): Error opening file '" << filename << "'"; 00125 exit (1); 00126 } 00127 00128 //reset variables used for reading 00129 userId = 0; 00130 username = password = NULL; 00131 00132 //go to start of users 00133 uint zaehler = 0; 00134 while (! fp.eof() ) 00135 { 00136 zaehler++; 00137 fp.getline(line,READLINE_MAXLEN); 00138 if (line != NULL && strncmp(line, "############USERS start############", strlen("############USERS start############")) == 0) break; 00139 } 00140 00141 //read line by line until end of file 00142 //Attention: maximum line characters is set in Globals.hpp 00143 while (! fp.eof() ) 00144 { 00145 fp.getline (line, READLINE_MAXLEN); 00146 if (line != NULL) 00147 { 00148 if (strncmp(line, "############USERS end############", strlen("############USERS end############")) == 0) break; 00149 00150 //Globals::sdebug << "\nUL::readUsersFromFile: read line:\n" << line; 00151 if (line[0] == '#') 00152 { 00153 //ignore comments 00154 } 00155 //look for username 00156 else if (line[0] == '[' && line[strlen(line) - 1] == ']') 00157 { 00158 username = new char[strlen(line) - 1]; 00159 strncpy(username, line + 1, strlen(line) - 2); 00160 username[strlen(line) - 2] = 0; 00161 } 00162 //look for userid 00163 else if ((buffer = strstr(line, "uid=")) != NULL) 00164 { 00165 userId = atoi(buffer + 4); 00166 } 00167 //look for password 00168 else if ((buffer = strstr(line, "pwd=")) != NULL) 00169 { 00170 password = new char[strlen(buffer) - 3]; 00171 strncpy(password, buffer + 4, strlen(buffer) - 4); 00172 password[strlen(buffer) - 4] = 0; 00173 } 00174 else 00175 { 00176 //Unrecognized parameter ignored 00177 } 00178 } 00179 00180 //if all necessary information was read, add user to list 00181 //and reset variables used for reading 00182 if (userId != 0 && username != NULL && password != NULL) 00183 { 00184 00185 Globals::sdebug << "\nUL::readUsersFromFile: create User: " << userId << " " << username << " " << password; 00186 addUser(new User(userId, username, password)); 00187 usersCount++; 00188 00189 userId = 0; 00190 username = password = NULL; 00191 } 00192 00193 } 00194 00195 fp.close(); 00196 00197 Globals::sdebug << "\nUserList::readUsersFromFile(): read users successfully finished with " << usersCount << "entries!"; 00198 }