4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7 * Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 #include <json-glib.h>
\r
24 #include "location.h"
\r
29 static int _get_polygon_position_count(int polygon_index)
\r
31 if (parser == NULL || root == NULL) return 0;
\r
33 JsonObject* polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
\r
34 if (polygon_obj == NULL) return 0;
\r
36 JsonArray * positions = json_object_get_array_member (polygon_obj, "positions");
\r
37 if (positions == NULL) return 0;
\r
39 return json_array_get_length(positions);
\r
42 static LocationPosition* _get_position_from_polygon(int polygon_index, int pos_index)
\r
44 double latitude = 0.0;
\r
45 double longitude = 0.0;
\r
46 LocationPosition *position = NULL;
\r
47 if (parser == NULL || root == NULL) return NULL;
\r
49 JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
\r
50 JsonArray * pos_array = json_object_get_array_member(polygon_obj, "positions");
\r
51 JsonObject* pos = json_array_get_object_element(pos_array, pos_index);
\r
53 latitude = json_object_get_double_member(pos, "latitude");
\r
54 longitude = json_object_get_double_member(pos, "longitude");
\r
56 if (latitude == 0.0 || longitude == 0.0) return NULL;
\r
58 position = location_position_new(0, latitude, longitude, 0.0, LOCATION_STATUS_2D_FIX);
\r
63 static LocationPosition* _get_marker_position_from_polygon(int polygon_index, int pos_index)
\r
65 double latitude = 0.0;
\r
66 double longitude = 0.0;
\r
67 LocationPosition *position = NULL;
\r
68 if (parser == NULL || root == NULL) return NULL;
\r
70 JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
\r
71 JsonArray * pos_array = json_object_get_array_member(polygon_obj, "marker_position");
\r
72 JsonObject* pos = json_array_get_object_element(pos_array, pos_index);
\r
74 latitude = json_object_get_double_member(pos, "latitude");
\r
75 longitude = json_object_get_double_member(pos, "longitude");
\r
77 if (latitude == 0.0 || longitude == 0.0) return NULL;
\r
79 position = location_position_new(0, latitude, longitude, 0.0, LOCATION_STATUS_2D_FIX);
\r
84 static void _free_position_list(gpointer data)
\r
86 if (data == NULL) return;
\r
88 LocationPosition *position = (LocationPosition*) data;
\r
90 location_position_free(position);
\r
94 LocationBoundary* json_util_get_polygon_boundary(int polygon_index)
\r
96 if (parser == NULL || root == NULL) {
\r
97 g_printf("invalid param parser[%d], root[%d]\n", parser, root);
\r
100 GList* position_list = NULL;
\r
101 LocationBoundary *boundary = NULL;
\r
103 int pos_count = _get_polygon_position_count(polygon_index);
\r
104 if (pos_count == 0) return NULL;
\r
106 for(index = 0; index < pos_count; index++) {
\r
107 position_list = g_list_append(position_list, _get_position_from_polygon(polygon_index, index));
\r
110 boundary = location_boundary_new_for_polygon(position_list);
\r
112 g_list_free_full(position_list, (GDestroyNotify)_free_position_list);
\r
118 /* Polygon boundary */
\r
119 int json_util_get_polygon_count(void)
\r
121 JsonArray * array = json_node_get_array(root);
\r
123 return json_array_get_length(array);
\r
126 char *json_util_get_polygon_name(int polygon_index)
\r
129 JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
\r
131 name = (char *)json_object_get_string_member(polygon_obj, "name");
\r
132 if (name == NULL) return NULL;
\r
134 return g_strdup(name);
\r
138 char * json_util_get_marker_name(int polygon_index, int pos_index)
\r
140 char *result = NULL;
\r
141 if (parser == NULL || root == NULL) return NULL;
\r
143 JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
\r
144 JsonArray * pos_array = json_object_get_array_member(polygon_obj, "marker_position");
\r
145 JsonObject* pos = json_array_get_object_element(pos_array, pos_index);
\r
147 result = (char *)json_object_get_string_member(pos, "where");
\r
148 if (result == NULL) return NULL;
\r
150 return g_strdup(result);
\r
153 int json_util_get_marker_position_count(int polygon_index)
\r
155 if (parser == NULL || root == NULL) return 0;
\r
157 JsonObject* polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
\r
158 JsonArray * marker_position = json_object_get_array_member (polygon_obj, "marker_position");
\r
160 return json_array_get_length(marker_position);
\r
163 LocationPosition *json_util_get_marker_position(int polygon_index, int marker_index)
\r
165 if (parser == NULL || root == NULL) return NULL;
\r
167 LocationPosition* position = NULL;
\r
168 position = _get_marker_position_from_polygon(polygon_index, marker_index);
\r
173 char* json_util_result_zone_test(int polygon_index, int marker_index)
\r
175 if (parser == NULL || root == NULL) return NULL;
\r
177 char *result = NULL;
\r
178 JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
\r
179 JsonArray * pos_array = json_object_get_array_member(polygon_obj, "marker_position");
\r
180 JsonObject* pos = json_array_get_object_element(pos_array, marker_index);
\r
182 result = (char *)json_object_get_string_member(pos, "result");
\r
183 if (result == NULL) return NULL;
\r
185 return g_strdup(result);
\r
188 void json_util_init(const char * file_name)
\r
190 g_print("Enter init_json_parser");
\r
192 gboolean ret = FALSE;
\r
193 if (parser != NULL) return;
\r
195 parser = json_parser_new();
\r
198 ret = json_parser_load_from_file(parser, file_name, &error);
\r
199 if (ret == FALSE) {
\r
200 g_print("Unable to parser[%s]:[%s]\n", file_name, error->message);
\r
204 root = json_parser_get_root(parser);
\r