project_files/frontlib/model/teamlist.c
changeset 7320 e704706008d4
parent 7316 f7b49b2c5d84
child 10017 de822cd3df3a
equal deleted inserted replaced
7318:a446eafcddeb 7320:e704706008d4
    31 }
    31 }
    32 
    32 
    33 void flib_teamlist_destroy(flib_teamlist *list) {
    33 void flib_teamlist_destroy(flib_teamlist *list) {
    34 	if(list) {
    34 	if(list) {
    35 		for(int i=0; i<list->teamCount; i++) {
    35 		for(int i=0; i<list->teamCount; i++) {
    36 			flib_team_release(list->teams[i]);
    36 			flib_team_destroy(list->teams[i]);
    37 		}
    37 		}
    38 		free(list->teams);
    38 		free(list->teams);
    39 		free(list);
    39 		free(list);
    40 	}
    40 	}
    41 }
    41 }
    53 }
    53 }
    54 
    54 
    55 int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) {
    55 int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) {
    56 	if(!log_badargs_if2(list==NULL, team==NULL)
    56 	if(!log_badargs_if2(list==NULL, team==NULL)
    57 			&& !insertTeam(&list->teams, &list->teamCount, team, pos)) {
    57 			&& !insertTeam(&list->teams, &list->teamCount, team, pos)) {
    58 		flib_team_retain(team);
       
    59 		return 0;
    58 		return 0;
    60 	}
    59 	}
    61 	return -1;
    60 	return -1;
    62 }
    61 }
    63 
    62 
    66 	if(!log_badargs_if2(list==NULL, name==NULL)) {
    65 	if(!log_badargs_if2(list==NULL, name==NULL)) {
    67 		int itemid = findTeam(list, name);
    66 		int itemid = findTeam(list, name);
    68 		if(itemid>=0) {
    67 		if(itemid>=0) {
    69 			flib_team *team = list->teams[itemid];
    68 			flib_team *team = list->teams[itemid];
    70 			if(!deleteTeam(&list->teams, &list->teamCount, itemid)) {
    69 			if(!deleteTeam(&list->teams, &list->teamCount, itemid)) {
    71 				flib_team_release(team);
    70 				flib_team_destroy(team);
    72 				result = 0;
    71 				result = 0;
    73 			}
    72 			}
    74 		}
    73 		}
    75 	}
    74 	}
    76 	return result;
    75 	return result;
    88 }
    87 }
    89 
    88 
    90 void flib_teamlist_clear(flib_teamlist *list) {
    89 void flib_teamlist_clear(flib_teamlist *list) {
    91 	if(!log_badargs_if(list==NULL)) {
    90 	if(!log_badargs_if(list==NULL)) {
    92 		for(int i=0; i<list->teamCount; i++) {
    91 		for(int i=0; i<list->teamCount; i++) {
    93 			flib_team_release(list->teams[i]);
    92 			flib_team_destroy(list->teams[i]);
    94 		}
    93 		}
    95 		free(list->teams);
    94 		free(list->teams);
    96 		list->teams = NULL;
    95 		list->teams = NULL;
    97 		list->teamCount = 0;
    96 		list->teamCount = 0;
    98 	}
    97 	}
   105 	flib_teamlist *result = flib_teamlist_create();
   104 	flib_teamlist *result = flib_teamlist_create();
   106 	if(result) {
   105 	if(result) {
   107 		bool error = false;
   106 		bool error = false;
   108 		for(int i=0; !error && i<list->teamCount; i++) {
   107 		for(int i=0; !error && i<list->teamCount; i++) {
   109 			flib_team *teamcopy = flib_team_copy(list->teams[i]);
   108 			flib_team *teamcopy = flib_team_copy(list->teams[i]);
   110 			if(!teamcopy) {
   109 			if(!teamcopy || flib_teamlist_insert(result, teamcopy, i)) {
       
   110 				flib_team_destroy(teamcopy);
   111 				error = true;
   111 				error = true;
   112 			} else {
       
   113 				error |= flib_teamlist_insert(result, teamcopy, i);
       
   114 			}
   112 			}
   115 			flib_team_release(teamcopy);
       
   116 		}
   113 		}
   117 		if(error) {
   114 		if(error) {
   118 			flib_teamlist_destroy(result);
   115 			flib_teamlist_destroy(result);
   119 			result = NULL;
   116 			result = NULL;
   120 		}
   117 		}