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