01ed687aed60802fd334875ffb78f4ef42ed650f
[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 using namespace SuperTux;
26
27 void SuperTux::string_list_init(string_list_type* pstring_list)
28 {
29   pstring_list->num_items = 0;
30   pstring_list->active_item = -1;
31   pstring_list->item = NULL;
32 }
33
34 char* SuperTux::string_list_active(string_list_type* pstring_list)
35 {
36   if(pstring_list == NULL)
37     return "";
38
39   if(pstring_list->active_item != -1)
40     return pstring_list->item[pstring_list->active_item];
41   else
42     return "";
43 }
44
45 void SuperTux::string_list_add_item(string_list_type* pstring_list,const  char* str)
46 {
47   char *pnew_string;
48   pnew_string = (char*) malloc(sizeof(char)*(strlen(str)+1));
49   strcpy(pnew_string,str);
50   ++pstring_list->num_items;
51   pstring_list->item = (char**) realloc(pstring_list->item,sizeof(char**)*pstring_list->num_items);
52   pstring_list->item[pstring_list->num_items-1] = pnew_string;
53   if(pstring_list->active_item == -1)
54     pstring_list->active_item = 0;
55 }
56
57 void SuperTux::string_list_copy(string_list_type* pstring_list, string_list_type pstring_list_orig)
58 {
59   int i;
60   string_list_free(pstring_list);
61   for(i = 0; i < pstring_list_orig.num_items; ++i)
62     string_list_add_item(pstring_list,pstring_list_orig.item[i]);
63 }
64
65 int SuperTux::string_list_find(string_list_type* pstring_list,const  char* str)
66 {
67   int i;
68   for(i = 0; i < pstring_list->num_items; ++i)
69     {
70       if(strcmp(pstring_list->item[i],str) == 0)
71         {
72           return i;
73         }
74     }
75   return -1;
76 }
77
78 void SuperTux::string_list_sort(string_list_type* pstring_list)
79 {
80   int i,j,y;
81
82   for(j = 0; j < pstring_list->num_items; ++j)
83     for(i = 0; i < pstring_list->num_items-1; ++i)
84       {
85
86         y = strcmp(pstring_list->item[i],pstring_list->item[i+1]);
87         if(y == 0)
88           {
89             continue;
90           }
91         else if(y < 0)
92           {
93             continue;
94           }
95         else if(y > 0)
96           {
97             char* char_pointer;
98             char_pointer = pstring_list->item[i];
99             pstring_list->item[i] = pstring_list->item[i+1];
100             pstring_list->item[i+1] = char_pointer;
101             continue;
102           }
103
104       }
105
106 }
107
108 void SuperTux::string_list_free(string_list_type* pstring_list)
109 {
110   if(pstring_list != NULL)
111     {
112       int i;
113       for(i=0; i < pstring_list->num_items; ++i)
114         free(pstring_list->item[i]);
115       free(pstring_list->item);
116       pstring_list->item = NULL;
117       pstring_list->num_items = 0;
118       pstring_list->active_item = -1;
119     }
120 }
121