Main Page   Class Hierarchy   Compound List   File List   Compound Members  

csvextract.c

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <sys/time.h>
00024 #include <unistd.h>
00025 
00026 #include <limits.h>
00027 #include <math.h>
00028 #include <time.h>
00029 
00030 typedef enum _enum_state {
00031   E_INIT,
00032   E_FIRST_LINE_X,
00033   E_FIRST_LINE_Y,
00034   E_LINE_X,
00035   E_LINE_Y,
00036   E_END
00037 } T_StateFile ;
00038 
00039 void convert_date (long P_sec, char *L_buf) {
00040 
00041   long L_sec, L_min, L_hour, L_day ;
00042   long L_tmp ;
00043 
00044   L_day = P_sec / 86400 ;
00045   L_tmp = P_sec % 86400 ;
00046   L_hour = L_tmp / 3600 ;
00047   L_tmp = L_tmp % 3600 ;
00048   L_min = L_tmp / 60 ;
00049   L_sec = L_tmp % 60 ;
00050   
00051   sprintf(L_buf, "%ld %02ld:%02ld:%02ld", L_day+1, L_hour, L_min, L_sec);
00052 
00053 
00054 }
00055 
00056 T_StateFile next_state(T_StateFile P_state) {
00057 
00058   T_StateFile L_ret ;
00059 
00060   switch (P_state) {
00061   case E_INIT:
00062     L_ret = E_FIRST_LINE_X ;
00063     break ;
00064   case E_FIRST_LINE_X:
00065     L_ret = E_FIRST_LINE_Y ;
00066     break ;
00067   case E_FIRST_LINE_Y:
00068     L_ret = E_LINE_X ;
00069     break ;
00070   case E_LINE_X:
00071     L_ret = E_LINE_Y ;
00072     break ;
00073   case E_LINE_Y:
00074     L_ret = E_LINE_X ;
00075     break ;
00076   case E_END:
00077     L_ret = E_END ;
00078     break ;
00079   }
00080   
00081   return (L_ret);
00082 }
00083 
00084 void make_space_string(char *P_string, size_t P_size) {
00085   size_t L_i ;
00086   for(L_i = 0; L_i < P_size; L_i++) {
00087     P_string[L_i]=' ';
00088   }
00089   P_string[L_i]='\0';
00090 }
00091 
00092 int main(int argc, char**argv) {
00093 
00094   FILE          *L_f, *L_out ;
00095   char          *L_name, L_dest_name[255];
00096   char          *L_extract ;
00097   char           L_char ;
00098   int            L_extract_field ;
00099   long           L_line ;
00100   unsigned long  L_nb_data;
00101   
00102   char           L_field[1024];
00103   int            L_field_size;
00104   fpos_t         L_pos;
00105 
00106   unsigned long  L_max ;
00107   int            L_date_format = 0 ;
00108 
00109   T_StateFile    L_state ;
00110 
00111   L_state = next_state(E_INIT) ;
00112   L_line = 0 ;
00113   L_field_size = 0 ;
00114   L_extract_field = -1 ;
00115   L_nb_data = 0UL ;
00116 
00117   if (argc != 3) {
00118     fprintf(stderr, "Syntax : %s <x|y|a|p> <in csv file>\n", argv[0]);
00119     fprintf(stderr, "         extract csv data into octave ascii format\n");
00120     fprintf(stderr, "         x for first column extraction\n");
00121     fprintf(stderr, "         y for second column extraction\n");
00122     fprintf(stderr, "         a for the two columns extraction\n");
00123     fprintf(stderr, "         p for the two columns extraction (time format for the first)\n");
00124     exit (-1);
00125   }
00126 
00127   L_extract = argv[1] ;
00128   if (strcmp(L_extract, "x")==0) {
00129     L_extract_field = 0x01 ;
00130   } else if (strcmp(L_extract, "y")==0) {
00131     L_extract_field = 0x02 ;
00132   } else if (strcmp(L_extract, "a")==0) {
00133     L_extract_field = 0x03 ;
00134   } else if (strcmp(L_extract, "p")==0) {
00135     L_extract_field = 0x03 ;
00136     L_date_format = 1 ;
00137   }
00138 
00139   if (L_extract_field == -1) {
00140     fprintf(stderr, "Error argument x or y needed\n");
00141     exit (-1);
00142   }
00143 
00144   L_name = argv[2] ;
00145   
00146   L_f = fopen(L_name, "r") ;
00147   if (L_f == NULL) {
00148     fprintf(stderr, "Unable to open %s", L_name);
00149     exit(-1);
00150   }
00151 
00152   strcpy(L_dest_name, L_name);
00153   strcat(L_dest_name, ".");
00154   strcat(L_dest_name, L_extract);
00155   L_out = fopen(L_dest_name, "w") ;
00156   if (L_out == NULL) {
00157     fprintf(stderr, "Unable to open %s", L_dest_name);
00158     exit(-1);
00159   } else {
00160     fprintf(stderr, "[creating file %s]\n", L_dest_name);
00161   }
00162 
00163   if (L_date_format == 0) {
00164     fprintf(L_out, "# name: data_%s\n", L_extract);
00165     fprintf(L_out, "# type: matrix\n");
00166     fprintf(L_out, "# rows: ");
00167     fgetpos(L_out, &L_pos);
00168 
00169     L_max = 0UL ;
00170     L_max = ~L_max ;
00171     sprintf(L_field, "%lu", L_max);
00172     make_space_string(L_field, strlen(L_field));
00173     fprintf(L_out, "%s\n", L_field);
00174 
00175 
00176     if (L_extract_field == 0x03) {
00177       fprintf(L_out, "# columns: 2\n");
00178     } else {
00179       fprintf(L_out, "# columns: 1\n");
00180     }
00181   }
00182     
00183   while (!feof(L_f)) { 
00184     
00185     fscanf(L_f, "%c", &L_char); 
00186 
00187     switch (L_char) { 
00188     case ';': 
00189 
00190       if (L_field_size >= 1024) { 
00191         fprintf(stderr, "max size buffer reached (1024)"); 
00192         exit(-1); 
00193       } 
00194       L_field[L_field_size]='\0'; 
00195 
00196       switch (L_state) { 
00197       case E_LINE_X: 
00198         if (L_extract_field & 0x01) { 
00199 
00200           if (L_date_format == 1) {
00201             // modify L_field here
00202             double L_value ;
00203             long   L_value_long ;
00204             char *L_end = NULL ;
00205             L_value = strtod(L_field, &L_end);
00206             L_value /= 1000.0 ;
00207             L_value_long = (long) (L_value + 0.5) ;
00208             convert_date(L_value_long, L_field);
00209           }
00210           fprintf(L_out, "%s", L_field); 
00211           if (L_extract_field != 0x03) {
00212             fprintf(L_out, "\n");
00213           } else {
00214             fprintf(L_out, " ");
00215           }
00216           L_nb_data++; 
00217         } 
00218         break ; 
00219       case E_LINE_Y: 
00220         if (L_extract_field & 0x02) { 
00221           fprintf(L_out, "%s\n", L_field); 
00222           if (L_extract_field != 0x03) {
00223             L_nb_data++; 
00224           }
00225         } 
00226         break ;
00227       default: 
00228         break ; 
00229       } 
00230 
00231       L_field_size=0; 
00232       L_state = next_state(L_state); 
00233       break ; 
00234 
00235 
00236     case '\n': 
00237       L_line++; 
00238       break ; 
00239 
00240     default: 
00241       if (L_field_size >= 1024) { 
00242         fprintf(stderr, "max size buffer reached (1024)"); 
00243         exit(-1); 
00244       } 
00245       L_field[L_field_size]=L_char; 
00246       L_field_size++; 
00247       break ; 
00248     } 
00249 
00250   } 
00251 
00252   fprintf(stderr, "[data %ld]\n", L_nb_data);
00253 
00254   if (L_date_format == 0) {
00255     fsetpos(L_out, &L_pos); 
00256     fprintf(L_out, "%ld", L_nb_data); 
00257   }
00258 
00259   fclose(L_f);
00260   fclose(L_out);
00261   
00262   return (0);
00263 }

Generated on Wed Mar 7 14:44:59 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002