gmyth/src/gmyth_util.c
author rosfran
Tue Dec 12 17:59:47 2006 +0000 (2006-12-12)
branchtrunk
changeset 214 254e85c43269
parent 213 631f2cf13501
child 215 633c6c9784d3
permissions -rw-r--r--
[svn r215] Use the GTimeVal GLib date/time structure, instead of using the time_t from libc.
leo_sobral@1
     1
/**
leo_sobral@1
     2
* GMyth Library
leo_sobral@1
     3
*
leo_sobral@1
     4
* @file gmyth/gmyth_util.c
leo_sobral@1
     5
* 
leo_sobral@1
     6
* @brief <p> This component provides utility functions.
leo_sobral@1
     7
* 
leo_sobral@1
     8
* Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
leo_sobral@1
     9
* @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
leo_sobral@1
    10
*
leo_sobral@1
    11
*//*
leo_sobral@1
    12
* 
leo_sobral@1
    13
* This program is free software; you can redistribute it and/or modify
leo_sobral@1
    14
* it under the terms of the GNU Lesser General Public License as published by
leo_sobral@1
    15
* the Free Software Foundation; either version 2 of the License, or
leo_sobral@1
    16
* (at your option) any later version.
leo_sobral@1
    17
*
leo_sobral@1
    18
* This program is distributed in the hope that it will be useful,
leo_sobral@1
    19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
leo_sobral@1
    20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
leo_sobral@1
    21
* GNU General Public License for more details.
leo_sobral@1
    22
*
leo_sobral@1
    23
* You should have received a copy of the GNU Lesser General Public License
leo_sobral@1
    24
* along with this program; if not, write to the Free Software
leo_sobral@1
    25
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
leo_sobral@1
    26
*/
leo_sobral@213
    27
 
leo_sobral@213
    28
#ifdef HAVE_CONFIG_H
leo_sobral@213
    29
#include "config.h"
leo_sobral@213
    30
#endif
leo_sobral@213
    31
rosfran@214
    32
#define _XOPEN_SOURCE
rosfran@214
    33
#define _XOPEN_SOURCE_EXTENDED
leo_sobral@1
    34
melunko@125
    35
#include <glib.h>
melunko@125
    36
#include <glib/gprintf.h>
rosfran@214
    37
#include <time.h>
rosfran@214
    38
#include <sys/time.h>
melunko@125
    39
rosfran@214
    40
#include "gmyth.h"
melunko@125
    41
rosfran@214
    42
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
leo_sobral@1
    43
leo_sobral@1
    44
/** Converts a time_t struct in a GString at ISO standard format 
leo_sobral@1
    45
 * (e.g. 2006-07-20T09:56:41).
leo_sobral@1
    46
 * 
leo_sobral@1
    47
 * The returned GString memory should be deallocated from 
leo_sobral@1
    48
 * the calling function.
leo_sobral@1
    49
 *
leo_sobral@1
    50
 * @param time_value the time value to be converted
leo_sobral@1
    51
 * @return GString* the converted isoformat string 
leo_sobral@1
    52
 */
leo_sobral@1
    53
GString*
leo_sobral@1
    54
