/* Asterisk INI to Property List File Conversion Utility -- Version 1.10 * * plistgen.c * aini2plist * * Property list code generator * * Author: Benjamin Kowarsch * * (C) 2006 Sunrise Telephone Systems Ltd. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * * In countries and territories where the above no-warranty disclaimer is * not permissible by applicable law, the following terms apply: * * NO PERMISSION TO USE THE SOFTWARE IS GRANTED AND THE SOFTWARE MUST NOT BE * USED AT ALL IN SUCH COUNTRIES AND TERRITORIES WHERE THE ABOVE NO-WARRANTY * DISCLAIMER IS NOT PERMISSIBLE AND INVALIDATED BY APPLICABLE LAW. HOWEVER, * THE COPYRIGHT HOLDERS HEREBY WAIVE THEIR RIGHT TO PURSUE OFFENDERS AS LONG * AS THEY OTHERWISE ABIDE BY THE TERMS OF THE LICENSE AS APPLICABLE FOR USE * OF THE SOFTWARE IN COUNTRIES AND TERRITORIES WHERE THE ABOVE NO-WARRANTY * DISCLAIMER IS PERMITTED BY APPLICABLE LAW. THIS WAIVER DOES NOT CONSTITUTE * A LICENSE TO USE THE SOFTWARE IN COUNTRIES AND TERRITORIES WHERE THE ABOVE * NO-WARRANTY DISCLAIMER IS NOT PERMISSIBLE AND INVALIDATED BY APPLICABLE * LAW. ANY LIABILITY OF ANY KIND IS CATEGORICALLY RULED OUT AT ALL TIMES. */ #include #include #include "ASCII.h" #include "globaldefs.h" #include "keywords.h" #include "intermediate.h" #include "plistgen.h" // for now let's write to stdout #define emit printf // -------------------------------------------------------------------------- // private function: is_all_alphanum_or_underscore_or_dot(string) // -------------------------------------------------------------------------- // static bool is_all_alphanum_or_underscore_or_dot(const char *string) { CARDINAL index = 0; char ch; ch = string[index]; while ((IS_ALPHANUM(ch)) || (ch == UNDERSCORE) || (ch == DOT) || (ch == CSTRING_TERMINATOR)) { if ((ch == CSTRING_TERMINATOR) || (index > MAXIMUM_LENGTH_FOR_VALUES)) { return true; } // end if index++; ch = string[index]; } // end while return false; } // end is_all_alphanum_or_underscore_or_dot // -------------------------------------------------------------------------- // private function: emit_key_value_assignment(hash) // -------------------------------------------------------------------------- // static int emit_key_value_assignment(CARDINAL section, CARDINAL key) { CARDINAL comment_count, value_count, index; char *str = NULL; // get number of values assigned to this key value_count = get_value_count_for_key(section, key); if (value_count == 0) { // no values assigned return 0; } // end if // get number of single line comments assigned to this key comment_count = get_comment_count_for_key(section, key); // emit all single line comments assigned to this key index = 1; while (index <= comment_count) { emit("\t// %s\n", get_comment_for_key_at_index(section, key, index)); index++; } // end while if (is_obsolete_keyword(section, key) == true) { emit("\t// parameter '%s' is obsolete\n", get_name_of_key(section, key)); return 1; } // end if // emit this key's key-value assignment if (is_multi_value_key(section, key) == true) { emit("\t%s = ( %s", get_name_of_key(section, key), get_value_for_key_at_index(section, key, 1)); index = 2; while (index <= value_count) { emit(", %s", get_value_for_key_at_index(section, key, index)); index++; } // end while emit(" );\n"); } else /* single-value key */ { str = get_value_for_key_at_index(section, key, 1); if ((str[0] == DOUBLE_QUOTE) || (is_all_alphanum_or_underscore_or_dot(str))) { // no quotes required emit("\t%s = %s;\n", get_name_of_key(section, key), str); } else /* value has non alphanum/underscore/dots in it */ { // quotes required emit("\t%s = \"%s\";\n", get_name_of_key(section, key), str); } // end if } // end if return 1; } // end emit_key_value_assignment // -------------------------------------------------------------------------- // function emit_text(string) // -------------------------------------------------------------------------- // int emit_text(const char *string) { if (string == NULL) { return 0; } // end if emit("%s", string); return 1; } // end emit_text // -------------------------------------------------------------------------- // function emit_pending_comments() // -------------------------------------------------------------------------- // int emit_pending_comments() { CARDINAL comment_count, index; // get number of pending comments comment_count = get_pending_comment_count(); if (comment_count == 0) { return 0; } // end if // emit any pending comments index = 1; while (index <= comment_count) { emit("// %s\n", get_pending_comment_at_index(index)); index++; } // end while return 1; } // end emit_pending_comments // -------------------------------------------------------------------------- // function emit_section(section) // -------------------------------------------------------------------------- // int emit_section(CARDINAL section) { CARDINAL thisKey, comment_count, key_count, index; const char *ident; if (section == 0) { return 0; } // end if // get number of single line comments assigned to this section comment_count = get_comment_count_for_section(section); // emit all single line comments assigned to this section index = 1; while (index <= comment_count) { emit("// %s\n", get_comment_for_section_at_index(section, index)); index++; } // end while ident = get_name_of_section(section); // emit section header emit("%s = {\n", ident); // get number of keys key_count = get_key_count_for_section(section); // emit section body if (key_count > 0) { index = 1; while (index <= key_count) { thisKey = get_key_for_section_at_index(section, index); emit_key_value_assignment(section, thisKey); index++; } // end while } else /* no key value assignments */ { emit("\t// empty section\n"); } // end if // emit section closure emit("};\n\n"); return 1; } // end emit_section // -------------------------------------------------------------------------- // function create_plist(pathname) // -------------------------------------------------------------------------- // int create_plist(const char *outfile) { CARDINAL section, count, index; // move relocatable keys move_all_relocatable_keys(); // emit all sections count = get_section_count(); index = 1; while (index <= count) { section = get_section_at_index(index); emit_section(section); index++; } // end while // remove the IR tree remove_all_sections(); return 1; } // end create_plist // END OF FILE