/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedflux/run_cpr.c

Go to the documentation of this file.
00001 //---
00002 //
00003 // This file is part of sedflux.
00004 //
00005 // sedflux is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // sedflux is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with sedflux; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //
00019 //---
00020 
00021 #define SED_CPR_PROC_NAME "cpr"
00022 #define EH_LOG_DOMAIN SED_CPR_PROC_NAME
00023 
00024 #include <string.h>
00025 #include <utils/utils.h>
00026 #include <sed/sed_sedflux.h>
00027 #include "my_processes.h"
00028 
00029 gboolean      dump_cpr_data      ( gpointer ptr , FILE *fp );
00030 gpointer      load_cpr_data      ( FILE *fp );
00031 void          eh_dump_file_list  ( Eh_file_list *fl , FILE *fp );
00032 Eh_file_list* eh_load_file_list  ( FILE *fp );
00033 gboolean      init_cpr_data      ( Sed_process proc , Sed_cube prof , GError** error );
00034 
00035 Sed_process_info
00036 run_cpr( Sed_process proc , Sed_cube prof )
00037 {
00038    Cpr_t*           data = sed_process_user_data(proc);
00039    Sed_process_info info = SED_EMPTY_INFO;
00040    gchar* file_name;
00041    FILE*  fp;
00042 
00043    if ( sed_process_run_count(proc)==0 )
00044       init_cpr_data( proc , prof , NULL );
00045 
00046    file_name = eh_get_next_file( data->file_list );
00047 
00048    fp = fopen( file_name , "wb" );
00049 
00050    sed_cube_write( fp , prof );
00051 
00052    fclose(fp);
00053 
00054    return info;
00055 }
00056 
00057 #define S_KEY_DIR       "output directory"
00058 
00059 gboolean
00060 init_cpr( Sed_process p , Eh_symbol_table tab , GError** error )
00061 {
00062    Cpr_t*   data    = sed_process_new_user_data( p , Cpr_t );
00063    GError*  tmp_err = NULL;
00064    gboolean is_ok   = TRUE;
00065 
00066    eh_return_val_if_fail( error==NULL || *error==NULL , FALSE );
00067 
00068    data->file_list  = NULL;
00069 
00070    data->output_dir = eh_symbol_table_value( tab , S_KEY_DIR );
00071 
00072    if ( !tmp_err ) try_dir( data->output_dir , &tmp_err );
00073 
00074    if ( tmp_err )
00075    {
00076       g_propagate_error( error , tmp_err );
00077       is_ok = FALSE;
00078    }
00079 
00080    return is_ok;
00081 }
00082 
00083 gboolean
00084 init_cpr_data( Sed_process proc , Sed_cube prof , GError** error )
00085 {
00086    Cpr_t* data = sed_process_user_data( proc );
00087 
00088    if ( data )
00089    {
00090       gchar* cube_name = sed_cube_name( prof );
00091       gchar* base_name = g_strconcat( data->output_dir  ,
00092                                       G_DIR_SEPARATOR_S ,
00093                                       cube_name         ,
00094                                       "#"               ,
00095                                       ".cpr" , NULL );
00096 
00097       data->file_list = eh_create_file_list( base_name );
00098 
00099       eh_free( base_name );
00100       eh_free( cube_name );
00101    }
00102    return TRUE;
00103 }
00104 
00105 gboolean
00106 destroy_cpr( Sed_process p )
00107 {
00108    if ( p )
00109    {
00110       Cpr_t* data = sed_process_user_data( p );
00111 
00112       if ( data )
00113       {
00114          eh_destroy_file_list( data->file_list );
00115 
00116          eh_free( data->output_dir );
00117          data->output_dir = NULL;
00118          eh_free( data );
00119       }
00120    }
00121    return TRUE;
00122 }
00123 
00124 gboolean dump_cpr_data( gpointer ptr , FILE *fp )
00125 {
00126    Cpr_t *data=(Cpr_t*)ptr;
00127    gint len;
00128 
00129    eh_require( ptr!=NULL );
00130    eh_require( fp!=NULL  );
00131 
00132    len = strlen( data->output_dir )+1;
00133 
00134    eh_dump_file_list( data->file_list , fp );
00135    fwrite( &len                 , sizeof(gint)      , 1   , fp );
00136    fwrite( data->output_dir     , sizeof(char)      , len , fp );
00137 
00138    return TRUE;
00139 }
00140 
00141 gpointer load_cpr_data( FILE *fp )
00142 {
00143    Cpr_t *data;
00144    gint len;
00145 
00146    eh_require( fp!=NULL );
00147 
00148    data = eh_new( Cpr_t , 1 );
00149 
00150    data->file_list = eh_load_file_list( fp );
00151    fread( &len                 , sizeof(gint) , 1   , fp );
00152    fread( data->output_dir     , sizeof(char) , len , fp );
00153 
00154    return (gpointer)data;
00155 }
00156 
00157 void eh_dump_file_list( Eh_file_list *fl , FILE *fp )
00158 {
00159    int len;
00160 
00161    len = strlen( fl->prefix );
00162    fwrite( &len         , sizeof(int)  , 1   , fp );
00163    fwrite( fl->prefix   , sizeof(char) , len , fp );
00164 
00165    len = strlen( fl->suffix );
00166    fwrite( &len         , sizeof(int)  , 1   , fp );
00167    fwrite( fl->suffix   , sizeof(char) , len , fp );
00168 
00169    len = strlen( fl->format );
00170    fwrite( &len         , sizeof(int)  , 1   , fp );
00171    fwrite( fl->suffix   , sizeof(char) , len , fp );
00172 
00173    fwrite( &(fl->count) , sizeof(int)  , 1   , fp );
00174 
00175 }
00176 
00177 Eh_file_list *eh_load_file_list( FILE *fp )
00178 {
00179    int len;
00180    Eh_file_list *fl = eh_new( Eh_file_list , 1 );
00181 
00182    fread( &len , sizeof(int) , 1 , fp );
00183    fl->prefix = eh_new( char , len );
00184    fread( fl->prefix   , sizeof(char) , len , fp );
00185 
00186    fread( &len , sizeof(int) , 1 , fp );
00187    fl->suffix = eh_new( char , len );
00188    fread( fl->suffix   , sizeof(char) , len , fp );
00189 
00190    fread( &len , sizeof(int) , 1 , fp );
00191    fl->format = eh_new( char , len );
00192    fread( fl->format   , sizeof(char) , len , fp );
00193 
00194    fread( &(fl->count) , sizeof(int)  , 1   , fp );
00195 
00196    return fl;
00197 }
00198 

Generated on Fri Jan 4 18:04:16 2008 for sedflux by  doxygen 1.5.2