gmyth_util_time_to_isoformat (time_t time_value)
leo_sobral@1
    55
{
leo_sobral@1
    56
	struct tm tm_time;
leo_sobral@1
    57
	GString *result;
leo_sobral@1
    58
	
rosfran@214
    59
	g_static_mutex_lock ( &mutex );
rosfran@214
    60
	
leo_sobral@1
    61
	if (localtime_r(&time_value, &tm_time) == NULL) {
rosfran@214
    62
		g_static_mutex_unlock ( &mutex );
leo_sobral@1
    63
		g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
leo_sobral@1
    64
        return NULL;
leo_sobral@1
    65
	}
leo_sobral@1
    66
	
leo_sobral@1
    67
	result = g_string_sized_new(20);
leo_sobral@1
    68
	g_string_printf(result, "%04d-%02d-%02dT%02d:%02d:%02d",
leo_sobral@1
    69
		tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
leo_sobral@1
    70
		tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
rosfran@214
    71
		
rosfran@214
    72
	gmyth_debug( "Result (ISO 8601) = %s", result->str );
rosfran@214
    73
		
rosfran@214
    74
	g_static_mutex_unlock ( &mutex );
leo_sobral@1
    75
	
leo_sobral@1
    76
	return result;
rosfran@214
    77
	
rosfran@214
    78
	
rosfran@214
    79
}
rosfran@214
    80
rosfran@214
    81
/** Converts a time_t struct in a GString at ISO standard format 
rosfran@214
    82
 * (e.g. 2006-07-20T09:56:41).
rosfran@214
    83
 * 
rosfran@214
    84
 * The returned GString memory should be deallocated from 
rosfran@214
    85
 * the calling function.
rosfran@214
    86
 *
rosfran@214
    87
 * @param time_value the GTimeValue to be converted
rosfran@214
    88
 * @return GString* the converted isoformat string 
rosfran@214
    89
 */
rosfran@214
    90
static gchar*
rosfran@214
    91
gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, GTimeVal* time)
rosfran@214
    92
{
rosfran@214
    93
	
rosfran@214
    94
	gchar *result = g_time_val_to_iso8601( time );
rosfran@214
    95
	gmyth_debug ("GMythUtil: before transcoding, result is: %s!\n", result);
rosfran@214
    96
	
rosfran@214
    97
	struct tm* tm_timeval = g_new0( struct tm, 1 );
rosfran@214
    98
	
rosfran@214
    99
	g_return_val_if_fail( fmt_string != NULL, NULL );
rosfran@214
   100
rosfran@214
   101
  if ( NULL == time ) { 
rosfran@214
   102
		gmyth_debug ("GMythUtil: time_to_isoformat_from_time converter error (timeval == NULL)!\n");
rosfran@214
   103
		return NULL;
rosfran@214
   104
	}
rosfran@214
   105
	
rosfran@214
   106
	g_static_mutex_lock ( &mutex );
rosfran@214
   107
	
rosfran@214
   108
	strptime( result, "%Y-%m-%dT%H:%M:%SZ", tm_timeval ); 
rosfran@214
   109
	
rosfran@214
   110
	//g_date_set_time_val( date, time );
rosfran@214
   111
	
rosfran@214
   112
	//g_date_strftime( result, 21, fmt_string, date );
rosfran@214
   113
	strftime( result, strlen(result), fmt_string, tm_timeval );
rosfran@214
   114
	
rosfran@214
   115
	g_static_mutex_unlock ( &mutex );
rosfran@214
   116
	
rosfran@214
   117
	gmyth_debug( "Result (ISO 8601) = %s", result  );
rosfran@214
   118
	
rosfran@214
   119
	//if ( date != NULL )
rosfran@214
   120
	//	g_date_free( date );
rosfran@214
   121
	
rosfran@214
   122
	return result;	
rosfran@214
   123
		
rosfran@214
   124
}
rosfran@214
   125
rosfran@214
   126
/** Converts a time_t struct in a GString at ISO standard format 
rosfran@214
   127
 * (e.g. 2006-07-20T09:56:41).
rosfran@214
   128
 * 
rosfran@214
   129
 * The returned GString memory should be deallocated from 
rosfran@214
   130
 * the calling function.
rosfran@214
   131
 *
rosfran@214
   132
 * @param time_value the GTimeValue to be converted
rosfran@214
   133
 * @return GString* the converted isoformat string 
rosfran@214
   134
 */
rosfran@214
   135
gchar*
rosfran@214
   136
gmyth_util_time_to_isoformat_from_time_val ( GTimeVal* time )
rosfran@214
   137
{
rosfran@214
   138
	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d %T", time );
rosfran@214
   139
	//result[10] = ' ';
rosfran@214
   140
	//result[ strlen(result) - 1] = '\0';
rosfran@214
   141
	
rosfran@214
   142
	return result;	
rosfran@214
   143
}
rosfran@214
   144
rosfran@214
   145
/** Converts a time_t struct in a GString at ISO standard format 
rosfran@214
   146
 * (e.g. 2006-07-20T09:56:41).
rosfran@214
   147
 * 
rosfran@214
   148
 * The returned GString memory should be deallocated from 
rosfran@214
   149
 * the calling function.
rosfran@214
   150
 *
rosfran@214
   151
 * @param time_value the GTimeValue to be converted
rosfran@214
   152
 * @return GString* the converted isoformat string 
rosfran@214
   153
 */
rosfran@214
   154
gchar*
rosfran@214
   155
gmyth_util_time_to_string_only_date ( GTimeVal* time )
rosfran@214
   156
{
rosfran@214
   157
	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%y-%m-%d", time );
rosfran@214
   158
	result[10] = ' ';
rosfran@214
   159
	result[ strlen(result) - 1] = '\0';
rosfran@214
   160
	return result;
rosfran@214
   161
}
rosfran@214
   162
