first versione of ThingSpeak IOT library (not tested)

parse.cpp

Committer:
mbedakhela
Date:
2014-04-04
Revision:
0:92f5363c7790

File content as of revision 0:92f5363c7790:

/* **************************************************************************                                                                                   
 *                                              
 *                                                           
 * 
 *              
 * **************************************************************************
 *  FileName:        parse.c
 *  Dependencies:    
 *  Module:         
 *  Compiler:        
 *
 *  Author               Rev.    Date              Comment
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  Stefano Lai          1.0     03/27/2014        First release 
 *                         
 * 
 *  
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  Software License Agreement
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  This is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License (version 2) as published by 
 *  the Free Software Foundation AND MODIFIED BY OpenPicus team.
 *  
 *  ***NOTE*** The exception to the GPL is included to allow you to distribute
 *  a combined work that includes OpenPicus code without being obliged to 
 *  provide the source code for proprietary components outside of the OpenPicus
 *  code. 
 *  OpenPicus software is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 *  more details. 
 * 
 * 
 * Warranty
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
 *
 **************************************************************************/

#include "mbed.h"
#include "parse.h" 
#include "TCPSocketConnection.h" 
#include <string>
//#include "string.h"
//#include "HWlib.h"

  /*-------------------------------------------------------------------------------------
  | Function:       dynamicPARSE(char *tab[][5], int rows)                              |
  | Description:    Function to parse strings.                                          |
  | Returns:        -                                                                   |
  | Parameters:     char *tab[][5] - pointer to bidimensional array                     |
  |                                                                                     |
  |                                 tab[0][0] => type of parse (use denfine)            |
  |                                             - BETWEEN                               |
  |                                             - BEFORE                                |
  |                                             - AFTER                                 |
  |                                 tab[0][1] => string/token where to start            |
  |                                 tab[0][2] => string/token where to stop             |
  |                                 tab[0][3] => destination string                     |
  |                                 tab[0][4] => source string                          |
  |                                                                                     |
  |                 int rows - number of the array rows                                 |  
  -------------------------------------------------------------------------------------*/

void PARSE::dynamicPARSE(char *tab[][5], int rows)
{
    int i = 0;
        
    for(i=0; i<rows; i++)
    {
        if ( !(strcmp(tab[i][0],"BETWEEN")) )
        {
            PARSEbetween( tab[i][4], tab[i][1], tab[i][2], tab[i][3]);
        }
            
        else if ( !(strcmp(tab[i][0],"BEFORE")) )
        {
            PARSEbefore( tab[i][4], tab[i][2], tab[i][3]);
        }
            
        else //after
        {
            PARSEafter( tab[i][4], tab[i][1], tab[i][3]);
        }
    }
}

  /*-------------------------------------------------------------------------------------------------------
  | Function:       HTTPdynamicPARSE(char host[], char command[], char *tab[][5], int rows, int snifftime)|
  | Description:    Function to parse strings with built-in TCPClient.                                    |
  | Returns:        -                                                                                     |
  | Parameters:     char host[] - IP or Website address to which want to connect                          |
  |                                                                                                       |  
  |                 char command[] - command to be sent through the TCPClient                             |
  |                                                                                                       |  
  |                 char *tab[][5] - pointer to bidimensional array                                       |
  |                                                                                                       |
  |                                 tab[0][0] => type of parse (use denfine)                              |
  |                                             - BETWEEN                                                 |
  |                                             - BEFORE                                                  |
  |                                             - AFTER                                                   |
  |                                 tab[0][1] => string/token where to start                              |
  |                                 tab[0][2] => string/token where to stop                               |
  |                                 tab[0][3] => destination string                                       |
  |                                 tab[0][4] => option destination string (use denfine)                  |
  |                                             - YES -> destination string = buffer                      |
  |                                             - NO  -> destination string = tab[0][3]                   |
  |                                                                                                       |
  |                 int rows - number of the array rows                                                   |
  |                                                                                                       | 
  |                 int snifftime - time in 10 microseconds to accumulate more characters                 |   
  -------------------------------------------------------------------------------------------------------*/

