apply FSL(Flora Software License)
[apps/home/calculator.git] / src / calc-string.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   * 
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   * 
8   *     http://www.tizenopensource.org/license
9   * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
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         while ((pch = strstr(str, a)) != NULL) {
56                 strncpy(buf, str, pch - str);
57                 buf[pch - str] = '\0';
58                 g_strlcat(buf, b, sizeof(buf));
59                 g_strlcat(buf, pch + strlen(a), sizeof(buf));
60                 strcpy(str, buf);
61          }
62 }
63
64 void string_remove_at(char *str, int at, int length)
65 {
66         int i = at;
67         while ((str[i] = str[i + length]) != '\0') {
68                 i++;
69         }
70 }
71
72
73 /* @ attention
74     This is redundant comment.
75     Its only use is for Klocwork testing.
76     The comment rete should passed 25%.
77     But we have only one day to do this work.
78     So, sorry, I will delete this comment and add useful comment later.
79 */
80
81 #ifdef _DEBUG
82 /* @attention   DON'T REMOVE
83 char* itoa(int i)
84 {
85         char *a = (char *)malloc(42);
86         if (a) { sprintf(a, "%d", i); }
87         return a;
88 }
89 */
90 #endif  /* _DEBUG */
91
92 /* Remove later.
93 if (i != 0)//last digit number
94 {
95     //if ((new_node) && (CALCULATOR_GET_NODE_DATA(new_node)->tmp_result == PI || CALCULATOR_GET_NODE_DATA(new_node)->tmp_result == EXPONENT))
96     if ((new_node) && (FLOAT_EQUAL(CALCULATOR_GET_NODE_DATA(new_node)->tmp_result,PI)
97         || FLOAT_EQUAL(CALCULATOR_GET_NODE_DATA(new_node)->tmp_result, EXPONENT)))
98     {
99         node_data = g_malloc0(sizeof(calculator_node_data_t));
100         node_data->cur_operator = 'x';
101         node_data->negative_flag = 1;
102         node_data->operator_type = OPERATOR_TYPE_BINARY;
103         node_data->node_calcu_priority = CALCULATOR_CALCULATE_PRIORITY_MIDDLE;
104         tree = g_node_new(node_data);
105
106         if (last_node)
107         {
108             __calculator_calculate_insert_node(&last_node, tree, new_node);
109         }
110         else
111         {
112             g_node_insert(tree, -1, new_node);
113             CALCULATOR_GET_NODE_DATA(tree)->children_num++;
114         }
115         last_node = tree;
116         new_node = NULL;
117     }
118
119     factor = atof(tmp);
120     memset(tmp, 0x00, sizeof(tmp));
121     i = 0;
122
123     node_data = g_malloc0(sizeof(calculator_node_data_t));
124     node_data->tmp_result = factor;
125     node_data->negative_flag = 1;
126     node_data->negative_flag *= negative_sign;
127     negative_sign = 1;
128     new_node = g_node_new(node_data);
129
130     if (last_node != NULL)
131     {
132         if (CALCULATOR_GET_NODE_DATA(last_node)->children_num > CALCULATOR_GET_NODE_DATA(last_node)->operator_type)
133         {
134             strcat(error_msg, CALC_MSG_SYNTAX_ERROR);
135             return FALSE;
136         }
137         else
138         {
139             g_node_insert(last_node, -1, new_node);
140             CALCULATOR_GET_NODE_DATA(last_node)->children_num++;
141         }
142         new_node = NULL;
143     }
144 }
145 */
146