rosfran@214
   163
/** Converts a time_t struct in a GString at ISO standard format 
rosfran@214
   164
 * (e.g. 2006-07-20T09:56:41).
rosfran@214
   165
 * 
rosfran@214
   166
 * The returned GString memory should be deallocated from 
rosfran@214
   167
 * the calling function.
rosfran@214
   168
 *
rosfran@214
   169
 * @param time_value the GTimeValue to be converted
rosfran@214
   170
 * @return GString* the converted isoformat string 
rosfran@214
   171
 */
rosfran@214
   172
gchar*
rosfran@214
   173
gmyth_util_time_to_string_only_time ( GTimeVal* time )
rosfran@214
   174
{
rosfran@214
   175
	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%T", time );
rosfran@214
   176
	result[10] = ' ';
rosfran@214
   177
	result[ strlen(result) - 1] = '\0';
rosfran@214
   178
	return result;
leo_sobral@1
   179
}
leo_sobral@1
   180
leo_sobral@1
   181
/** Converts a time_t struct in a GString to the following 
leo_sobral@1
   182
 * format (e.g. 2006-07-20 09:56:41).
leo_sobral@1
   183
 * 
leo_sobral@1
   184
 * The returned GString memory should be deallocated from 
leo_sobral@1
   185
 * the calling function.
leo_sobral@1
   186
 *
leo_sobral@1
   187
 * @param time_value the time value to be converted
leo_sobral@1
   188
 * @return GString* the converted string 
leo_sobral@1
   189
 */
leo_sobral@1
   190
GString*
leo_sobral@1
   191
gmyth_util_time_to_string (time_t time_value)
leo_sobral@1
   192
{
leo_sobral@1
   193
	GString *result = gmyth_util_time_to_isoformat (time_value);
leo_sobral@1
   194
	result->str[10] = ' ';	
leo_sobral@1
   195
leo_sobral@1
   196
	return result;
leo_sobral@1
   197
}
leo_sobral@1
   198
rosfran@214
   199
/** Converts a time_t struct in a GString to the following 
rosfran@214
   200
 * format (e.g. 2006-07-20 09:56:41).
rosfran@214
   201
 * 
rosfran@214
   202
 * The returned GString memory should be deallocated from 
rosfran@214
   203
 * the calling function.
rosfran@214
   204
 *
rosfran@214
   205
 * @param time_value the time value to be converted
rosfran@214
   206
 * @return GString* the converted string 
rosfran@214
   207
 */
rosfran@214
   208
gchar*
rosfran@214
   209
gmyth_util_time_to_string_from_time_val (GTimeVal *time_val)
rosfran@214
   210
{
rosfran@214
   211
	gchar *result = gmyth_util_time_to_isoformat_from_time_val (time_val);
rosfran@214
   212
rosfran@214
   213
	return result;
rosfran@214
   214
}
rosfran@214
   215
leo_sobral@1
   216
/** Converts a GString in the following format 
leo_sobral@1
   217
 * (e.g. 2006-07-20 09:56:41) to a time_t struct.
leo_sobral@1
   218
 * 
leo_sobral@1
   219
 * @param time_str the string to be converted
leo_sobral@1
   220
 * @return time_t the time converted value
leo_sobral@1
   221
 */
leo_sobral@1
   222
time_t
leo_sobral@1
   223
