added comments
[supertux.git] / lib / special / stringlist.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include "string.h"
22 #include "stdlib.h"
23 #include "special/stringlist.h"
24
25 void string_list_init(string_list_type* pstring_list)
26 {
27   pstring_list->num_items = 0;
28   pstring_list->active_item = -1;
29   pstring_list->item = NULL;
30 }
31
32 char* string_list_active(string_list_type* pstring_list)
33 {
34   if(pstring_list == NULL)
35     return "";
36
37   if(pstring_list->active_item != -1)
38     return pstring_list->item[pstring_list->active_item];
39   else
40     return "";
41 }
42
43 void string_list_add_item(string_list_type* pstring_list,const  char* str)
44 {
45   char *pnew_string;
46   pnew_string = (char*) malloc(sizeof(char)*(strlen(str)+1));
47   strcpy(pnew_string,str);
48   ++pstring_list->num_items;
49   pstring_list->item = (char**) realloc(pstring_list->item,sizeof(char**)*pstring_list->num_items);
50   pstring_list->item[pstring_list->num_items-1] = pnew_string;
51   if(pstring_list->active_item == -1)
52     pstring_list->active_item = 0;
53 }
54
55 void string_list_copy(string_list_type* pstring_list, string_list_type pstring_list_orig)
56 {
57   int i;
58   string_list_free(pstring_list);
59   for(i = 0; i < pstring_list_orig.num_items; ++i)
60     string_list_add_item(pstring_list,pstring_list_orig.item[i]);
61 }
62
63 int string_list_find(string_list_type* pstring_list,const  char* str)
64 {
65   int i;
66   for(i = 0; i < pstring_list->num_items; ++i)
67     {
68       if(strcmp(pstring_list->item[i],str) == 0)
69         {
70           return i;
71         }
72     }
73   return -1;
74 }
75
76 void string_list_sort(string_list_type* pstring_list)
77 {
78   int i,j,y;
79
80   for(j = 0; j < pstring_list->num_items; ++j)
81     for(i = 0; i < pstring_list->num_items-1; ++i)
82       {
83
84         y = strcmp(pstring_list->item[i],pstring_list->item[i+1]);
85         if(y == 0)
86           {
87             continue;
88           }
89         else if(y < 0)
90           {
91             continue;
92           }
93         else if(y > 0)
94           {
95             char* char_pointer;
96             char_pointer = pstring_list->item[i];
97             pstring_list->item[i] = pstring_list->item[i+1];
98             pstring_list->item[i+1] = char_pointer;
99             continue;
100           }
101
102       }
103
104 }
105
106 void string_list_free(string_list_type* pstring_list)
107 {
108   if(pstring_list != NULL)
109     {
110       int i;
111       for(i=0; i < pstring_list->num_items; ++i)
112         free(pstring_list->item[i]);
113       free(pstring_list->item);
114       pstring_list->item = NULL;
115       pstring_list->num_items = 0;
116       pstring_list->active_item = -1;
117     }
118 }
119