3 * Copyright 2012 Samsung Electronics Co., Ltd
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
9 * http://www.tizenopensource.org/license
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.
21 #include "calc-main.h"
22 #include "calc-string.h"
29 * Insert str2 to str at index.
31 void string_insert(char *str, int index, char *str2)
33 int buf_size = strlen(str) + strlen(str2) + 1;
34 char *buf = (char *)malloc(buf_size);
38 memset(buf, 0, buf_size);
39 strncpy(buf, str, index);
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));
49 * Replace a with b in str locally.
51 void string_replace(char *str, char *a, char *b)
54 char buf[BUFLEN] = { 0 };
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));
66 void string_remove_at(char *str, int at, int length)
69 while ((str[i] = str[i + length]) != '\0') {
76 /* @attention DON'T REMOVE
79 char *a = (char *)malloc(42);
80 if (a) { sprintf(a, "%d", i); }
87 if (i != 0)//last digit number
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)))
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);
102 __calculator_calculate_insert_node(&last_node, tree, new_node);
106 g_node_insert(tree, -1, new_node);
107 CALCULATOR_GET_NODE_DATA(tree)->children_num++;
114 memset(tmp, 0x00, sizeof(tmp));
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;
122 new_node = g_node_new(node_data);
124 if (last_node != NULL)
126 if (CALCULATOR_GET_NODE_DATA(last_node)->children_num > CALCULATOR_GET_NODE_DATA(last_node)->operator_type)
128 strcat(error_msg, CALC_MSG_SYNTAX_ERROR);
133 g_node_insert(last_node, -1, new_node);
134 CALCULATOR_GET_NODE_DATA(last_node)->children_num++;