5b1b1bacf8be97470bd7b17cab5365e0a4ff71c1
[platform/core/telephony/tel-plugin-imc.git] / res / convert_to_sql.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hayoon Ko <hayoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #define TABLE_NAME "mcc_mnc_oper_list"
25 #define TABLE_SCHEMA "create table " TABLE_NAME " (id integer primary key, country char(3), mcc integer, mnc char(3), oper char(45));"
26
27 #define dbg(fmt, args ...) fprintf(stderr, fmt, ## args)
28
29 int main(int argc, char *argv[])
30 {
31         FILE *fp_in;
32
33         char buf[255];
34         char brand[255];
35         char oper[255];
36         char *pos1, *pos2;
37         char country[10];
38         char mnc[10];
39         char *oper_select;
40         int mcc;
41
42         if (argc != 2) {
43                 printf("%s filename.csv\n", argv[0]);
44                 return -1;
45         }
46
47         fp_in = fopen(argv[1], "r");
48         if (fp_in == NULL) {
49                 printf("faild.\n");
50                 return -1;
51         }
52
53         printf("%s\n", TABLE_SCHEMA);
54
55         printf("BEGIN;\n");
56         while (1) {
57                 fgets(buf, 255, fp_in);
58
59                 if (feof(fp_in)) {
60                         break;
61                 }
62
63                 // remove '\n'
64                 buf[strlen(buf) - 1] = '\0';
65
66                 dbg("\n%s\n", buf);
67
68                 pos1 = strchr(buf, ',');
69                 memset(country, 0, 10);
70                 memcpy(country, buf, pos1 - buf);
71
72                 dbg("country=[%s]\n", country);
73
74                 sscanf(pos1 + 1, "%d", &mcc);
75                 dbg("mcc=[%d]\n", mcc);
76
77                 // get mnc
78                 pos1 = strchr(pos1 + 1, ',');
79                 pos2 = strchr(pos1 + 1, ',');
80
81                 dbg("mnc=[%s]\n", pos1 + 1);
82
83                 memset(mnc, 0, 10);
84                 strncpy(mnc, pos1 + 1, pos2 - pos1 - 1);
85
86                 // get brand
87                 pos1 = pos2;
88                 pos2 = strchr(pos1 + 1, ',');
89
90                 dbg("brand=[%s]\n", pos1 + 1);
91
92                 memset(brand, 0, 255);
93                 strncpy(brand, pos1 + 1, pos2 - pos1 - 1);
94
95                 // get oper
96                 pos1 = pos2;
97                 pos2 = strchr(pos1 + 1, ',');
98
99                 dbg("oper=[%s]\n", pos1 + 1);
100
101                 memset(oper, 0, 255);
102                 strcpy(oper, pos1 + 1);
103
104                 oper_select = brand;
105                 if (strlen(brand) == 0)
106                         oper_select = oper;
107
108                 if (oper_select[0] == '\"') {
109                         memset(buf, 0, 255);
110                         snprintf(buf, strlen(oper_select) - 2, "%s", oper_select + 1);
111                         snprintf(oper_select, 255, "%s", buf);
112                 }
113
114                 snprintf(buf, 255, "insert into %s "
115                                                    " (country, mcc, mnc, oper) "
116                                                    " values (\"%s\", %d, \"%s\", \"%s\");",
117                                  TABLE_NAME, country, mcc, mnc, oper_select);
118                 printf("%s\n", buf);
119         }
120         printf("COMMIT;\n");
121
122         fclose(fp_in);
123 }