1 /*
2  *     gtkatlantic - the gtk+ monopd client, enjoy network monopoly games
3  *
4  *
5  *  Copyright © 2002-2015 Sylvain Rochet
6  *
7  *  gtkatlantic is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; see the file COPYING. If not, see
19  *  <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "config.h"
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 
29 #include <gtk/gtk.h>
30 
31 #include "parser.h"
32 
33 /* get identifier in a string to parse, identifier is the first word */
parser_get_identifier(gchar * str)34 gchar *parser_get_identifier(gchar *str)  {
35 
36 	gint32 i, len;
37 	gchar *str2 = NULL;
38 
39 	len = strlen(str);
40 
41 	for(i = 0 ; i < len ; i++)  {
42 
43 		if(str[i] == ' ')  {
44 
45 			str2 = g_strndup(str, i);
46 			break;
47 		}
48 	}
49 
50 	return(str2);
51 }
52 
53 /* get a data from a key in a string to parse */
parser_get_data(gchar * str,gchar * key)54 gchar *parser_get_data(gchar *str, gchar *key)  {
55 
56 	gint32 i, len, len_key2, start = 0;
57 	gchar *key2, *str2 = NULL;
58 	gboolean get_data = 0;
59 
60 	len = strlen(str);
61 
62 	key2 = g_strconcat(key, "=\"", NULL);
63 	len_key2 = strlen(key2);
64 
65 	for(i = 0 ; i < len ; i++)  {
66 
67 		if(!get_data)  {
68 
69 			if(! strncmp(str + i, key2, len_key2) )  {
70 
71 				get_data = TRUE;
72 				i += len_key2 -1;
73 				start = i +1;
74 			}
75 		}
76 		else {
77 
78 			if(str[i] == '"')  {
79 
80 				str2 = g_strndup(str + start, i - start);
81 				break;
82 			}
83 		}
84 	}
85 
86 	g_free(key2);
87 	return str2;
88 }
89