Merge lp:~godezinc/damn-monkey/parser-init into lp:damn-monkey

Proposed by Vincent PEYROUSE (GodezInc)
Status: Merged
Merged at revision: 15
Proposed branch: lp:~godezinc/damn-monkey/parser-init
Merge into: lp:damn-monkey
Diff against target: 170 lines (+122/-1)
4 files modified
src/credits.c (+2/-0)
src/main_functions.c (+106/-0)
src/main_functions.h (+12/-1)
src/main_menu.c (+2/-0)
To merge this branch: bzr merge lp:~godezinc/damn-monkey/parser-init
Reviewer Review Type Date Requested Status
Fabien LOISON Approve
Review via email: mp+59506@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Fabien LOISON (flozz) wrote :

Hello,

Please init the pointer to NULL line 348 of main_functions.c, and please comment your code :)

review: Needs Fixing
lp:~godezinc/damn-monkey/parser-init updated
16. By Vincent PEYROUSE (GodezInc)

* Split function added (Parser dev. beginning)

Revision history for this message
Fabien LOISON (flozz) wrote :

It's ok for merging

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/credits.c'
2--- src/credits.c 2011-04-27 20:33:58 +0000
3+++ src/credits.c 2011-04-29 14:09:29 +0000
4@@ -207,6 +207,8 @@
5 case SDLK_KP_ENTER:
6 selected = menu->selected;
7 break;
8+ default:
9+ break;
10 }
11 }
12 else if (event.type == SDL_QUIT)
13
14=== modified file 'src/main_functions.c'
15--- src/main_functions.c 2011-04-27 20:33:58 +0000
16+++ src/main_functions.c 2011-04-29 14:09:29 +0000
17@@ -334,3 +334,109 @@
18 }
19
20
21+/**
22+ * \fn DM_Splited* split(char* string, char separator)
23+ * \brief Parse the line considered and return an array of string.
24+ *
25+ * This function read the string in order to create an array of string
26+ *
27+ * \param string The string to split.
28+ * \param separator The separator char (ex : ';').
29+ * \return A DM_Splited containing the array.
30+ */
31+DM_Splited* split(char *string, char separator)
32+{
33+ //Future return initialization
34+ DM_Splited *splited = malloc(sizeof(splited));
35+ if (splited == NULL)
36+ {
37+ fprintf(stderr, "E: Cannot allocate memory.");
38+ exit(EXIT_FAILURE);
39+ }
40+ //Some variables initialization
41+ int counter = 0;
42+ int items_int = 0;
43+ char buffer_c;
44+ //String buffer initialization
45+ char *buffer = malloc(strlen(string) * sizeof(char));
46+ if (buffer == NULL)
47+ {
48+ fprintf(stderr, "E: Cannot allocate memory.");
49+ exit(EXIT_FAILURE);
50+ }
51+ //First loop allowing the counting of items for allocations operations
52+ for (counter=0 ; counter<strlen(string) ; counter++)
53+ {
54+ if (string[counter] == separator)
55+ {
56+ items_int++;
57+ }
58+ else if (string[counter] == ' ' || string[counter] == '\t' || string[counter] == '\r' || string[counter] == '\n')
59+ {
60+ continue;
61+ }
62+ else
63+ {
64+ continue;
65+ }
66+ }
67+
68+ //Allocation of the first dimension of the parameters array
69+ splited->parameters = malloc(items_int * sizeof(char*));
70+ if (splited->parameters == NULL)
71+ {
72+ fprintf(stderr, "E: Cannot allocate memory.");
73+ exit(EXIT_FAILURE);
74+ }
75+ //Assignation of the parameters_int thanks to the first loop
76+ splited->parameters_int = items_int;
77+
78+ items_int = 0;
79+
80+ //Second loop allowing the buffering and the creation of the second dimension of the parameters array
81+ buffer_c = string[0];
82+ //Detects if we have a common char and start buffering -> We got a new item
83+ if (buffer_c != ' ' && buffer_c != '\t' && buffer_c != '\r' && buffer_c != '\n' && buffer_c != separator)
84+ {
85+ strcpy(buffer, &buffer_c);
86+ }
87+ for (counter=1 ; counter<strlen(string) ; counter++)
88+ {
89+ if (string[counter-1] == separator)
90+ {
91+ //Detects if we have an "uncommon" char just after the separator
92+ if (string[counter] == ' ' || string[counter] == '\t' || string[counter] == '\r' || string[counter] == '\n')
93+ {
94+ continue;
95+ }
96+ //Start of a new buffering -> We got a new item
97+ buffer_c = string[counter];
98+ strcpy(buffer, &buffer_c);
99+ continue;
100+ }
101+ if (string[counter] == separator) //Separator found
102+ {
103+ splited->parameters[items_int] = malloc(strlen(buffer) * sizeof(char)); //Allocatation of a new item in the array
104+ if (splited->parameters[items_int] == NULL)
105+ {
106+ fprintf(stderr, "E: Cannot allocate memory.");
107+ exit(EXIT_FAILURE);
108+ }
109+ strcpy(splited->parameters[items_int], buffer); //Assignation of the new item
110+ memset(buffer, '\0', strlen(buffer)); //Empty the buffer
111+ items_int++; //Incrementation of the parameters array "counter"
112+ }
113+ else if (string[counter] == ' ' || string[counter] == '\t' || string[counter] == '\r' || string[counter] == '\n') // Detects if we have an "uncommon" char -> Next turn
114+ {
115+ continue;
116+ }
117+ else //"Common" char found -> Buffering
118+ {
119+ buffer_c = string[counter];
120+ strcat(buffer, &buffer_c);
121+ }
122+ }
123+
124+ return splited;
125+}
126+
127
128=== modified file 'src/main_functions.h'
129--- src/main_functions.h 2010-12-30 13:04:51 +0000
130+++ src/main_functions.h 2011-04-29 14:09:29 +0000
131@@ -44,6 +44,17 @@
132 SDL_Rect rect; /*!< The rectangle. */
133 };
134
135+/**
136+ * \struct DM_Splited
137+ * \brief Structure for parsing files.
138+ */
139+typedef struct DM_Splited DM_Splited;
140+struct DM_Splited
141+{
142+ int parameters_int; /*!< The number of strings in the array */
143+ char **parameters; /*!< The array of strings */
144+};
145+
146
147 SDL_Surface* load_resource(char *resource_name);
148 DM_Surface* load_resource_as_dm_surface(char *resource_name);
149@@ -51,7 +62,7 @@
150 SDL_Surface* str_to_surface(char *font_name, char *str);
151 Mix_Chunk* load_sound_resource(char *resource_name);
152 Mix_Music* load_music_resource(char *resource_name);
153-
154+DM_Splited* split(char *string, char separator);
155
156 #endif //MAIN_FUNCTIONS_H_INCLUDED
157
158
159=== modified file 'src/main_menu.c'
160--- src/main_menu.c 2011-04-27 20:33:58 +0000
161+++ src/main_menu.c 2011-04-29 14:09:29 +0000
162@@ -143,6 +143,8 @@
163 case SDLK_KP_ENTER:
164 selected = menu->selected;
165 break;
166+ default:
167+ break;
168 }
169 }
170 else if (event.type == SDL_QUIT)

Subscribers

People subscribed via source and target branches