From: <svn...@op...> - 2009-09-17 20:28:53
|
Author: dgollub Date: Thu Sep 17 22:28:40 2009 New Revision: 5790 URL: http://www.opensync.org/changeset/5790 Log: Introduce OSyncError** parameter to OSyncTime API, due to replacment of g_error0 by osync_try_malloc0 Patch by bricks - testsuite porting by dgollub Modified: trunk/opensync/format/opensync_time.c trunk/opensync/format/opensync_time.h trunk/tests/format-tests/check_time.c Modified: trunk/opensync/format/opensync_time.c ============================================================================== --- trunk/opensync/format/opensync_time.c Thu Sep 17 22:17:50 2009 (r5789) +++ trunk/opensync/format/opensync_time.c Thu Sep 17 22:28:40 2009 (r5790) @@ -171,11 +171,16 @@ /*****************************************************************************/ -struct tm *osync_time_vtime2tm(const char *vtime) +struct tm *osync_time_vtime2tm(const char *vtime, OSyncError **error) { - struct tm *utime = g_malloc0(sizeof(struct tm)); osync_trace(TRACE_ENTRY, "%s(%s)", __func__, vtime); - + struct tm *utime = g_try_malloc0(sizeof(struct tm)); + + if (!utime) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + goto error; + } + utime->tm_year = 0; utime->tm_mon = 0; utime->tm_mday = 0; @@ -198,9 +203,12 @@ osync_trace(TRACE_EXIT, "%s", __func__); return utime; +error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return NULL; } -char *osync_time_tm2vtime(const struct tm *time, osync_bool is_utc) +char *osync_time_tm2vtime(const struct tm *time, osync_bool is_utc, OSyncError **error) { GString *vtime = g_string_new(""); struct tm my_time = *time; @@ -234,16 +242,16 @@ /*****************************************************************************/ -time_t osync_time_vtime2unix(const char *vtime, int offset) +time_t osync_time_vtime2unix(const char *vtime, int offset, OSyncError **error) { struct tm *utime = NULL; time_t timestamp; char *utc = NULL; osync_trace(TRACE_ENTRY, "%s(%s, %i)", __func__, vtime, offset); - utc = osync_time_vtime2utc(vtime, offset); - utime = osync_time_vtime2tm(utc); - timestamp = osync_time_utctm2unix(utime); + utc = osync_time_vtime2utc(vtime, offset, error); + utime = osync_time_vtime2tm(utc, error); + timestamp = osync_time_utctm2unix(utime, error); g_free(utc); g_free(utime); @@ -253,14 +261,14 @@ } -char *osync_time_unix2vtime(const time_t *timestamp) +char *osync_time_unix2vtime(const time_t *timestamp, OSyncError **error) { char *vtime; struct tm utc; osync_trace(TRACE_ENTRY, "%s(%lu)", __func__, *timestamp); gmtime_r(timestamp, &utc); - vtime = osync_time_tm2vtime(&utc, TRUE); + vtime = osync_time_tm2vtime(&utc, TRUE, error); osync_trace(TRACE_EXIT, "%s: %s", __func__, vtime); return vtime; @@ -271,10 +279,14 @@ * Unix time_t converters */ -time_t osync_time_localtm2unix(const struct tm *localtime) +time_t osync_time_localtm2unix(const struct tm *localtime, OSyncError **error) { time_t timestamp; - struct tm *tmp = g_malloc0(sizeof(struct tm)); + struct tm *tmp = g_try_malloc0(sizeof(struct tm)); + if (!tmp) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + return -1L; + } memcpy(tmp, localtime, sizeof(struct tm)); @@ -286,12 +298,16 @@ return timestamp; } -time_t osync_time_utctm2unix(const struct tm *utctime) +time_t osync_time_utctm2unix(const struct tm *utctime, OSyncError **error) { time_t timestamp; #if 1 - struct tm *tmp = g_malloc0(sizeof(struct tm)); + struct tm *tmp = g_try_malloc0(sizeof(struct tm)); + if (!tmp) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + return -1; + } struct tm localnow; struct tm check; int tzdiff; @@ -300,7 +316,7 @@ // to reduce the number of loops to find the correct match time(×tamp); localtime_r(×tamp, &localnow); - tzdiff = osync_time_timezone_diff(&localnow); + tzdiff = osync_time_timezone_diff(&localnow, error); // now loop, converting "local time" to time_t to utctm, // and adjusting until there are no differences... this @@ -355,19 +371,27 @@ return timestamp; } -struct tm *osync_time_unix2localtm(const time_t *timestamp) +struct tm *osync_time_unix2localtm(const time_t *timestamp, OSyncError **error) { - struct tm *ptr_tm = g_malloc0(sizeof(struct tm)); - + struct tm *ptr_tm = g_try_malloc0(sizeof(struct tm)); + if (!ptr_tm) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + return NULL; + } + localtime_r(timestamp, ptr_tm); return ptr_tm; } -struct tm *osync_time_unix2utctm(const time_t *timestamp) +struct tm *osync_time_unix2utctm(const time_t *timestamp, OSyncError **error) { - struct tm *ptr_tm = g_malloc0(sizeof(struct tm)); - + struct tm *ptr_tm = g_try_malloc0(sizeof(struct tm)); + if (!ptr_tm) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + return NULL; + } + gmtime_r(timestamp, ptr_tm); return ptr_tm; @@ -375,7 +399,7 @@ /*****************************************************************************/ -int osync_time_timezone_diff(const struct tm *local) +int osync_time_timezone_diff(const struct tm *local, OSyncError **error) { struct tm utime; unsigned int lsecs, usecs; @@ -384,7 +408,7 @@ osync_trace(TRACE_ENTRY, "%s()", __func__); /* convert local time to UTC */ - timestamp = osync_time_localtm2unix(local); + timestamp = osync_time_localtm2unix(local, error); /* convert UTC to split tm struct in UTC */ gmtime_r(×tamp, &utime); @@ -434,10 +458,14 @@ return zonediff; } -struct tm *osync_time_tm2utc(const struct tm *ltime, int offset) +struct tm *osync_time_tm2utc(const struct tm *ltime, int offset, OSyncError **error) { - struct tm *tmtime = g_malloc0(sizeof(struct tm)); osync_trace(TRACE_ENTRY, "%s(%p, %i)", __func__, ltime, offset); + struct tm *tmtime = g_try_malloc0(sizeof(struct tm)); + if (!tmtime) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + goto error; + } tmtime->tm_year = ltime->tm_year; tmtime->tm_mon = ltime->tm_mon; @@ -457,11 +485,18 @@ osync_trace(TRACE_EXIT, "%s: %p", __func__, tmtime); return tmtime; +error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return NULL; } -struct tm *osync_time_tm2localtime(const struct tm *utime, int offset) +struct tm *osync_time_tm2localtime(const struct tm *utime, int offset, OSyncError **error) { - struct tm *tmtime = g_malloc0(sizeof(struct tm)); + struct tm *tmtime = g_try_malloc0(sizeof(struct tm)); + if (!tmtime) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + return NULL; + } tmtime->tm_year = utime->tm_year; tmtime->tm_mon = utime->tm_mon; @@ -482,7 +517,7 @@ return tmtime; } -char *osync_time_vtime2utc(const char* localtime, int offset) +char *osync_time_vtime2utc(const char* localtime, int offset, OSyncError **error) { char *utc = NULL; struct tm *tm_local = NULL, *tm_utc = NULL; @@ -493,9 +528,9 @@ goto end; } - tm_local = osync_time_vtime2tm(localtime); - tm_utc = osync_time_tm2utc(tm_local, offset); - utc = osync_time_tm2vtime(tm_utc, TRUE); + tm_local = osync_time_vtime2tm(localtime, error); + tm_utc = osync_time_tm2utc(tm_local, offset, error); + utc = osync_time_tm2vtime(tm_utc, TRUE, error); g_free(tm_local); g_free(tm_utc); @@ -505,7 +540,7 @@ return utc; } -char *osync_time_vtime2localtime(const char* utc, int offset) +char *osync_time_vtime2localtime(const char* utc, int offset, OSyncError **error) { char *localtime = NULL; struct tm *tm_local = NULL, *tm_utc = NULL; @@ -515,9 +550,9 @@ return localtime; } - tm_utc = osync_time_vtime2tm(utc); - tm_local = osync_time_tm2localtime(tm_utc, offset); - localtime = osync_time_tm2vtime(tm_local, FALSE); + tm_utc = osync_time_vtime2tm(utc, error); + tm_local = osync_time_tm2localtime(tm_utc, offset, error); + localtime = osync_time_tm2vtime(tm_local, FALSE, error); g_free(tm_local); g_free(tm_utc); @@ -558,13 +593,13 @@ NULL }; -/*! @brief Function converts a UTC vtime stamp to a localtime vtime stamp +/** @brief Function converts a UTC vtime stamp to a localtime vtime stamp * * @param entry The whole vcal entry as GString which gets modified. * @param field The field name which should be modified. * @param toUTC The toggle in which direction we convert. TRUE = convert to UTC */ -static void _convert_time_field(GString *entry, const char *field, osync_bool toUTC) +static void _convert_time_field(GString *entry, const char *field, osync_bool toUTC, OSyncError **error) { int i, tzdiff; char *res = NULL; @@ -584,14 +619,14 @@ entry = g_string_erase(entry, pos, i); // Get System offset to UTC - tm_stamp = osync_time_vtime2tm(stamp->str); - tzdiff = osync_time_timezone_diff(tm_stamp); + tm_stamp = osync_time_vtime2tm(stamp->str, error); + tzdiff = osync_time_timezone_diff(tm_stamp, error); g_free(tm_stamp); if (toUTC) - new_stamp = osync_time_vtime2utc(stamp->str, tzdiff); + new_stamp = osync_time_vtime2utc(stamp->str, tzdiff, error); else - new_stamp = osync_time_vtime2localtime(stamp->str, tzdiff); + new_stamp = osync_time_vtime2localtime(stamp->str, tzdiff, error); entry = g_string_insert(entry, pos, new_stamp); g_free(new_stamp); @@ -604,27 +639,27 @@ * @param toUTC If TRUE conversion from localtime to UTC. * @return timestamp modified vcalendar */ -char *_convert_entry(const char *vcal, osync_bool toUTC) +char *_convert_entry(const char *vcal, osync_bool toUTC, OSyncError **error) { int i = 0; GString *new_entry = g_string_new(vcal); for (i=0; _time_attr[i] != NULL; i++) - _convert_time_field(new_entry, _time_attr[i], toUTC); + _convert_time_field(new_entry, _time_attr[i], toUTC, error); return g_string_free(new_entry, FALSE); } -char *osync_time_vcal2localtime(const char *vcal) +char *osync_time_vcal2localtime(const char *vcal, OSyncError **error) { - return _convert_entry(vcal, FALSE); + return _convert_entry(vcal, FALSE, error); } -char *osync_time_vcal2utc(const char *vcal) +char *osync_time_vcal2utc(const char *vcal, OSyncError **error) { - return _convert_entry(vcal, TRUE); + return _convert_entry(vcal, TRUE, error); } /*****************************************************************************/ @@ -780,9 +815,13 @@ return weekday; } -struct tm *osync_time_relative2tm(const char *byday, const int bymonth, const int year) +struct tm *osync_time_relative2tm(const char *byday, const int bymonth, const int year, OSyncError **error) { - struct tm *datestamp = g_malloc0(sizeof(struct tm)); + struct tm *datestamp = g_try_malloc0(sizeof(struct tm)); + if (!datestamp) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Could not allocate memory for time stuct."); + return NULL; + } struct tm search; char weekday[3]; int first_wday = 0, last_wday = 0; Modified: trunk/opensync/format/opensync_time.h ============================================================================== --- trunk/opensync/format/opensync_time.h Thu Sep 17 22:17:50 2009 (r5789) +++ trunk/opensync/format/opensync_time.h Thu Sep 17 22:28:40 2009 (r5790) @@ -86,9 +86,10 @@ /** @brief Function converts vtime to tm struct * * @param vtime The formatted timestamp (YYYYMMDDTHHMMSS) + * @param error An OSyncError struct * @returns struct tm (caller is responsible for freeing) */ -OSYNC_EXPORT struct tm *osync_time_vtime2tm(const char *vtime); +OSYNC_EXPORT struct tm *osync_time_vtime2tm(const char *vtime, OSyncError **error); /** @brief Function converts struct tm in vtime string * @@ -97,9 +98,10 @@ * * @param time The tm struct which gets converted * @param is_utc If struct tm is UTC time is_utc have to be TRUE + * @param error An OSyncError struct * @returns vtime formatted as YYYYMMDDTHHMMSS[Z] (caller is responsible for freeing) */ -OSYNC_EXPORT char *osync_time_tm2vtime(const struct tm *time, osync_bool is_utc); +OSYNC_EXPORT char *osync_time_tm2vtime(const struct tm *time, osync_bool is_utc, OSyncError **error); /*@}*/ @@ -119,16 +121,18 @@ * * @param offset Seconds of UTC offset * @param vtime The osync formmatted timestamp + * @param error An OSyncError struct * @returns Unix timestamp in time_t (UTC) */ -OSYNC_EXPORT time_t osync_time_vtime2unix(const char *vtime, int offset); +OSYNC_EXPORT time_t osync_time_vtime2unix(const char *vtime, int offset, OSyncError **error); /** @brief Function converts unix timestamp to vtime in UTC * * @param timestamp The unix timestamp which gets converted + * @param error An OSyncError struct * @returns vtime formatted as YYYYMMDDTHHMMSSZ (caller is responsible for freeing) */ -OSYNC_EXPORT char *osync_time_unix2vtime(const time_t *timestamp); +OSYNC_EXPORT char *osync_time_unix2vtime(const time_t *timestamp, OSyncError **error); /* Unix time_t converters */ @@ -138,13 +142,15 @@ * forced to -1. Aka, mktime(). * * @param localtime The struct tm, in localtime, which gets converted + * @param error An OSyncError struct * @returns time_t (in UTC of course) */ -OSYNC_EXPORT time_t osync_time_localtm2unix(const struct tm *localtime); +OSYNC_EXPORT time_t osync_time_localtm2unix(const struct tm *localtime, OSyncError **error); /** @brief Function converts struct tm, in utc, to unix timestamp. * * @param utctime The struct tm, in utc, which gets converted + * @param error An OSyncError struct * @returns time_t (in UTC of course) * * This algorithm abuses the POSIX time functions, only because @@ -156,25 +162,27 @@ * * If there is a better way, I'd love to know! - cdfrey */ -OSYNC_EXPORT time_t osync_time_utctm2unix(const struct tm *utctime); +OSYNC_EXPORT time_t osync_time_utctm2unix(const struct tm *utctime, OSyncError **error); /** @brief Function converts unix timestamp to struct tm in localtime. * This is the same as calling localtime_r(), except you * have to free the returned value. Aka, localtime(). * * @param timestamp The unixtimestamp which gets converted + * @param error An OSyncError struct * @returns: struct tm (in localtime) (Caller is responsible for freeing!) */ -OSYNC_EXPORT struct tm *osync_time_unix2localtm(const time_t *timestamp); +OSYNC_EXPORT struct tm *osync_time_unix2localtm(const time_t *timestamp, OSyncError **error); /** @brief Function converts unix timestamp to struct tm in utc. * This is the same as calling gmtime_r(), except you * have to free the returned value. Aka, gmtime(). * * @param timestamp The unixtimestamp which gets converted + * @paran error An OSyncError struct * @returns: struct tm (in UTC) (Caller is responsible for freeing) */ -OSYNC_EXPORT struct tm *osync_time_unix2utctm(const time_t *timestamp); +OSYNC_EXPORT struct tm *osync_time_unix2utctm(const time_t *timestamp, OSyncError **error); /*@}*/ @@ -199,9 +207,10 @@ * * @param local The point in time when the offset have to be calculated, * specified in localtime (need for CEST/CET) + * @param error An OSyncError struct * @returns Seconds of timezone offset */ -OSYNC_EXPORT int osync_time_timezone_diff(const struct tm *local); +OSYNC_EXPORT int osync_time_timezone_diff(const struct tm *local, OSyncError **error); /** @brief Function converts (struct tm) ltime from localtime to UTC. * Paramter offset is used as UTC offset. Note that _only_ the @@ -210,9 +219,10 @@ * * @param ltime The struct tm which gets converted to UTC timezone * @param offset Seconds of UTC offset, in seconds east of UTC. + * @param error An OSyncError struct * @returns struct tm in UTC (caller is responsible for freeing) */ -OSYNC_EXPORT struct tm *osync_time_tm2utc(const struct tm *ltime, int offset); +OSYNC_EXPORT struct tm *osync_time_tm2utc(const struct tm *ltime, int offset, OSyncError **error); /** @brief Function converts (struct tm) utime from UTC to localtime * Paramter offset is used as UTC offset. Note that _only_ the @@ -221,25 +231,28 @@ * * @param utime The struct tm which gets converted to localtime * @param offset Seconds of UTC offset, in seconds east of UTC. + * @param error An OSyncError struct * @returns struct tm in localtime (caller is responsible for freeing) */ -OSYNC_EXPORT struct tm *osync_time_tm2localtime(const struct tm *utime, int offset); +OSYNC_EXPORT struct tm *osync_time_tm2localtime(const struct tm *utime, int offset, OSyncError **error); /** @brief Functions converts a localtime vtime stamp to a UTC vtime stamp * * @param localtime The local timestamp in vtime format * @param offset Seconds of UTC offset, in seconds east of UTC. + * @param error An OSyncError struct * @returns vtime in UTC timezone (caller is responsible for freeing) */ -OSYNC_EXPORT char *osync_time_vtime2utc(const char* localtime, int offset); +OSYNC_EXPORT char *osync_time_vtime2utc(const char* localtime, int offset, OSyncError **error); /** @brief Functions converts a UTC vtime stamp to a localtime vtime stamp * * @param utc The timestap in UTC timezone which gets converted to localtime * @param offset The offset in seconds between UTC and localtime + * @param error An OSyncError struct * @returns vtime in local timezon (caller is preponsible for freeing) */ -OSYNC_EXPORT char *osync_time_vtime2localtime(const char* utc, int offset); +OSYNC_EXPORT char *osync_time_vtime2localtime(const char* utc, int offset, OSyncError **error); /** @brief Function converts UTC offset string in offset in seconds * @@ -267,16 +280,18 @@ /** @brief Functions converts timestamps of vcal to localtime * * @param vcal The vcalendar which has to be converted. + * @param error An OSyncError struct * @return modified vcalendar with local timestamps (related to system time) */ -OSYNC_EXPORT char *osync_time_vcal2localtime(const char *vcal); +OSYNC_EXPORT char *osync_time_vcal2localtime(const char *vcal, OSyncError **error); /** @brief Functions converts timestamps of vcal to UTC * * @param vcal The vcalendar which has to be converted. + * @param error An OSyncError struct * @return modified vcalendar with UTC timestamps (related to system time) */ -OSYNC_EXPORT char *osync_time_vcal2utc(const char *vcal); +OSYNC_EXPORT char *osync_time_vcal2utc(const char *vcal, OSyncError **error); /*@}*/ @@ -343,11 +358,12 @@ * @param byday string of the relative day of month modifier * @param bymonth calendar number of the month (January = 1) * @param year calendar year (e.g. 1970, 2007, etc) + * @param error An OSyncError struct * @returns struct tm of the relative information date with 00:00:00 timestamp * or NULL on error. * (Caller is responsible for freeing) */ -OSYNC_EXPORT struct tm *osync_time_relative2tm(const char *byday, const int bymonth, const int year); +OSYNC_EXPORT struct tm *osync_time_relative2tm(const char *byday, const int bymonth, const int year, OSyncError **error); /*@}*/ Modified: trunk/tests/format-tests/check_time.c ============================================================================== --- trunk/tests/format-tests/check_time.c Thu Sep 17 22:17:50 2009 (r5789) +++ trunk/tests/format-tests/check_time.c Thu Sep 17 22:28:40 2009 (r5790) @@ -15,6 +15,7 @@ START_TEST (time_timezone_diff) { struct tm local; + OSyncError *error = NULL; int zonediff_normal, zonediff_day, zonediff_month; // discover localtime zone @@ -37,17 +38,20 @@ // t = mktime(&local); // gmtime_r(&t, &utc); - zonediff_normal = osync_time_timezone_diff(&local); + zonediff_normal = osync_time_timezone_diff(&local, &error); + fail_unless(error == NULL, NULL); // test day straddle local.tm_hour = 23; - zonediff_day = osync_time_timezone_diff(&local); + zonediff_day = osync_time_timezone_diff(&local, &error); + fail_unless(error == NULL, NULL); // test month straddle local.tm_mday = 31; - zonediff_month = osync_time_timezone_diff(&local); + zonediff_month = osync_time_timezone_diff(&local, &error); + fail_unless(error == NULL, NULL); printf("normal = %d\nday = %d\nmonth = %d\n", zonediff_normal, zonediff_day, zonediff_month); @@ -59,10 +63,11 @@ static int test_relative2tm(const char *byday, int month, int year, int expected_day, int expected_wday) { + OSyncError *error = NULL; printf("Test parameters: %s, month: %d, year: %d\n", byday, month, year); - struct tm *test = osync_time_relative2tm(byday, month, year); + struct tm *test = osync_time_relative2tm(byday, month, year, &error); if( !test ) { printf(" Error in osync_time_relative2tm()\n"); return expected_day == -1; @@ -121,19 +126,22 @@ void test_unix_converter(const struct tm *base, const char *vresult) { + OSyncError *error = NULL; struct tm tm_first, tm_second, *tm_ptr = NULL; time_t first, second; char *vtime = NULL; // test that osync_time_localtm2unix() behaves like mktime() memcpy(&tm_first, base, sizeof(struct tm)); - first = osync_time_localtm2unix(&tm_first); + first = osync_time_localtm2unix(&tm_first, &error); + fail_unless(error == NULL, NULL); tm_first.tm_isdst = -1; second = mktime(&tm_first); fail_unless( first == second, NULL ); // test that osync_time_unix2localtm() behaves like localtime() - tm_ptr = osync_time_unix2localtm(&first); + tm_ptr = osync_time_unix2localtm(&first, &error); + fail_unless(error == NULL, NULL); localtime_r(&first, &tm_second); fail_unless( tm_equal(&tm_first, &tm_second), NULL ); fail_unless( tm_equal(tm_ptr, &tm_second), NULL ); @@ -141,7 +149,8 @@ tm_ptr = NULL; // test that osync_time_unix2utctm() behaves like gmtime_r() - tm_ptr = osync_time_unix2utctm(&first); + tm_ptr = osync_time_unix2utctm(&first, &error); + fail_unless(error == NULL, NULL); gmtime_r(&first, &tm_second); fail_unless( tm_equal(tm_ptr, &tm_second), NULL ); g_free(tm_ptr); @@ -149,15 +158,19 @@ // test that osync_time_utctm2unix() works correctly tm_second.tm_isdst = 0; // make sure incorrect value is handled - second = osync_time_utctm2unix(&tm_second); + second = osync_time_utctm2unix(&tm_second, &error); + fail_unless(error == NULL, NULL); fail_unless( first == second, NULL ); // test vtime string converters, in both directions - vtime = osync_time_unix2vtime(&first); + vtime = osync_time_unix2vtime(&first, &error); + fail_unless(error == NULL, NULL); fail_unless( vtime != NULL, NULL ); fail_unless( strcmp(vtime, vresult) == 0, NULL ); printf("osync_time_unix2vtime() returned: %s\n", vtime); - second = osync_time_vtime2unix(vtime, 0); + second = osync_time_vtime2unix(vtime, 0, &error); + fail_unless(error == NULL, NULL); + fail_unless( vtime != NULL, NULL ); fail_unless( first == second ); g_free(vtime); } |