📄 wml_compiler.c
字号:
error(0, "WML compiler: no name in a setvar element"); return FAILED; } ret = check_variable_syntax(name, NOESC); octstr_destroy(name); return ret;}/* * get_do_element_name - returns the name for a do element. Name is either * name when the element has the attribute or defaults to the type attribute * if there is no name. */static Octstr *get_do_element_name(xmlNodePtr node){ Octstr *name = NULL; xmlAttrPtr attr; if ((attr = node->properties) != NULL) { while (attr != NULL) { if (strcmp(attr->name, "name") == 0) { name = create_octstr_from_node(attr->children); break; } attr = attr->next; } if (attr == NULL) { attr = node->properties; while (attr != NULL) { if (strcmp(attr->name, "type") == 0) { name = create_octstr_from_node(attr->children); break; } attr = attr->next; } } } return name;}/* * check_if_url - checks whether the attribute value is an URL or some other * kind of value. Returns 1 for an URL and 0 otherwise. */static int check_if_url(int hex){ switch ((unsigned char) hex) { case 0x4A: case 0x4B: case 0x4C: /* href, href http://, href https:// */ case 0x32: case 0x58: case 0x59: /* src, src http://, src https:// */ return 1; break; } return 0;}/* * check_if_emphasis - checks if the node is an emphasis element. * Returns 1 for an emphasis and 0 otherwise. */static int check_if_emphasis(xmlNodePtr node){ if (node == NULL || node->name == NULL) return 0; if (strcmp(node->name, "b") == 0) return 1; if (strcmp(node->name, "big") == 0) return 1; if (strcmp(node->name, "em") == 0) return 1; if (strcmp(node->name, "i") == 0) return 1; if (strcmp(node->name, "small") == 0) return 1; if (strcmp(node->name, "strong") == 0) return 1; if (strcmp(node->name, "u") == 0) return 1; return 0;}/* * wml_table_len - returns the length of a wml_table_t array. */static int wml_table_len(wml_table_t *table){ int i = 0; while (table[i].text != NULL) i++; return i;}/* * wml_table3_len - returns the length of a wml_table3_t array. */static int wml_table3_len(wml_table3_t *table){ int i = 0; while (table[i].text1 != NULL) i++; return i;}/* * string_table_create - reserves memory for the string_table_t and sets the * fields. */static string_table_t *string_table_create(int offset, Octstr *ostr){ string_table_t *node; node = gw_malloc(sizeof(string_table_t)); node->offset = offset; node->string = ostr; return node;}/* * string_table_destroy - frees the memory allocated for the string_table_t. */static void string_table_destroy(string_table_t *node){ if (node != NULL) { octstr_destroy(node->string); gw_free(node); }}/* * string_table_proposal_create - reserves memory for the * string_table_proposal_t and sets the fields. */static string_table_proposal_t *string_table_proposal_create(Octstr *ostr){ string_table_proposal_t *node; node = gw_malloc(sizeof(string_table_proposal_t)); node->count = 1; node->string = ostr; return node;}/* * string_table_proposal_destroy - frees the memory allocated for the * string_table_proposal_t. */static void string_table_proposal_destroy(string_table_proposal_t *node){ if (node != NULL) { octstr_destroy(node->string); gw_free(node); }}/* * string_table_build - collects the strings from the WML source into a list, * adds those strings that appear more than once into string table. The rest * of the strings are sliced into words and the same procedure is executed to * the list of these words. */static void string_table_build(xmlNodePtr node, wml_binary_t **wbxml){ string_table_proposal_t *item = NULL; List *list = NULL; list = list_create(); string_table_collect_strings(node, list); list = string_table_add_many(string_table_sort_list(list), wbxml); list = string_table_collect_words(list); /* Don't add strings if there aren't any. (no NULLs please) */ if (list) { list = string_table_add_many(string_table_sort_list(list), wbxml); } /* Memory cleanup. */ while (list_len(list)) { item = list_extract_first(list); string_table_proposal_destroy(item); } list_destroy(list, NULL);}/* * string_table_collect_strings - collects the strings from the WML * ocument into a list that is then further processed to build the * string table for the document. */static void string_table_collect_strings(xmlNodePtr node, List *strings){ Octstr *string; xmlAttrPtr attribute; switch (node->type) { case XML_TEXT_NODE: string = create_octstr_from_node(node); octstr_shrink_blanks(string); octstr_strip_blanks(string); if (octstr_len(string) > WBXML_STRING_TABLE_MIN) octstr_strip_nonalphanums(string); if (octstr_len(string) > WBXML_STRING_TABLE_MIN) list_append(strings, string); else octstr_destroy(string); break; case XML_ELEMENT_NODE: if(node->properties != NULL) { attribute = node->properties; while (attribute != NULL) { if (attribute->children != NULL) string_table_collect_strings(attribute->children, strings); attribute = attribute->next; } } break; default: break; } if (node->children != NULL) string_table_collect_strings(node->children, strings); if (node->next != NULL) string_table_collect_strings(node->next, strings);}/* * string_table_sort_list - takes a list of octet strings and returns a list * of string_table_proposal_t:s that contains the same strings with number of * instants of every string in the input list. */static List *string_table_sort_list(List *start){ int i; Octstr *string = NULL; string_table_proposal_t *item = NULL; List *sorted = NULL; sorted = list_create(); while (list_len(start)) { string = list_extract_first(start); /* Check whether the string is unique. */ for (i = 0; i < list_len(sorted); i++) { item = list_get(sorted, i); if (octstr_compare(item->string, string) == 0) { octstr_destroy(string); string = NULL; item->count ++; break; } } if (string != NULL) { item = string_table_proposal_create(string); list_append(sorted, item); } } list_destroy(start, NULL); return sorted;}/* * string_table_add_many - takes a list of string with number of instants and * adds those whose number is greater than 1 into the string table. Returns * the list ofrejected strings for memory cleanup. */static List *string_table_add_many(List *sorted, wml_binary_t **wbxml){ string_table_proposal_t *item = NULL; List *list = NULL; list = list_create(); while (list_len(sorted)) { item = list_extract_first(sorted); if (item->count > 1 && octstr_len(item->string) > WBXML_STRING_TABLE_MIN) { string_table_add(octstr_duplicate(item->string), wbxml); string_table_proposal_destroy(item); } else list_append(list, item); } list_destroy(sorted, NULL); return list;}/* * string_table_collect_words - takes a list of strings and returns a list * of words contained by those strings. */static List *string_table_collect_words(List *strings){ Octstr *word = NULL; string_table_proposal_t *item = NULL; List *list = NULL, *temp_list = NULL; while (list_len(strings)) { item = list_extract_first(strings); if (list == NULL) { list = octstr_split_words(item->string); string_table_proposal_destroy(item); } else { temp_list = octstr_split_words(item->string); while ((word = list_extract_first(temp_list)) != NULL) list_append(list, word); list_destroy(temp_list, NULL); string_table_proposal_destroy(item); } } list_destroy(strings, NULL); return list;}/* * string_table_add - adds a string to the string table. Duplicates are * discarded. The function returns the offset of the string in the * string table; if the string is already in the table then the offset * of the first copy. */static unsigned long string_table_add(Octstr *ostr, wml_binary_t **wbxml){ string_table_t *item = NULL; unsigned long i, offset = 0; /* Check whether the string is unique. */ for (i = 0; i < (unsigned long)list_len((*wbxml)->string_table); i++) { item = list_get((*wbxml)->string_table, i); if (octstr_compare(item->string, ostr) == 0) { octstr_destroy(ostr); return item->offset; } } /* Create a new list item for the string table. */ offset = (*wbxml)->string_table_length; item = string_table_create(offset, ostr); (*wbxml)->string_table_length = (*wbxml)->string_table_length + octstr_len(ostr) + 1; list_append((*wbxml)->string_table, item); return offset;}/* * string_table_apply - takes a octet string of WML bnary and goes it * through searching for substrings that are in the string table and * replaces them with string table references. */static void string_table_apply(Octstr *ostr, wml_binary_t **wbxml){ Octstr *input = NULL; string_table_t *item = NULL; long i = 0, word_s = 0, str_e = 0; input = octstr_create(""); for (i = 0; i < list_len((*wbxml)->string_table); i++) { item = list_get((*wbxml)->string_table, i); if (octstr_len(item->string) > WBXML_STRING_TABLE_MIN) /* No use to replace 1 to 3 character substring, the reference will eat the saving up. A variable will be in the string table even though it's only 1 character long. */ if ((word_s = octstr_search(ostr, item->string, 0)) >= 0) { /* Check whether the octet string are equal if they are equal in length. */ if (octstr_len(ostr) == octstr_len(item->string)) { if ((word_s = octstr_compare(ostr, item->string)) == 0) { octstr_truncate(ostr, 0); octstr_append_char(ostr, WBXML_STR_T); octstr_append_uintvar(ostr, item->offset); str_e = 1; } } /* Check the possible substrings. */ else if (octstr_len(ostr) > octstr_len(item->string)) { if (word_s + octstr_len(item->string) == octstr_len(ostr)) str_e = 1; octstr_delete(ostr, word_s, octstr_len(item->string)); octstr_truncate(input, 0); /* Substring in the start? No STR_END then. */ if (word_s > 0) octstr_append_char(input, WBXML_STR_END); octstr_append_char(input, WBXML_STR_T); octstr_append_uintvar(input, item->offset); /* Subtring the end? No need to start a new one. */ if ( word_s < octstr_len(ostr)) octstr_append_char(input, WBXML_STR_I); octstr_insert(ostr, input, word_s); } /* If te string table entry is longer than the string, it can be skipped. */ } } octstr_destroy(input); if (octstr_get_char(ostr, 0) != WBXML_STR_T) output_st_char(WBXML_STR_I, wbxml); if (!str_e) octstr_append_char(ostr, WBXML_STR_END); output_st_octet_string(ostr, wbxml);}/* * string_table_output - writes the contents of the string table * into an octet string that is sent to the phone. */static void string_table_output(Octstr *ostr, wml_binary_t **wbxml){ string_table_t *item; while ((item = list_extract_first((*wbxml)->string_table)) != NULL) { octstr_insert(ostr, item->string, octstr_len(ostr)); octstr_append_char(ostr, WBXML_STR_END); string_table_destroy(item); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -