123
html.cpp
- Committer:
- superphil06
- Date:
- 2015-08-11
- Revision:
- 0:539b0fc9d536
- Child:
- 1:ba608856b208
File content as of revision 0:539b0fc9d536:
/**************************************************************************** * * (C) 2000 by BECK IPC GmbH * * BECK IPC GmbH * Garbenheimerstr. 38 * D-35578 Wetzlar * * Phone : (49)-6441-905-240 * Fax : (49)-6441-905-245 * * --------------------------------------------------------------------------- * Module : HTML.C * Function : contains functions to operate between HTML files and SC12 * Cgi Interface. It read Html files, copy it into memory * block and save positions of '^VARDEF[x]' entry in var_field * struct to insert dynamtic code int the static file. By Example: On the flash exists the following htm file (a:\exp.htm): _______________________________________________________ <HTML><HEAD><TITLE>Example Page</TITLE></HEAD> <BODY> The first Dynamic code/text ^VARDEF[0] = 32 <BR> And now the Dynamic code/text ^VARDEF[1] = 3 <BR> </BODY> </HTML> _______________________________________________________ now you have to call the function in the following way: #include "html.h" vard_field_t exp_vardef[2]; // size "2": 2 var defs in htm file char exp_page[1024]; // html code buffer (1024 bytes) .... .... .. int main(void) { char tmpBuffer[1024]; ... exp_page = Gen_HtmlCode_From_File( "a:\exp.htm", &exp_vardef, 2 ); // know, if exp != Null the page is in the memory block. // the Vardef field are filled with blank spaces (ASCII 32). // The exp_vardef struct points to this vardefs fields. // So you can access to this empty fields in the // following way (normaly the filling of the Vardefs will // be done in the CGI function): // 1. text: strcpy( tmpBuffer, "I am the First text" ); FILL_STR( tmpBuffer, exp_vardef[0].length ); // this will // to fill the not used characters of // the string with blank spaces memcpy( exp_vardef[0].ptr, tmpBufferm, exp_vardef[0].length ); // 2. text: strcpy( tmpBuffer, "I am the First text" ); FILL_STR( tmpBuffer, exp_vardef[0].length ); // this will // to fill the not used characters of // the string with blank spaces memcpy( exp_vardef[0].ptr, tmpBufferm, exp_vardef[0].length ); ..... .. return; } * * Author : Stoidner * Date : 30.08.00 * Version : V1.00 * --------------------------------------------------------------------------- * History : * * Vx.yy Author Changes * * V1.00 cs Create **************************************************************/ //******************* //* standard includes #include "mbed.h" #include <stdio.h> #include <stdlib.h> #include <string.h> //******************** //* specified includes #include "html.h" #include <stdlib.h> #define HTML_DEBUG char *Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) {LocalFileSystem local("local"); FILE *in; // File Handle //fpos_t filepos; // file position counter long filepos; // file position counter int data; // read data from file char value_str[40]; // to search for '^VARDEF[x]' entrys int i=0; // VarDef Counter ( '^VARDEF[i]' ) long mem_pos = -1; // actually mem offset char *html_page; // pointer to memory area of Html Page if ((in = fopen(Path, "rt")) == NULL) { printf("\r\nError Open File: -%s-", Path); return(NULL); } #ifdef HTML_DEBUG printf("\r\nOpen File.. \"%s\"", Path); #endif // checking Html Page size and filling var_field struct while (!feof(in)) { // serach for Commands mem_pos++; // check for unexpected page size if (mem_pos >= 0xFFFF) { fclose(in); return(NULL); } if ( fgetc(in) == CMD_HTML_SIGN ) { //fgetpos(in, &filepos); filepos= ftell ( in );// replace fgetpos call if ( fscanf(in , "VARDEF[ %d ] = %s", &i, &value_str ) ) { #ifdef HTML_DEBUG printf("\r\nFound and save entry.."); #endif mem_pos += atoi(value_str)-1; } else fseek( in, filepos, SEEK_SET ); } } // while (!feof // allocate memory html_page = (char *) malloc( (int) mem_pos+1 ); // check if alloc was successful if (html_page == NULL) return(NULL); // jump to beginning of html file fseek( in, 0, SEEK_SET); // now loading website into memory mem_pos = 0; #ifdef HTML_DEBUG printf("\r\nReading Page"); #endif while (!feof(in)) { data = fgetc(in); // serach for Commands if ( data == CMD_HTML_SIGN ) { // fgetpos(in, &filepos); filepos =ftell ( in );// replace fgetpos call if ( fscanf(in , "VARDEF[ %d ] = %s", &i, &value_str ) ) { if (var_field) { // save VarDef // check if vardef index is valid if ( i>max_vardef ) { free(html_page); return(NULL); }; //var_field[i].length = atoi(value_str); var_field[i].length=strlen(value_str); var_field[i].ptr = &html_page[(int)mem_pos]; } // insert free characters // for (i=0; i<=atoi(value_str)-1; i++) for(i=0;i<=strlen(value_str);i++) { html_page[(int)mem_pos] = (char) 32; mem_pos++; } } else { // set data pointer back fseek( in, (long) filepos, SEEK_SET); // copy data into memory html_page[(int)mem_pos] = (char)data; mem_pos++; } } else { // copy data into memory html_page[(int)mem_pos] = (char)data; mem_pos++; } } // while (!feof // terminate string html_page[(int)mem_pos-1] = (char)'\0'; // close HTML Page fclose(in); #ifdef HTML_DEBUG printf("\r\nDone."); #endif return(html_page); } //simple function thatr load file from disk and return pointer char *load_HtmlCode_From_File( char *Path,long * size) {LocalFileSystem local("local"); FILE *in; // File Handle int data; // read data from file long mem_pos = -1; // actually mem offset char *html_page; // pointer to memory area of Html Page if ((in = fopen(Path, "rt")) == NULL) { printf("\r\nError Open File: -%s-", Path); //open file from disk return(NULL); } while (!feof(in)) // checking Html Page size { mem_pos++; if ( (fgetc(in) == EOF)||(mem_pos==0xFFFF) ) {break;} }// while (!feof ) html_page = (char *) malloc( (int) mem_pos+1 );// allocate memory // check if alloc was successful if (html_page == NULL) return(NULL); fseek( in, 0, SEEK_SET);// jump to beginning of html file mem_pos = 0; // now loading website into memory while (!feof(in)) { data = fgetc(in); html_page[(int)mem_pos] = (char)data; mem_pos++; } // while (!feof html_page[(int)mem_pos-1] = (char)'\0'; // terminate string *size=mem_pos-1; //return size of file fclose(in); // close HTML Page return(html_page); } void Html_Patch ( var_field_t *pTab_Balise,int index, char * pChaine ){ FILL_STR( pChaine,pTab_Balise[index].length ); memcpy( pTab_Balise[index].ptr, pChaine, pTab_Balise[index].length ); }