gmyth_util_string_to_time (GString* time_str)
leo_sobral@1
   224
{
rosfran@214
   225
	gint year, month, day, hour, min, sec;
rosfran@214
   226
	GDate *date = g_date_new();
leo_sobral@49
   227
    
rosfran@214
   228
	gmyth_debug( "[%s] time_str = %s. [%s]\n", __FUNCTION__, time_str != NULL ? 
rosfran@214
   229
					time_str->str : "[time string is NULL!]", g_strdelimit( time_str->str, " ", 'T' ) );
leo_sobral@1
   230
rosfran@214
   231
rosfran@214
   232
	//if (sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
rosfran@214
   233
	//		&year, &month, &day, &hour, &min, &sec) < 3) {				
rosfran@214
   234
	//g_date_set_parse( date, time_str->str );
rosfran@214
   235
  if ( NULL == date ) { 
leo_sobral@1
   236
		g_warning ("GMythUtil: isoformat_to_time converter error!\n");
leo_sobral@1
   237
		return 0;
rosfran@214
   238
	}
rosfran@214
   239
	
rosfran@214
   240
	g_static_mutex_lock ( &mutex );
rosfran@214
   241
	//GDate *date = g_date_new_dmy( day, month - 1, year - 1900 );
rosfran@214
   242
	
rosfran@214
   243
	struct tm* tm_time = g_malloc0( sizeof(struct tm) );
rosfran@214
   244
/*		
rosfran@183
   245
		tm_time->tm_year = year - 1900;
rosfran@183
   246
		tm_time->tm_mon = month - 1;
rosfran@183
   247
		tm_time->tm_mday = day;
rosfran@183
   248
		tm_time->tm_hour = hour;
rosfran@183
   249
		tm_time->tm_min = min;
rosfran@183
   250
		tm_time->tm_sec = sec;
rosfran@214
   251
	*/
rosfran@214
   252
	
rosfran@214
   253
	GTimeVal *time = g_new0( GTimeVal, 1 ); 
rosfran@214
   254
	
rosfran@214
   255
	if ( !g_time_val_from_iso8601( g_strdelimit( time_str->str, " ", 'T' ), time ) )
rosfran@214
   256
	{
rosfran@214
   257
		gmyth_debug( "ERROR! Problems converting to GTimeVal == %s\n", g_time_val_to_iso8601( time ) );
rosfran@214
   258
	} 
rosfran@214
   259
	
rosfran@214
   260
	gmyth_debug( "Converted to GTimeVal == %s\n", g_time_val_to_iso8601( time ) );
rosfran@214
   261
	
rosfran@214
   262
	g_date_set_time_val( date, time );
rosfran@214
   263
	
rosfran@214
   264
	g_date_to_struct_tm( date, tm_time ); 
rosfran@214
   265
	
rosfran@214
   266
	g_static_mutex_unlock ( &mutex );
rosfran@214
   267
	
rosfran@214
   268
	return mktime( tm_time );
rosfran@214
   269
}
rosfran@214
   270
rosfran@214
   271
/** Converts a GString in the following format 
rosfran@214
   272
 * (e.g. 2006-07-20 09:56:41) to a time_t struct.
rosfran@214
   273
 * 
rosfran@214
   274
 * @param time_str the string to be converted
rosfran@214
   275
 * @return time_t the time converted value
rosfran@214
   276
 */
rosfran@214
   277
GDate *
rosfran@214
   278
gmyth_util_time_val_to_date (GTimeVal* time)
rosfran@214
   279
{
rosfran@214
   280
	GDate *date = g_date_new();
rosfran@214
   281
    
rosfran@214
   282
  if ( NULL == date ) { 
rosfran@214
   283
		g_warning ("GMythUtil: GDate *gmyth_util_time_val_to_date (GTimeVal* time) - converter error!\n");
rosfran@214
   284
		return 0;
leo_sobral@1
   285
	}
rosfran@214
   286
	
rosfran@214
   287
	g_date_set_time_val( date, time );
rosfran@214
   288
	
rosfran@214
   289
	gmyth_debug( "Converted from GTimeVal == %s to GDate\n", g_time_val_to_iso8601( time ) );
rosfran@214
   290
	
rosfran@214
   291
	return date;
rosfran@214
   292
}
rosfran@214
   293
rosfran@214
   294
/** Converts a GString in the following format 
rosfran@214
   295
 * (e.g. 2006-07-20 09:56:41) to a time_t struct.
rosfran@214
   296
 * 
rosfran@214
   297
 * @param time_str the string to be converted
rosfran@214
   298
 * @return time_t the time converted value
rosfran@214
   299
 */
rosfran@214
   300
GTimeVal*
rosfran@214
   301
gmyth_util_string_to_time_val (gchar* time_str)
rosfran@214
   302
{
rosfran@214
   303
	GTimeVal *time = g_new0( GTimeVal, 1 ); 
rosfran@214
   304
    
rosfran@214
   305
	gmyth_debug( "[%s] time_str = %s. [%s]\n", __FUNCTION__, time_str != NULL ? 
rosfran@214
   306
					time_str : "[time string is NULL!]", g_strdelimit( time_str, " ", 'T' ) );
rosfran@214
   307
rosfran@214
   308
  if ( NULL == time ) { 
rosfran@214
   309
		g_warning ("GMythUtil: isoformat_to_time converter error!\n");
rosfran@214
   310
		return NULL;
rosfran@214
   311
	}
rosfran@214
   312
	
rosfran@214
   313
	g_static_mutex_lock ( &mutex );
rosfran@214
   314
	
rosfran@214
   315
	if ( !g_time_val_from_iso8601( g_strdelimit( time_str, " ", 'T' ), time ) )
rosfran@214
   316
	{
rosfran@214
   317
		gmyth_debug( "ERROR! Problems converting to GTimeVal == %s\n", g_strdelimit( time_str, " ", 'T' ) );
rosfran@214
   318
	} 
rosfran@214
   319
	
rosfran@214
   320
	gmyth_debug( "Converted to GTimeVal == %s\n", g_time_val_to_iso8601( time ) );
rosfran@214
   321
	
rosfran@214
   322
	g_static_mutex_unlock ( &mutex );
rosfran@214
   323
	
rosfran@214
   324
	return time;
leo_sobral@1
   325
}
leo_sobral@1
   326
