APPLY_RSA
[apps/home/calculator.git] / theme / src / calc-string.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *    http://www.tizenopensource.org/license
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include "calc-main.h"
22 #include "calc-string.h"
23 #include <glib.h>
24
25 #undef  BUFLEN
26 #define BUFLEN 1024
27
28 /*
29  * Insert str2 to str at index.
30  */
31 void string_insert(char *str, int index, char *str2)
32 {
33         int buf_size = strlen(str) + strlen(str2) + 1;
34         char *buf = (char *)malloc(buf_size);
35         if (buf == NULL) {
36                 return;
37         }
38         memset(buf, 0, buf_size);
39         strncpy(buf, str, index);
40         buf[index] = '\0';
41         strncat(buf, str2, buf_size - 1 - strlen(buf)); //don't use g_strlcat, because has bug.
42          strncat(buf, str + index, buf_size - 1 - strlen(buf));
43         strcpy(str, buf);
44         free(buf);
45 }
46
47
48 /*
49  * Replace a with b in str locally.
50  */
51 void string_replace(char *str, char *a, char *b)
52 {
53         char *pch = NULL;
54         char buf[BUFLEN] = { 0 };
55         if(strcmp(a, b)){
56                 while ((pch = strstr(str, a)) != NULL) {
57                         strncpy(buf, str, pch - str);
58                         buf[pch - str] = '\0';
59                         g_strlcat(buf, b, sizeof(buf));
60                         g_strlcat(buf, pch + strlen(a), sizeof(buf));
61                         strcpy(str, buf);
62                 }
63         }
64 }
65
66 void string_remove_at(char *str, int at, int length)
67 {
68         int i = at;
69         while ((str[i] = str[i + length]) != '\0') {
70                 i++;
71         }
72 }
73
74
75 #ifdef _DEBUG
76 /* @attention   DON'T REMOVE
77 char* itoa(int i)
78 {
79         char *a = (char *)malloc(42);
80         if (a) { sprintf(a, "%d", i); }
81         return a;
82 }
83 */
84 #endif  /* _DEBUG */
85
86 /* Remove later.
87 if (i != 0)//last digit number
88 {
89     //if ((new_node) && (CALCULATOR_GET_NODE_DATA(new_node)->tmp_result == PI || CALCULATOR_GET_NODE_DATA(new_node)->tmp_result == EXPONENT))
90     if ((new_node) && (FLOAT_EQUAL(CALCULATOR_GET_NODE_DATA(new_node)->tmp_result,PI)
91         || FLOAT_EQUAL(CALCULATOR_GET_NODE_DATA(new_node)->tmp_result, EXPONENT)))
92     {
93         node_data = g_malloc0(sizeof(calculator_node_data_t));
94         node_data->cur_operator = 'x';
95         node_data->negative_flag = 1;
96         node_data->operator_type = OPERATOR_TYPE_BINARY;
97         node_data->node_calcu_priority = CALCULATOR_CALCULATE_PRIORITY_MIDDLE;
98         tree = g_node_new(node_data);
99
100         if (last_node)
101         {
102             __calculator_calculate_insert_node(&last_node, tree, new_node);
103         }
104         else
105         {
106             g_node_insert(tree, -1, new_node);
107             CALCULATOR_GET_NODE_DATA(tree)->children_num++;
108         }
109         last_node = tree;
110         new_node = NULL;
111     }
112
113     factor = atof(tmp);
114     memset(tmp, 0x00, sizeof(tmp));
115     i = 0;
116
117     node_data = g_malloc0(sizeof(calculator_node_data_t));
118     node_data->tmp_result = factor;
119     node_data->negative_flag = 1;
120     node_data->negative_flag *= negative_sign;
121     negative_sign = 1;
122     new_node = g_node_new(node_data);
123
124     if (last_node != NULL)
125     {
126         if (CALCULATOR_GET_NODE_DATA(last_node)->children_num > CALCULATOR_GET_NODE_DATA(last_node)->operator_type)
127         {
128             strcat(error_msg, CALC_MSG_SYNTAX_ERROR);
129             return FALSE;
130         }
131         else
132         {
133             g_node_insert(last_node, -1, new_node);
134             CALCULATOR_GET_NODE_DATA(last_node)->children_num++;
135         }
136         new_node = NULL;
137     }
138 }
139 */
140