- commited Michael George's config-file patch
[supertux.git] / src / type.cpp
1 //
2 // C Implementation: type
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "SDL_image.h"
14 #include "string.h"
15 #include "stdlib.h"
16 #include "setup.h"
17 #include "globals.h"
18 #include "screen.h"
19 #include "defines.h"
20 #include "type.h"
21 #include "scene.h"
22
23 void string_list_init(string_list_type* pstring_list)
24 {
25   pstring_list->num_items = 0;
26   pstring_list->active_item = -1;
27   pstring_list->item = NULL;
28 }
29
30 char* string_list_active(string_list_type* pstring_list)
31 {
32   if(pstring_list == NULL)
33     return "";
34
35   if(pstring_list->active_item != -1)
36     return pstring_list->item[pstring_list->active_item];
37   else
38     return "";
39 }
40
41 void string_list_add_item(string_list_type* pstring_list,const  char* str)
42 {
43   char *pnew_string;
44   pnew_string = (char*) malloc(sizeof(char)*(strlen(str)+1));
45   strcpy(pnew_string,str);
46   ++pstring_list->num_items;
47   pstring_list->item = (char**) realloc(pstring_list->item,sizeof(char**)*pstring_list->num_items);
48   pstring_list->item[pstring_list->num_items-1] = pnew_string;
49   if(pstring_list->active_item == -1)
50     pstring_list->active_item = 0;
51 }
52
53 void string_list_copy(string_list_type* pstring_list, string_list_type pstring_list_orig)
54 {
55   int i;
56   string_list_free(pstring_list);
57   for(i = 0; i < pstring_list_orig.num_items; ++i)
58     string_list_add_item(pstring_list,pstring_list_orig.item[i]);
59 }
60
61 int string_list_find(string_list_type* pstring_list,const  char* str)
62 {
63   int i;
64   for(i = 0; i < pstring_list->num_items; ++i)
65     {
66       if(strcmp(pstring_list->item[i],str) == 0)
67         {
68           return i;
69         }
70     }
71   return -1;
72 }
73
74 void string_list_sort(string_list_type* pstring_list)
75 {
76   int i,j,y;
77
78   for(j = 0; j < pstring_list->num_items; ++j)
79     for(i = 0; i < pstring_list->num_items-1; ++i)
80       {
81
82         y = strcmp(pstring_list->item[i],pstring_list->item[i+1]);
83         if(y == 0)
84           {
85             continue;
86           }
87         else if(y < 0)
88           {
89             continue;
90           }
91         else if(y > 0)
92           {
93             char* char_pointer;
94             char_pointer = pstring_list->item[i];
95             pstring_list->item[i] = pstring_list->item[i+1];
96             pstring_list->item[i+1] = char_pointer;
97             continue;
98           }
99
100       }
101
102 }
103
104 void string_list_free(string_list_type* pstring_list)
105 {
106   if(pstring_list != NULL)
107     {
108       int i;
109       for(i=0; i < pstring_list->num_items; ++i)
110         free(pstring_list->item[i]);
111       free(pstring_list->item);
112       pstring_list->item = NULL;
113       pstring_list->num_items = 0;
114       pstring_list->active_item = -1;
115     }
116 }