leo_sobral@1
   327
/** Decodes a long long variable from the string list
leo_sobral@1
   328
 * format of the myhtprotocol.
leo_sobral@1
   329
 * 
leo_sobral@1
   330
 * @param strlist the string list of mythprotocol values
leo_sobral@1
   331
 * @param offset  the list node offset of the long long variable
rosfran@35
   332
 * @return gint64  the long long converted value
leo_sobral@1
   333
 */
rosfran@35
   334
gint64
leo_sobral@1
   335
gmyth_util_decode_long_long(GMythStringList *strlist, guint offset) 
leo_sobral@1
   336
{
leo_sobral@1
   337
rosfran@35
   338
	gint64 ret_value = 0LL;
leo_sobral@1
   339
leo_sobral@1
   340
	g_return_val_if_fail( strlist != NULL, ret_value );
leo_sobral@1
   341
rosfran@35
   342
	if ( offset > gmyth_string_list_length( strlist ))
rosfran@35
   343
		g_printerr( "[%s] Offset is greater than the Stringlist (offset = %d)!\n", 
leo_sobral@1
   344
                    __FUNCTION__, offset );
leo_sobral@1
   345
    
leo_sobral@1
   346
	g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
leo_sobral@1
   347
leo_sobral@1
   348
	gint l1 = gmyth_string_list_get_int( strlist, offset );
leo_sobral@1
   349
	gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
leo_sobral@1
   350
rosfran@35
   351
	ret_value = (l2 /*& 0xffffffffLL*/) | ( (gint64)l1 << 32 );
leo_sobral@1
   352
leo_sobral@1
   353
	return ret_value;
leo_sobral@1
   354
leo_sobral@1
   355
}
leo_sobral@1
   356
melunko@125
   357
gboolean
rosfran@214
   358
gmyth_util_file_exists (GMythBackendInfo *backend_info, const gchar* filename)
melunko@125
   359
{
melunko@125
   360
    GMythSocket *socket;
melunko@125
   361
    gboolean res;
melunko@125
   362
    
melunko@125
   363
    socket = gmyth_socket_new ();
renatofilho@147
   364
    res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
melunko@125
   365
		    backend_info->port, TRUE);
rosfran@35
   366
renatofilho@147
   367
    if (res == TRUE) {
renatofilho@147
   368
        GMythStringList *slist;
renatofilho@147
   369
        GMythProgramInfo *program;
melunko@125
   370
renatofilho@147
   371
        program = g_new0 (GMythProgramInfo, 1);
renatofilho@147
   372
        program->pathname = g_string_new (filename);
melunko@125
   373
renatofilho@147
   374
        slist = gmyth_string_list_new ();
renatofilho@147
   375
        gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE");
melunko@125
   376
renatofilho@147
   377
        gmyth_program_info_to_string_list (program, slist);
melunko@125
   378
renatofilho@147
   379
        gmyth_socket_sendreceive_stringlist (socket, slist);
renatofilho@147
   380
renatofilho@147
   381
        res = (gmyth_string_list_get_int (slist, 0) == 1);
melunko@125
   382
    
renatofilho@147
   383
        // fixme: we should do this in a program_info_free() function
renatofilho@147
   384
        g_string_free (program->pathname, TRUE);
renatofilho@147
   385
        g_free (program);
melunko@125
   386
renatofilho@147
   387
        g_object_unref (slist);
melunko@125
   388
renatofilho@147
   389
        gmyth_socket_close_connection (socket);
renatofilho@147
   390
    }
renatofilho@147
   391
    g_object_unref (socket);
melunko@125
   392
    return res;    
melunko@125
   393
}
melunko@125
   394