From: <svn...@op...> - 2010-01-18 19:48:45
|
Author: scriptor Date: Mon Jan 18 20:41:28 2010 New Revision: 6015 URL: http://www.opensync.org/changeset/6015 Log: Comments need to be removed from LDIF data, as well. I have added the corresponding function and some missing doxygen annotations. Some of the aleady existing annotations have been slightly changed. Modified: plugins/ldap-sync/src/ldap_format.c plugins/ldap-sync/src/ldap_format.h Modified: plugins/ldap-sync/src/ldap_format.c ============================================================================== --- plugins/ldap-sync/src/ldap_format.c Mon Jan 18 20:41:19 2010 (r6014) +++ plugins/ldap-sync/src/ldap_format.c Mon Jan 18 20:41:28 2010 (r6015) @@ -2447,6 +2447,7 @@ /** + * @note * The following call to osync_xmlformat_copy() is a workaround for * the problem, that libxml2 may or may not free some cur->node * pointers: @@ -2457,8 +2458,8 @@ * * xmlNodePtr xmlAddChild (xmlNodePtr parent, xmlNodePtr cur) * - * Add a new node to @parent, at the end of the child (or property) list - * merging adjacent TEXT nodes (in which case @cur is freed). + * Add a new node to parent, at the end of the child (or property) list + * merging adjacent TEXT nodes (in which case cur is freed). * * If the new node is ATTRIBUTE, it is added into properties instead of * children. @@ -5064,12 +5065,12 @@ /** * @brief Entry function for converting from the ldap-sync plugin internal - * format to xmlformat-contact. - * @param input The data you need to convert - * @param inpsize The size of the input data + * format to xmlformat-contact. + * @param input The data you need to convert. + * @param inpsize The size of the input data. * - * @param output This will contain the result of the conversion - * @param outpsize The will contain the size of the output + * @param output This will contain the result of the conversion. + * @param outpsize The will contain the size of the output. * * @param free_input * "You need to set this to TRUE if opensync @@ -5123,12 +5124,12 @@ /** * @brief Entry function for converting from the ldap-sync plugin internal - * format to xmlformat-contact. - * @param input The data you need to convert - * @param inpsize The size of the input data + * format to xmlformat-contact. + * @param input The data you need to convert. + * @param inpsize The size of the input data. * - * @param output This will contain the result of the conversion - * @param outpsize The will contain the size of the output + * @param output This will contain the result of the conversion. + * @param outpsize The will contain the size of the output. * * @param free_input * "You need to set this to TRUE if opensync @@ -5514,12 +5515,12 @@ /** * @brief Entry function for converting from the ldap-sync plugin internal - * format to xmlformat-event. - * @param input The data you need to convert - * @param inpsize The size of the input data + * format to xmlformat-event. + * @param input The data you need to convert. + * @param inpsize The size of the input data. * - * @param output This will contain the result of the conversion - * @param outpsize The will contain the size of the output + * @param output This will contain the result of the conversion. + * @param outpsize The will contain the size of the output. * * @param free_input * "You need to set this to TRUE if opensync @@ -5625,12 +5626,12 @@ /** * @brief Entry function for converting from the ldap-sync plugin internal - * format to xmlformat-todo. - * @param input The data you need to convert - * @param inpsize The size of the input data + * format to xmlformat-todo. + * @param input The data you need to convert. + * @param inpsize The size of the input data. * - * @param output This will contain the result of the conversion - * @param outpsize The will contain the size of the output + * @param output This will contain the result of the conversion. + * @param outpsize The will contain the size of the output. * * @param free_input * "You need to set this to TRUE if opensync @@ -5677,12 +5678,12 @@ /** * @brief Entry function for converting from xmlformat-note to the - * ldap-sync plugin internal format. - * @param input The data you need to convert - * @param inpsize The size of the input data + * ldap-sync plugin internal format. + * @param input The data you need to convert. + * @param inpsize The size of the input data. * - * @param output This will contain the result of the conversion - * @param outpsize This will contain the size of the output + * @param output This will contain the result of the conversion. + * @param outpsize This will contain the size of the output. * * @param free_input * "You need to set this to TRUE if opensync @@ -5739,12 +5740,12 @@ /** * @brief Entry function for converting from the ldap-sync plugin internal - * format to xmlformat-note. - * @param input The data you need to convert - * @param inpsize The size of the input data + * format to xmlformat-note. + * @param input The data you need to convert. + * @param inpsize The size of the input data. * - * @param output This will contain the result of the conversion - * @param outpsize The will contain the size of the output + * @param output This will contain the result of the conversion. + * @param outpsize The will contain the size of the output. * * @param free_input * "You need to set this to TRUE if opensync @@ -5797,13 +5798,13 @@ /** * @brief Entry function for converting from xmlformat-note to the - * ldap-sync plugin internal format. + * ldap-sync plugin internal format. * - * @param input The data you need to convert - * @param inpsize The size of the input data + * @param input The data you need to convert. + * @param inpsize The size of the input data. * - * @param output This will contain the result of the conversion - * @param outpsize The will contain the size of the output + * @param output This will contain the result of the conversion. + * @param outpsize The will contain the size of the output. * * @param free_input * "You need to set this to TRUE if opensync @@ -6348,15 +6349,34 @@ +/** + * @brief This function performs replacements in a string on the basis + * of regular expressions. Currently it is a helper function + * for ldap_format_remove_comments() and ldap_format_remove_newlines() + * only. Those functions prepare LDIF data for further conversions + * by this plugin. See man 5 ldif for information about LDIF. + * + * @param string This could be any kind of text. Currently, however, + * it will only be filled with the contents of an LDIF file. + * @param replace_pattern This is the pattern that is to be replaced. + * The source pattern, so to speak. + * @param compile_options This int comprises the enum GRegexCompileFlags, such + * as G_REGEX_CASELESS or G_REGEX_MULTILINE. + * @param target_pattern This describes how the replace_pattern should look like + * afterwards. + * @param error The libopensync error pointer. + * + * @returns TRUE on success, FALSE in case of any errors. + * + */ osync_bool -ldap_format_remove_newlines(gchar **string, OSyncError **error) +ldap_format_replace_pattern(gchar **string, const gchar *replace_pattern, const int compile_options, const gchar *target_pattern, OSyncError **error) { GRegex *replace_regex = NULL; - const gchar *replace_pattern = "\\R[ \\t]"; GError *gerror = NULL; - osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, (void *)string, (void *)error); + osync_trace(TRACE_ENTRY, "%s(%p, %p, %i, %p, %p)", __func__, (void *)string, (void *)replace_pattern, compile_options, (void *)target_pattern, (void *)error); if (string == NULL) { @@ -6364,8 +6384,23 @@ goto error; } + if (replace_pattern == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: replace_pattern = NULL.", __FILE__, __LINE__); + goto error; + } - replace_regex = g_regex_new(replace_pattern, G_REGEX_MULTILINE, 0, &gerror); + if (replace_pattern[0] == 0) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: replace_pattern[0] = 0.", __FILE__, __LINE__); + goto error; + } + + if (target_pattern == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: target_pattern = NULL.", __FILE__, __LINE__); + goto error; + } + + + replace_regex = g_regex_new(replace_pattern, compile_options, 0, &gerror); if (gerror != NULL) { osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: g_regex_new() has failed: %s", __FILE__, __LINE__, gerror->message); @@ -6379,7 +6414,8 @@ goto error; } - *string = g_regex_replace_literal(replace_regex, *string, -1, 0, (const gchar *)"", 0, &gerror); + + *string = g_regex_replace_literal(replace_regex, *string, -1, 0, target_pattern, 0, &gerror); if (gerror != NULL) { osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: g_regex_replace_literal() has failed: %s", __FILE__, __LINE__, gerror->message); @@ -6407,8 +6443,124 @@ +/** + * @brief The LDIF format knows of a particular kind of line continuation. + * This function eliminates the line continuation in that it merges + * two consecutive lines into just one line. Helper function for + * ldap_format_parse_ldif(). + * + * @param string An arbitrary string. Currently it is only the contents + * of an LDIF file. + * @param error The libopensync error pointer. + * + * @returns TRUE on success, FALSE in case of any error. + */ +osync_bool +ldap_format_remove_newlines(gchar **string, OSyncError **error) +{ + const gchar *replace_pattern = "\\R[ \\t]"; + const int compile_options = G_REGEX_MULTILINE; + const gchar *target_pattern = ""; + + + osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, (void *)string, (void *)error); + + + if (string == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: string = NULL.", __FILE__, __LINE__); + goto error; + } + + + if (!ldap_format_replace_pattern(string, replace_pattern, compile_options, target_pattern, error)) { + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: string = NULL.", __FILE__, __LINE__); + } + + goto error; + } + + + osync_trace(TRACE_EXIT, "%s()", __func__); + return TRUE; + +error: + if (!osync_error_is_set(error)) + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; + +} + + + +/** + * @brief This function removes comments (= lines beginning with '#') + * from LDIF data. Helper function for ldap_format_parse_ldif(). + * + * @param string An arbitrary string. Currently it is only the contents + * of an LDIF file. + * @param error The libopensync error pointer. + * + * @returns TRUE on success, FALSE in case of any error. + */ + +osync_bool +ldap_format_remove_comments(gchar **string, OSyncError **error) +{ + const gchar *replace_pattern = "^[ \\t]*#.*\\R"; + const int compile_options = G_REGEX_MULTILINE; + const gchar *target_pattern = ""; + + osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, (void *)string, (void *)error); + + if (string == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: string = NULL.", __FILE__, __LINE__); + goto error; + } + + + if (!ldap_format_replace_pattern(string, replace_pattern, compile_options, target_pattern, error)) { + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: string = NULL.", __FILE__, __LINE__); + } + + goto error; + } + + + osync_trace(TRACE_EXIT, "%s()", __func__); + return TRUE; + +error: + if (!osync_error_is_set(error)) + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; + +} + + + + +/** + * @brief This function reads the LDAP attributes of an LDAP entry of an + * LDIF string into a list of LDAPMod's. + * See also ldap_format_append_ldapmod() and /usr/include/ldap.h. + * + * @param string This string contains the LDIF data. + * @param format_name The name of the format, as defined, for example, + * in FORMAT_XMLFORMAT_CONTACT or FORMAT_LDAP_EVOLUTIONPERSON. + * @param ldapmods A Glist, which gets filled with LDAPMod's as a result of + * this function. + * @param error The libopensync error pointer. + * + * @returns TRUE on succes, FALSE in case of any error. + */ osync_bool ldap_format_extract_attributes_from_ldif(const gchar *string, const char *format_name, GList **ldapmods, OSyncError **error) { @@ -6523,7 +6675,21 @@ - +/** + * @brief This function extracts LDAP entries out of LDIF data and prepares + * them for further conversion steps performed by this plugin. + * See also man 5 ldif. + * Helper function for ldap_format_parse_ldif(). + * + * @param string This string contains the LDIF data. + * @param format_name The name of the format, as defined, for example, + * in FORMAT_XMLFORMAT_CONTACT or FORMAT_LDAP_EVOLUTIONPERSON. + * @param ldap_entries A Glist, which gets filled with LDAPMod's as a result of + * this function. + * @param error The libopensync error pointer. + * + * @returns TRUE on succes, FALSE in case of any error. + */ osync_bool ldap_format_read_ldap_entries_from_ldif(const gchar *string, const char *format_name, GList **ldap_entries, OSyncError **error) @@ -6702,6 +6868,21 @@ +/* + * @brief This function reads in LDIF data and prepares it for further + * conversion steps performed by this plugin. + * See also man 5 ldif. + * + * @param data This string contains the LDIF data. + * @param size The length of this string. + * @param format_name The name of the format, as defined, for example, + * in FORMAT_XMLFORMAT_CONTACT or FORMAT_LDAP_EVOLUTIONPERSON. + * @param ldap_entries A Glist, which gets filled with LDAPMod's as a result of + * this function. + * @param error The libopensync error pointer. + * + * @returns TRUE on succes, FALSE in case of any error. +*/ osync_bool ldap_format_parse_ldif(const char *data, const int size, const char *format_name, GList **ldap_entries, OSyncError **error) { @@ -6740,6 +6921,20 @@ // string is NULL-terminated. string = g_strndup(data, size); + if (string == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: string = NULL.", __FILE__, __LINE__); + goto error; + } + + + if (!ldap_format_remove_comments(&string, error)) { + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: ldap_format_remove_comments() has failed.", __FILE__, __LINE__); + } + + goto error; + } + // LDIF uses a strange way of continuation on the next line. This call // merges two of such lines into one line @@ -6751,6 +6946,11 @@ goto error; } + +#ifdef DEBUG_detection + ldap_plugin_printf("%s:%i: string = \n\"%s\"\n", __FILE__, __LINE__, string); +#endif + if (!ldap_format_read_ldap_entries_from_ldif(string, format_name, ldap_entries, error)) { if (!osync_error_is_set(error)) { @@ -6782,6 +6982,22 @@ +/* + * @brief This function checks whether a certain string consists of LDIF data. + * See also man 5 ldif. This is a helper function for + * ldap_format_do_conv_ldap_to_xmlformat() which is part of the + * converter functions, and it is a helper function for + * ldap_format_do_detect_plain_as_ldap_format which is part of the + * detector functions. + * + * @param data This string contains the LDIF data. + * @param size The length of this string. + * @param format_name The name of the format, as defined, for example, + * in FORMAT_XMLFORMAT_CONTACT or FORMAT_LDAP_EVOLUTIONPERSON. + * @param error The libopensync error pointer. + * + * @returns TRUE on succes, FALSE in case of any error. +*/ osync_bool ldap_format_check_ldif(const char *data, int size, const char *format_name, OSyncError **error) Modified: plugins/ldap-sync/src/ldap_format.h ============================================================================== --- plugins/ldap-sync/src/ldap_format.h Mon Jan 18 20:41:19 2010 (r6014) +++ plugins/ldap-sync/src/ldap_format.h Mon Jan 18 20:41:28 2010 (r6015) @@ -87,7 +87,9 @@ static osync_bool ldap_format_post_stylesheet(OSyncXMLFormat *xmlformat, const char *objtype, const char *ldap_format_name, GList **ldap_entries, OSyncError **error); static osync_bool ldap_format_pre_stylesheet(GList *ldap_entries, const char *objtype, const char *ldap_format_name, xmlChar **xmlbuff, int *xmlbuff_size, OSyncError **error); osync_bool ldap_format_read_ldap_entries_from_ldif(const gchar *string, const char *format_name, GList **ldap_entries, OSyncError **error); +osync_bool ldap_format_remove_comments(gchar **string, OSyncError **error); osync_bool ldap_format_remove_newlines(gchar **string, OSyncError **error); +osync_bool ldap_format_replace_pattern(gchar **string, const gchar *replace_pattern, const int compile_options, const gchar *target_pattern, OSyncError **error); static osync_bool ldap_format_validate_xmlformat_by_libopensync(const char *input, const int inpsize, OSyncXMLFormat *xmlformat, const char *str, const char *stylesheet_file, OSyncError **error); static osync_bool ldap_format_validate_xmlformat_by_libxml(const char *input, const int inpsize, xmlDocPtr xmlcard, const char *stylesheet_file, OSyncError **error); static xmlDoc *ldap_format_xmlchars2xmlinternal(xmlChar *xmlchars); |