void PARSE::HTTPdynamicPARSE(char host[], char command[], char *tab[][5], int rows, int snifftime)
{
    //TCP_SOCKET Socket;
//    int flag = 0; 
TCPSocketConnection ThingSock;
    int size;
    int i = 0;
        
    //Socket=TCPClientOpen ( host, "80"); 
    //while((Socket==INVALID_SOCKET));
    //while(!TCPisConn(Socket));
            
    //TCPWrite ( Socket, command, strlen(command) );
    //vTaskDelay(snifftime);  
 ///////////////////////////////////   
    ThingSock.connect(host, 80); 
    
    while(!ThingSock.is_connected());
        ThingSock.send_all(command, (int)strlen(command)); 
    
    
    
 /*   
    while( flag==0 )
    {
        
        size = TCPRxLen ( Socket );
        
        if((int)size<=0)
        {
            i++;
            if(i==10)
            break;
        }   
        else
        flag = 1;
    }

    if(flag==1)
    {
*/        
        char buffer[(int)size];
    
        //TCPRead ( Socket, buffer, (int)size ); 
        //vTaskDelay(200);
        //TCPClientClose ( Socket );
        
        ThingSock.receive_all(buffer, (int)size); 
        ThingSock.close();
        
    
        
        
        for(i=0; i<rows; i++)
        {
            if ( !(strcmp(tab[i][0],"BETWEEN")) )
            {
                
                if ( !(strcmp(tab[i][4],"YES")) )
                PARSEbetween( buffer, tab[i][1], tab[i][2], buffer);
                else                
                PARSEbetween( buffer, tab[i][1], tab[i][2], tab[i][3]);
                
            }
            
            else if ( !(strcmp(tab[i][0],"BEFORE")) )
            {
                
                if ( !(strcmp(tab[i][4],"YES")) )
                PARSEbefore( buffer, tab[i][2], buffer);
                else
                PARSEbefore( buffer, tab[i][2], tab[i][3]);
            }
            
            else //after
            {
                
                if ( !(strcmp(tab[i][4],"YES")) )
                PARSEafter( buffer, tab[i][1], buffer);
                else
                PARSEafter( buffer, tab[i][1], tab[i][3]);
                                
            }
        }
//    }
}

  /*-------------------------------------------------------------------------------------------------------
  | Function:       multiPARSEbetween(char str[], char start[], char stop[], char *substr[], int multi)   |
  | Description:    Function to use more PARSEbetween in a string.                                        |
  | Returns:        -                                                                                     |
  | Parameters:     char str[] - source string                                                            |
  |                                                                                                       |  
  |                 char start[] - string/token where to start                                            |
  |                                                                                                       |  
  |                 char stop[] - string/token where to stop                                              |
  |                                                                                                       |
  |                 char *substr[] - pointer to destination string                                        |
  |                                                                                                       | 
  |                 int multi - number of executions of the PARSEbetween function                         |   
  -------------------------------------------------------------------------------------------------------*/

void PARSE::multiPARSEbetween(char str[], char start[], char stop[], char *substr[], int multi)
{
    int i;
    char *pf;
    char *pi;
    int pd;
    int c;
    int j;
    
    int sizestart = strlen(start);
    
    pi=strstr(str,start);
    
    
    for( j=0; j<multi; j++)
    {
        
        pf=strstr(pi+sizestart,stop);
        pd=pf-pi;
        c=pd-sizestart;
    
        for(i=0;i<c;i++)
        {
            *(substr[j]+i)=*(pi+sizestart+i);
        }
        *(substr[j]+i)='\0';
        pi=strstr(pi+sizestart,start);
        
    }
}

  /*-------------------------------------------------------------------------------------------------------
  | Function:       PARSEafter(char str[], char start[], char substr[])                                   |
  | Description:    Function to parse a string and rewrite it from start token to the end.                |
  | Returns:        -                                                                                     |
  | Parameters:     char str[] - source string                                                            |
  |                                                                                                       |  
  |                 char start[] - string/token where to start                                            |
  |                                                                                                       |  
  |                 char substr[] - destination string                                                    |
  -------------------------------------------------------------------------------------------------------*/
  
void PARSE::PARSEafter(char str[], char start[], char substr[])
{
    int i;
    int sizestart;
    int sizestr;
    char *ptrsub;
    char *ptrstr;
    char *pi;
    int c;
    
    ptrsub=substr;
    sizestart=strlen(start);
    sizestr=strlen(str);
    ptrstr=str;
    
    pi=strstr(str,start);
    c=sizestr-(int)pi+(int)ptrstr-sizestart+1;
    
    for(i=0;i<c;i++)
    {
        *(ptrsub+i)=*(pi+sizestart+i);
    }
    *(ptrsub+i)='\0';
}

  /*-------------------------------------------------------------------------------------------------------
  | Function:       PARSEbefore(char str[], char stop[], char substr[])                                   |
  | Description:    Function to parse a string and rewrite it from start to the stop token.               |
  | Returns:        -                                                                                     |
  | Parameters:     char str[] - source string                                                            |
  |                                                                                                       |  
  |                 char stop[] - string/token where to stop                                              |
  |                                                                                                       |  
  |                 char substr[] - destination string                                                    |
  -------------------------------------------------------------------------------------------------------*/
  
void PARSE::PARSEbefore(char str[], char stop[], char substr[])
{
    int i;
    char *pf;
    char *ptrsub;
    char *ptrstr;
    int c;
    
    ptrsub=substr;
    ptrstr=str;
    
    pf=strstr(str,stop);
    c=(int)pf-(int)ptrstr;
    
    
    for(i=0;i<c;i++)
    {
        *(ptrsub+i)=*(ptrstr+i);
    }
    *(ptrsub+i)='\0';
    
}

  /*-------------------------------------------------------------------------------------------------------
  | Function:       PARSEbetween(char str[], char start[], char stop[], char substr[])                    |
  | Description:    Function to parse a string and rewrite it from start token to the stop token.         |
  | Returns:        -                                                                                     |
  | Parameters:     char str[] - source string                                                            |
  |                                                                                                       |
  |                 char start[] - string/token where to start                                            |
  |                                                                                                       |
  |                 char stop[] - string/token where to stop                                              |
  |                                                                                                       |  
  |                 char substr[] - destination string                                                    |
  -------------------------------------------------------------------------------------------------------*/
  
void PARSE::PARSEbetween(char str[], char start[], char stop[], char substr[])
{
    int i;
    int sizestart;
    char *pf;
    char *ptrsub;
    char *pi;
    int pd;
    int c;
    
    ptrsub=substr;
    sizestart=strlen(start);
    
    pi=strstr(str,start);
    pf=strstr(pi+sizestart,stop);

    pd=pf-pi;
    c=pd-sizestart;
    
    for(i=0;i<c;i++)
    {
        *(ptrsub+i)=*(pi+sizestart+i);
    }
    *(ptrsub+i)='\0';

}