Stefano Lai / ThingSpeak
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers parse.cpp Source File

parse.cpp

00001 /* **************************************************************************                                                                                   
00002  *                                              
00003  *                                                           
00004  * 
00005  *              
00006  * **************************************************************************
00007  *  FileName:        parse.c
00008  *  Dependencies:    
00009  *  Module:         
00010  *  Compiler:        
00011  *
00012  *  Author               Rev.    Date              Comment
00013  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00014  *  Stefano Lai          1.0     03/27/2014        First release 
00015  *                         
00016  * 
00017  *  
00018  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00019  *
00020  *  Software License Agreement
00021  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00022  *  This is free software; you can redistribute it and/or modify it under
00023  *  the terms of the GNU General Public License (version 2) as published by 
00024  *  the Free Software Foundation AND MODIFIED BY OpenPicus team.
00025  *  
00026  *  ***NOTE*** The exception to the GPL is included to allow you to distribute
00027  *  a combined work that includes OpenPicus code without being obliged to 
00028  *  provide the source code for proprietary components outside of the OpenPicus
00029  *  code. 
00030  *  OpenPicus software is distributed in the hope that it will be useful, but 
00031  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00032  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00033  *  more details. 
00034  * 
00035  * 
00036  * Warranty
00037  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00038  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
00039  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
00040  * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
00041  * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
00042  * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
00043  * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
00044  * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
00045  * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
00046  * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
00047  * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
00048  * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
00049  *
00050  **************************************************************************/
00051 
00052 #include "mbed.h"
00053 #include "parse.h" 
00054 #include "TCPSocketConnection.h" 
00055 #include <string>
00056 //#include "string.h"
00057 //#include "HWlib.h"
00058 
00059   /*-------------------------------------------------------------------------------------
00060   | Function:       dynamicPARSE(char *tab[][5], int rows)                              |
00061   | Description:    Function to parse strings.                                          |
00062   | Returns:        -                                                                   |
00063   | Parameters:     char *tab[][5] - pointer to bidimensional array                     |
00064   |                                                                                     |
00065   |                                 tab[0][0] => type of parse (use denfine)            |
00066   |                                             - BETWEEN                               |
00067   |                                             - BEFORE                                |
00068   |                                             - AFTER                                 |
00069   |                                 tab[0][1] => string/token where to start            |
00070   |                                 tab[0][2] => string/token where to stop             |
00071   |                                 tab[0][3] => destination string                     |
00072   |                                 tab[0][4] => source string                          |
00073   |                                                                                     |
00074   |                 int rows - number of the array rows                                 |  
00075   -------------------------------------------------------------------------------------*/
00076 
00077 void PARSE::dynamicPARSE(char *tab[][5], int rows)
00078 {
00079     int i = 0;
00080         
00081     for(i=0; i<rows; i++)
00082     {
00083         if ( !(strcmp(tab[i][0],"BETWEEN")) )
00084         {
00085             PARSEbetween( tab[i][4], tab[i][1], tab[i][2], tab[i][3]);
00086         }
00087             
00088         else if ( !(strcmp(tab[i][0],"BEFORE")) )
00089         {
00090             PARSEbefore( tab[i][4], tab[i][2], tab[i][3]);
00091         }
00092             
00093         else //after
00094         {
00095             PARSEafter( tab[i][4], tab[i][1], tab[i][3]);
00096         }
00097     }
00098 }
00099 
00100   /*-------------------------------------------------------------------------------------------------------
00101   | Function:       HTTPdynamicPARSE(char host[], char command[], char *tab[][5], int rows, int snifftime)|
00102   | Description:    Function to parse strings with built-in TCPClient.                                    |
00103   | Returns:        -                                                                                     |
00104   | Parameters:     char host[] - IP or Website address to which want to connect                          |
00105   |                                                                                                       |  
00106   |                 char command[] - command to be sent through the TCPClient                             |
00107   |                                                                                                       |  
00108   |                 char *tab[][5] - pointer to bidimensional array                                       |
00109   |                                                                                                       |
00110   |                                 tab[0][0] => type of parse (use denfine)                              |
00111   |                                             - BETWEEN                                                 |
00112   |                                             - BEFORE                                                  |
00113   |                                             - AFTER                                                   |
00114   |                                 tab[0][1] => string/token where to start                              |
00115   |                                 tab[0][2] => string/token where to stop                               |
00116   |                                 tab[0][3] => destination string                                       |
00117   |                                 tab[0][4] => option destination string (use denfine)                  |
00118   |                                             - YES -> destination string = buffer                      |
00119   |                                             - NO  -> destination string = tab[0][3]                   |
00120   |                                                                                                       |
00121   |                 int rows - number of the array rows                                                   |
00122   |                                                                                                       | 
00123   |                 int snifftime - time in 10 microseconds to accumulate more characters                 |   
00124   -------------------------------------------------------------------------------------------------------*/
00125 
00126 void PARSE::HTTPdynamicPARSE(char host[], char command[], char *tab[][5], int rows, int snifftime)
00127 {
00128     //TCP_SOCKET Socket;
00129 //    int flag = 0; 
00130 TCPSocketConnection ThingSock;
00131     int size;
00132     int i = 0;
00133         
00134     //Socket=TCPClientOpen ( host, "80"); 
00135     //while((Socket==INVALID_SOCKET));
00136     //while(!TCPisConn(Socket));
00137             
00138     //TCPWrite ( Socket, command, strlen(command) );
00139     //vTaskDelay(snifftime);  
00140  ///////////////////////////////////   
00141     ThingSock.connect(host, 80); 
00142     
00143     while(!ThingSock.is_connected());
00144         ThingSock.send_all(command, (int)strlen(command)); 
00145     
00146     
00147     
00148  /*   
00149     while( flag==0 )
00150     {
00151         
00152         size = TCPRxLen ( Socket );
00153         
00154         if((int)size<=0)
00155         {
00156             i++;
00157             if(i==10)
00158             break;
00159         }   
00160         else
00161         flag = 1;
00162     }
00163 
00164     if(flag==1)
00165     {
00166 */        
00167         char buffer[(int)size];
00168     
00169         //TCPRead ( Socket, buffer, (int)size ); 
00170         //vTaskDelay(200);
00171         //TCPClientClose ( Socket );
00172         
00173         ThingSock.receive_all(buffer, (int)size); 
00174         ThingSock.close();
00175         
00176     
00177         
00178         
00179         for(i=0; i<rows; i++)
00180         {
00181             if ( !(strcmp(tab[i][0],"BETWEEN")) )
00182             {
00183                 
00184                 if ( !(strcmp(tab[i][4],"YES")) )
00185                 PARSEbetween( buffer, tab[i][1], tab[i][2], buffer);
00186                 else                
00187                 PARSEbetween( buffer, tab[i][1], tab[i][2], tab[i][3]);
00188                 
00189             }
00190             
00191             else if ( !(strcmp(tab[i][0],"BEFORE")) )
00192             {
00193                 
00194                 if ( !(strcmp(tab[i][4],"YES")) )
00195                 PARSEbefore( buffer, tab[i][2], buffer);
00196                 else
00197                 PARSEbefore( buffer, tab[i][2], tab[i][3]);
00198             }
00199             
00200             else //after
00201             {
00202                 
00203                 if ( !(strcmp(tab[i][4],"YES")) )
00204                 PARSEafter( buffer, tab[i][1], buffer);
00205                 else
00206                 PARSEafter( buffer, tab[i][1], tab[i][3]);
00207                                 
00208             }
00209         }
00210 //    }
00211 }
00212 
00213   /*-------------------------------------------------------------------------------------------------------
00214   | Function:       multiPARSEbetween(char str[], char start[], char stop[], char *substr[], int multi)   |
00215   | Description:    Function to use more PARSEbetween in a string.                                        |
00216   | Returns:        -                                                                                     |
00217   | Parameters:     char str[] - source string                                                            |
00218   |                                                                                                       |  
00219   |                 char start[] - string/token where to start                                            |
00220   |                                                                                                       |  
00221   |                 char stop[] - string/token where to stop                                              |
00222   |                                                                                                       |
00223   |                 char *substr[] - pointer to destination string                                        |
00224   |                                                                                                       | 
00225   |                 int multi - number of executions of the PARSEbetween function                         |   
00226   -------------------------------------------------------------------------------------------------------*/
00227 
00228 void PARSE::multiPARSEbetween(char str[], char start[], char stop[], char *substr[], int multi)
00229 {
00230     int i;
00231     char *pf;
00232     char *pi;
00233     int pd;
00234     int c;
00235     int j;
00236     
00237     int sizestart = strlen(start);
00238     
00239     pi=strstr(str,start);
00240     
00241     
00242     for( j=0; j<multi; j++)
00243     {
00244         
00245         pf=strstr(pi+sizestart,stop);
00246         pd=pf-pi;
00247         c=pd-sizestart;
00248     
00249         for(i=0;i<c;i++)
00250         {
00251             *(substr[j]+i)=*(pi+sizestart+i);
00252         }
00253         *(substr[j]+i)='\0';
00254         pi=strstr(pi+sizestart,start);
00255         
00256     }
00257 }
00258 
00259   /*-------------------------------------------------------------------------------------------------------
00260   | Function:       PARSEafter(char str[], char start[], char substr[])                                   |
00261   | Description:    Function to parse a string and rewrite it from start token to the end.                |
00262   | Returns:        -                                                                                     |
00263   | Parameters:     char str[] - source string                                                            |
00264   |                                                                                                       |  
00265   |                 char start[] - string/token where to start                                            |
00266   |                                                                                                       |  
00267   |                 char substr[] - destination string                                                    |
00268   -------------------------------------------------------------------------------------------------------*/
00269   
00270 void PARSE::PARSEafter(char str[], char start[], char substr[])
00271 {
00272     int i;
00273     int sizestart;
00274     int sizestr;
00275     char *ptrsub;
00276     char *ptrstr;
00277     char *pi;
00278     int c;
00279     
00280     ptrsub=substr;
00281     sizestart=strlen(start);
00282     sizestr=strlen(str);
00283     ptrstr=str;
00284     
00285     pi=strstr(str,start);
00286     c=sizestr-(int)pi+(int)ptrstr-sizestart+1;
00287     
00288     for(i=0;i<c;i++)
00289     {
00290         *(ptrsub+i)=*(pi+sizestart+i);
00291     }
00292     *(ptrsub+i)='\0';
00293 }
00294 
00295   /*-------------------------------------------------------------------------------------------------------
00296   | Function:       PARSEbefore(char str[], char stop[], char substr[])                                   |
00297   | Description:    Function to parse a string and rewrite it from start to the stop token.               |
00298   | Returns:        -                                                                                     |
00299   | Parameters:     char str[] - source string                                                            |
00300   |                                                                                                       |  
00301   |                 char stop[] - string/token where to stop                                              |
00302   |                                                                                                       |  
00303   |                 char substr[] - destination string                                                    |
00304   -------------------------------------------------------------------------------------------------------*/
00305   
00306 void PARSE::PARSEbefore(char str[], char stop[], char substr[])
00307 {
00308     int i;
00309     char *pf;
00310     char *ptrsub;
00311     char *ptrstr;
00312     int c;
00313     
00314     ptrsub=substr;
00315     ptrstr=str;
00316     
00317     pf=strstr(str,stop);
00318     c=(int)pf-(int)ptrstr;
00319     
00320     
00321     for(i=0;i<c;i++)
00322     {
00323         *(ptrsub+i)=*(ptrstr+i);
00324     }
00325     *(ptrsub+i)='\0';
00326     
00327 }
00328 
00329   /*-------------------------------------------------------------------------------------------------------
00330   | Function:       PARSEbetween(char str[], char start[], char stop[], char substr[])                    |
00331   | Description:    Function to parse a string and rewrite it from start token to the stop token.         |
00332   | Returns:        -                                                                                     |
00333   | Parameters:     char str[] - source string                                                            |
00334   |                                                                                                       |
00335   |                 char start[] - string/token where to start                                            |
00336   |                                                                                                       |
00337   |                 char stop[] - string/token where to stop                                              |
00338   |                                                                                                       |  
00339   |                 char substr[] - destination string                                                    |
00340   -------------------------------------------------------------------------------------------------------*/
00341   
00342 void PARSE::PARSEbetween(char str[], char start[], char stop[], char substr[])
00343 {
00344     int i;
00345     int sizestart;
00346     char *pf;
00347     char *ptrsub;
00348     char *pi;
00349     int pd;
00350     int c;
00351     
00352     ptrsub=substr;
00353     sizestart=strlen(start);
00354     
00355     pi=strstr(str,start);
00356     pf=strstr(pi+sizestart,stop);
00357 
00358     pd=pf-pi;
00359     c=pd-sizestart;
00360     
00361     for(i=0;i<c;i++)
00362     {
00363         *(ptrsub+i)=*(pi+sizestart+i);
00364     }
00365     *(ptrsub+i)='\0';
00366 
00367 }