1d142f562740ed60e3e262bb44ec912302bdc4f1
[profile/ivi/tel-plugin-imc.git] / res / convert_to_sql.c
1 /**
2  * tel-plugin-samsung
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * PROPRIETARY/CONFIDENTIAL
9  *
10  * This software is the confidential and proprietary information of SAMSUNG ELECTRONICS ("Confidential Information").
11  * You shall not disclose such Confidential Information and shall
12  * use it only in accordance with the terms of the license agreement you entered into with SAMSUNG ELECTRONICS.
13  * SAMSUNG make no representations or warranties about the suitability
14  * of the software, either express or implied, including but not
15  * limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement.
16  * SAMSUNG shall not be liable for any damages suffered by licensee as
17  * a result of using, modifying or distributing this software or its derivatives.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #define TABLE_NAME "mcc_mnc_oper_list"
24 #define TABLE_SCHEMA "create table " TABLE_NAME " (id integer primary key, country char(3), mcc integer, mnc char(3), oper char(45));"
25
26 #define dbg(fmt,args...) fprintf(stderr, fmt, ##args);
27
28 int main(int argc, char *argv[])
29 {
30         FILE *fp_in;
31
32         char buf[255];
33         char brand[255];
34         char oper[255];
35         char *pos1, *pos2;
36         char country[10];
37         char mnc[10];
38         char *oper_select;
39         int mcc;
40
41         if (argc != 2) {
42                 printf("%s filename.csv\n", argv[0]);
43                 return -1;
44         }
45
46         fp_in = fopen(argv[1], "r");
47         if (fp_in == NULL) {
48                 printf("faild.\n");
49                 return -1;
50         }
51
52         printf("%s\n", TABLE_SCHEMA);
53
54         printf("BEGIN;\n");
55         while (1) {
56                 fgets (buf, 255, fp_in);
57
58                 if (feof(fp_in))  {
59                         break;
60                 }
61
62                 // remove '\n'
63                 buf[strlen(buf)-1] = '\0';
64
65                 dbg("\n%s\n", buf);
66
67                 pos1 = strchr (buf, ',');
68                 memset (country, 0, 10);
69                 memcpy(country, buf, pos1-buf);
70
71                 dbg("country=[%s]\n", country);
72
73                 sscanf (pos1+1, "%d", &mcc);
74                 dbg("mcc=[%d]\n", mcc);
75
76                 // get mnc
77                 pos1 = strchr (pos1+1, ',');
78                 pos2 = strchr (pos1+1, ',');
79
80                 dbg("mnc=[%s]\n", pos1+1);
81
82                 memset (mnc, 0, 10);
83                 strncpy (mnc, pos1+1, pos2-pos1-1);
84
85                 // get brand
86                 pos1 = pos2;
87                 pos2 = strchr (pos1+1, ',');
88
89                 dbg("brand=[%s]\n", pos1+1);
90
91                 memset (brand, 0, 255);
92                 strncpy (brand, pos1+1, pos2-pos1-1);
93
94                 // get oper
95                 pos1 = pos2;
96                 pos2 = strchr (pos1+1, ',');
97
98                 dbg("oper=[%s]\n", pos1+1);
99
100                 memset (oper, 0, 255);
101                 strcpy (oper, pos1+1);
102
103                 oper_select = brand;
104                 if (strlen(brand) == 0)
105                         oper_select = oper;
106
107                 if (oper_select[0] == '\"') {
108                         memset(buf, 0, 255);
109                         snprintf(buf, strlen(oper_select)-2, "%s", oper_select+1);
110                         snprintf(oper_select, 255, "%s", buf);
111                 }
112
113                 snprintf(buf, 255, "insert into %s "
114                                 " (country, mcc, mnc, oper) "
115                                 " values (\"%s\", %d, \"%s\", \"%s\");",
116                                 TABLE_NAME, country, mcc, mnc, oper_select);
117                 printf("%s\n",buf);
118         }
119         printf("COMMIT;\n");
120
121         fclose(fp_in);
122 }