Files
@ 29928fb1ab2b
Branch filter:
Location: DistRen/src/server/user_mgr.c
29928fb1ab2b
5.3 KiB
text/plain
moved macro declaration _GNU_SOURCE to configure.ac
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | /*
Copyright 2009 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
This file is a part of DistRen.
DistRen is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DistRen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with DistRen. If not, see <http://www.gnu.org/licenses/>.
*/
#include "user_mgr.h"
#include "malloc.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct user_mgr_info
{
struct user *user_array;
int current_users;
int user_array_size;
} user_mgr_info;
int resize_user_array()
{
int counter;
int counter2;
// create array twice the size of the current amount of users
user_mgr_info.user_array_size = user_mgr_info.current_users * 2;
struct user *new_user_array = malloc(sizeof(struct user) * user_mgr_info.user_array_size);
// this copies the original user_array over to the new one
// using two counters allows the array to be resized at any time
// leaving exactly 1 open space between each user when it is done;
counter2 = 0;
for(counter = 0; counter < user_mgr_info.current_users; counter++)
{
if(user_mgr_info.user_array[counter].username != 0)
{
new_user_array[counter2*2] = user_mgr_info.user_array[counter];
counter2++;
}
}
// cleanup old array
free(user_mgr_info.user_array);
// change the pointer to point to the new user array
user_mgr_info.user_array = new_user_array;
return 1;
}
struct user *findUser(char *nameOfUser)
{
int high;
int low;
int middle;
int result;
high = user_mgr_info.user_array_size - 1;
low = 0;
for(middle = (low+high)/2; 1 == 1; middle = (low+high)/2)
{
// in case middle lands on a part of the array with no user
while(user_mgr_info.user_array[middle].username == 0)
{
if(result < 0)
middle--;
else
middle++;
}
// this is where the array is cut in half and the half that the nameOfUser is on is kept
result = strcmp(nameOfUser, user_mgr_info.user_array[middle].username);
if(result == 0)
return &user_mgr_info.user_array[middle];
else if(result < 0)
high = middle;
else
low = middle;
// in case the user doesn't exist
if(high-low <= 0)
return 0;
}
}
int deleteUser(struct user *user_ptr)
{
free(user_ptr->username);
memset(user_ptr, '\0', sizeof(struct user));
return 1;
}
int createUser(struct user *user_ptr, char *nameOfUser)
{
// clear old memory
memset(user_ptr, '\0', sizeof(struct user));
user_ptr->username = nameOfUser;
user_ptr->render_power = 0;
user_ptr->last_job = 0;
return 1;
}
// places the new user at position index in the array, moves other users if it needs to
int placeUser(int index, char *nameOfUser)
{
int higher;
int lower;
// I shift data in the array to create an open the space where the user should be added
// but first I figure out which way is the shortest
if(user_mgr_info.user_array[index].username != 0)
{
higher = index + 1;
while(user_mgr_info.user_array[higher].username != 0)
higher++;
lower = index - 1;
while(user_mgr_info.user_array[lower].username != 0)
lower--;
// here the data is shifted to open up a space
if(index - lower < higher - index)
for(; lower < index; lower++)
memcpy(&user_mgr_info.user_array[lower], &user_mgr_info.user_array[lower + 1], sizeof(struct user));
else
for(; higher > index; higher--)
memcpy(&user_mgr_info.user_array[higher], &user_mgr_info.user_array[higher - 1], sizeof(struct user));
}
// add the user to the array
createUser(&user_mgr_info.user_array[index], nameOfUser);
return 1;
}
int addUser(char *nameOfUser)
{
int high;
int low;
int middle;
int result;
high = user_mgr_info.user_array_size - 1;
low = 0;
for(middle = (low+high)/2; 1 == 1; middle = (low+high)/2)
{
// in case middle lands on a part of the array with no user
while(user_mgr_info.user_array[middle].username == 0)
{
if(result < 0)
middle--;
else
middle++;
}
// this is where the array is cut in half and the half that the nameOfUser is on is kept
result = strcmp(nameOfUser, user_mgr_info.user_array[middle].username);
if(result == 0)
return 0;
else if(result < 0)
high = middle;
else
low = middle;
// once there are less than 10 possible places for the user to be placed
// break out of this loop and begin next loop
if(high-low <= 10)
break;
}
// this function starts at the low index number, and goes up until it finds a
// username that is bigger than it alphabetically, and tells the userPlacer
// that it needs to go 1 before that spot
for(; low <= high; low++)
{
while(user_mgr_info.user_array[low].username == 0)
low++;
result = strcmp(nameOfUser, user_mgr_info.user_array[low].username) < 0;
if(result < 0)
{
placeUser(low - 1, nameOfUser);
return 1;
}
if(result == 0)
{
fprintf(stderr, "user already exists");
return 0;
}
}
return 0;
}
int initialize_users()
{
// pull data from XML file
// if XML file is not found create new array of size 50
return 1;
}
|