Main Page   Class Hierarchy   Compound List   File List   Compound Members  

csvsplit.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 typedef enum _enum_state {
00027   E_INIT,
00028   E_LINE_X,
00029   E_LINE_Y,
00030   E_END
00031 } T_StateFile ;
00032 
00033 T_StateFile next_state(T_StateFile P_state) {
00034 
00035   T_StateFile L_ret ;
00036 
00037   switch (P_state) {
00038   case E_INIT:
00039     L_ret = E_LINE_X ;
00040     break ;
00041   case E_LINE_X:
00042     L_ret = E_LINE_Y ;
00043     break ;
00044   case E_LINE_Y:
00045     L_ret = E_LINE_X ;
00046     break ;
00047   case E_END:
00048     L_ret = E_END ;
00049     break ;
00050   }
00051   
00052   return (L_ret);
00053 }
00054 
00055 void make_space_string(char *P_string, size_t P_size) {
00056   size_t L_i ;
00057   for(L_i = 0; L_i < P_size; L_i++) {
00058     P_string[L_i]=' ';
00059   }
00060   P_string[L_i]='\0';
00061 }
00062 
00063 int main(int argc, char**argv) {
00064 
00065   FILE          *L_in, *L_out ;
00066   long           L_line ;
00067   unsigned long  L_nb_data;
00068   
00069   char           L_field[1024];
00070   int            L_field_size;
00071   
00072   char           L_char ;
00073 
00074 
00075   int            L_nb_args = argc       ;
00076   char*          L_file_tab[2]          ;
00077   char          *L_in_file, *L_out_file ;
00078   int            L_nb_file = 0          ;
00079   char*          L_arg_value            ;
00080 
00081   int            L_skip = 0             ;
00082   int            L_ratio = 10           ;
00083   int            L_select = 0           ;
00084 
00085   T_StateFile    L_state ;
00086 
00087   L_state = next_state(E_INIT) ;
00088   L_line = 0 ;
00089   L_field_size = 0 ;
00090   L_nb_data = 0UL ;
00091 
00092   if (argc < 3) {
00093     fprintf(stderr, "Syntax : %s <in csv file> <out csv file>\n", argv[0]);
00094     fprintf(stderr, "         [-skip n] skip the n first values (default 0)\n");
00095     fprintf(stderr, "         [-ratio r] let 1 out of r value (default 10)\n");
00096     exit (-1);
00097   }
00098 
00099   L_nb_args--;
00100   while (L_nb_args) {
00101     L_arg_value = argv[argc-L_nb_args];
00102     if (strcmp(L_arg_value, "-skip") == 0) {
00103       if (L_nb_args) {
00104         L_nb_args--;
00105         L_arg_value=argv[argc-L_nb_args];
00106         L_skip = atoi(L_arg_value);
00107       } else {
00108         fprintf(stderr, "argument expected for -skip\n");
00109         exit (-1);
00110       }
00111     } else if (strcmp(L_arg_value, "-ratio") == 0) {
00112       if (L_nb_args) {
00113         L_nb_args--;
00114         L_arg_value=argv[argc-L_nb_args];
00115         L_ratio = atoi(L_arg_value);
00116       } else {
00117         fprintf(stderr, "argument expected for -ratio\n");
00118         exit (-1);
00119       }
00120     } else {
00121       if (L_nb_file < 2) {
00122         L_file_tab[L_nb_file] = L_arg_value ;
00123         L_nb_file++;
00124       } else {
00125         fprintf(stderr, "syntax error %s unexpected\n", L_arg_value);
00126         exit (-1);
00127       }
00128     }
00129     L_nb_args-- ;
00130   }
00131 
00132   if (L_nb_file != 2) {
00133     fprintf(stderr, "error on file in/out arguments\n");
00134     exit (-1);
00135   } else {
00136     L_in_file = L_file_tab[0];
00137     L_out_file = L_file_tab[1] ;
00138   }
00139 
00140   fprintf(stderr, "[in: %s; out:%s; skip:%d; ratio:%d]\n", 
00141           L_file_tab[0], L_file_tab[1], L_skip, L_ratio);
00142 
00143   L_in = fopen(L_in_file, "r") ; 
00144   if (L_in == NULL) { 
00145     fprintf(stderr, "Unable to open %s\n", L_in_file); 
00146     exit(-1); 
00147   } 
00148 
00149   L_out = fopen(L_out_file, "w") ; 
00150   if (L_out == NULL) { 
00151     fprintf(stderr, "Unable to open %s\n", L_out_file); 
00152     exit(-1); 
00153   } 
00154 
00155   while (!feof(L_in)) {
00156 
00157     L_char = fgetc(L_in) ;
00158 
00159     switch(L_char) {
00160     case ';':
00161       if (L_field_size >= 1024) {
00162         fprintf(stderr, "Maximum field size (1024) reached\n");
00163         exit (-1);
00164       }
00165       L_field[L_field_size]=L_char;
00166       L_field_size++;
00167       switch (L_state) {
00168       case E_LINE_X:
00169         break;
00170       case E_LINE_Y:
00171         if (L_field_size >= 1024) {
00172           fprintf(stderr, "Maximum field size (1024) reached\n");
00173           exit (-1);
00174         }
00175         L_field[L_field_size]=0;
00176         if (L_line == 0){
00177           fprintf(L_out,"%s\n",L_field);
00178           L_field_size=0;
00179           break;
00180         }         
00181         L_nb_data++;
00182         if (L_nb_data > L_skip) {
00183           if (L_select == 0) {
00184             fprintf(L_out,"%s\n",L_field);
00185             L_select = L_ratio - 1;
00186           } else {
00187             L_select -- ;
00188           }
00189         }
00190         L_field_size=0;
00191         break;
00192       default:
00193         break;
00194       }
00195       L_state = next_state(L_state);
00196       break ;
00197     case '\n':
00198       L_line++;
00199       break ;
00200     default:
00201       if (L_field_size >= 1024) {
00202         fprintf(stderr, "max buffer size (1024) reached");
00203         exit (-1);
00204       }
00205       L_field[L_field_size]=L_char;
00206       L_field_size++;
00207       break ;
00208     }
00209 
00210   }
00211 
00212   fclose(L_in) ;
00213   fclose(L_out) ;
00214 
00215   return (0);
00216 }

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