gmyth/src/gmyth_util.c
author renatofilho
Thu Sep 28 15:41:06 2006 +0100 (2006-09-28)
branchtrunk
changeset 18 79f6da40f6e9
parent 1 gmyth/src/libgmyth/gmyth_util.c@ffdf467315ec
child 35 a615ba8f8804
permissions -rw-r--r--
[svn r19] - splited libgmyth;
     1 /**
     2 * GMyth Library
     3 *
     4 * @file gmyth/gmyth_util.c
     5 * 
     6 * @brief <p> This component provides utility functions.
     7 * 
     8 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
     9 * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
    10 *
    11 *//*
    12 * 
    13 * This program is free software; you can redistribute it and/or modify
    14 * it under the terms of the GNU Lesser General Public License as published by
    15 * the Free Software Foundation; either version 2 of the License, or
    16 * (at your option) any later version.
    17 *
    18 * This program is distributed in the hope that it will be useful,
    19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    21 * GNU General Public License for more details.
    22 *
    23 * You should have received a copy of the GNU Lesser General Public License
    24 * along with this program; if not, write to the Free Software
    25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    26 */
    27 
    28 #include "gmyth_util.h"
    29 
    30 #include <glib.h>
    31 #include <glib/gprintf.h>
    32 
    33 /** Converts a time_t struct in a GString at ISO standard format 
    34  * (e.g. 2006-07-20T09:56:41).
    35  * 
    36  * The returned GString memory should be deallocated from 
    37  * the calling function.
    38  *
    39  * @param time_value the time value to be converted
    40  * @return GString* the converted isoformat string 
    41  */
    42 GString*
    43 gmyth_util_time_to_isoformat (time_t time_value)
    44 {
    45 	struct tm tm_time;
    46 	GString *result;
    47 	
    48 	if (localtime_r(&time_value, &tm_time) == NULL) {
    49 		g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
    50         return NULL;
    51 	}
    52 	
    53 	result = g_string_sized_new(20);
    54 	g_string_printf(result, "%04d-%02d-%02dT%02d:%02d:%02d",
    55 		tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
    56 		tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
    57 	
    58 	return result;
    59 }
    60 
    61 /** Converts a time_t struct in a GString to the following 
    62  * format (e.g. 2006-07-20 09:56:41).
    63  * 
    64  * The returned GString memory should be deallocated from 
    65  * the calling function.
    66  *
    67  * @param time_value the time value to be converted
    68  * @return GString* the converted string 
    69  */
    70 GString*
    71 gmyth_util_time_to_string (time_t time_value)
    72 {
    73 	GString *result = gmyth_util_time_to_isoformat (time_value);
    74 	result->str[10] = ' ';	
    75 
    76 	return result;
    77 }
    78 
    79 /** Converts a GString in the following format 
    80  * (e.g. 2006-07-20 09:56:41) to a time_t struct.
    81  * 
    82  * @param time_str the string to be converted
    83  * @return time_t the time converted value
    84  */
    85 time_t
    86 gmyth_util_string_to_time (GString* time_str)
    87 {
    88 	int year, month, day, hour, min, sec;
    89 
    90 	g_debug( "[%s] time_str = %s.\n", __FUNCTION__, time_str != NULL ? time_str->str : "[time string is NULL!]" );
    91 
    92 	if (sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
    93 			&year, &month, &day, &hour, &min, &sec) < 3) { /* At least date */
    94 		g_warning ("GMythUtil: isoformat_to_time converter error!\n");
    95 		return 0;
    96 	} else {
    97 		struct tm tm_time;
    98 		tm_time.tm_year = year - 1900;
    99 		tm_time.tm_mon = month - 1;
   100 		tm_time.tm_mday = day;
   101 		tm_time.tm_hour = hour;
   102 		tm_time.tm_min = min;
   103 		tm_time.tm_sec = sec;
   104 		
   105 		return mktime (&tm_time);
   106 	}
   107 }
   108 
   109 /** Decodes a long long variable from the string list
   110  * format of the myhtprotocol.
   111  * 
   112  * @param strlist the string list of mythprotocol values
   113  * @param offset  the list node offset of the long long variable
   114  * @return guint64  the long long converted value
   115  */
   116 guint64
   117 gmyth_util_decode_long_long(GMythStringList *strlist, guint offset) 
   118 {
   119 
   120 	guint64 ret_value = 0LL;
   121 
   122 	g_return_val_if_fail( strlist != NULL, ret_value );
   123 
   124 	if ( offset < gmyth_string_list_length( strlist ))
   125 		g_printerr( "[%s] Offset is lower than the Stringlist (offset = %d)!\n", 
   126                     __FUNCTION__, offset );
   127     
   128 	g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
   129 
   130 	gint l1 = gmyth_string_list_get_int( strlist, offset );
   131 	gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
   132 
   133 	ret_value = ((guint64)(l2) & 0xffffffffLL) | ((guint64)(l1) << 32);
   134 
   135 	return ret_value;
   136 
   137 }
   138