- renamed *.c to *.cxx
[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, 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, 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   short int sorter[pstring_list->num_items];
78
79   for(j = 0; j < pstring_list->num_items; ++j)
80     for(i = 0; i < pstring_list->num_items-1; ++i)
81       {
82
83         y = strcmp(pstring_list->item[i],pstring_list->item[i+1]);
84         if(y == 0)
85           {
86             continue;
87           }
88         else if(y < 0)
89           {
90             continue;
91           }
92         else if(y > 0)
93           {
94             char* char_pointer;
95             char_pointer = pstring_list->item[i];
96             pstring_list->item[i] = pstring_list->item[i+1];
97             pstring_list->item[i+1] = char_pointer;
98             continue;
99           }
100
101       }
102
103 }
104
105 void string_list_free(string_list_type* pstring_list)
106 {
107   if(pstring_list != NULL)
108     {
109       int i;
110       for(i=0; i < pstring_list->num_items; ++i)
111         free(pstring_list->item[i]);
112       free(pstring_list->item);
113       pstring_list->item = NULL;
114       pstring_list->num_items = 0;
115       pstring_list->active_item = -1;
116     }
117 }