📄 wpinit.c
字号:
/*=========================================================================
* Filename: WPInit.c
* Function Description: dynamic/static web page initialize and release.
* Author: Gong Zhigang
* Date: 2004/03/10
* Email: zhigang@sunnorth.com.cn
*
*========================================================================*/
/*
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "pollio.h"*/
#include "config.h"
#include "httpd.h"
#include "fs.h"
#include "web.h"
/**********************************************************************
argument:
file: web file point
opt: 0- static initialize
1- dynamic initialize
return: webdata point, include the web page data
**********************************************************************/
struct webdata *Web_Init(struct fs_file *file,u16_t opt)
{
struct webdata *headw=NULL, *web=NULL,*tweb=NULL;
u16_t i,j;
u16_t newpart, attr=STATIC,*payload;
newpart = 1; //the beginning is always static web data.
if (file->data == NULL)
return NULL; //
for(i=0;i<file->len;i++)
{
if (1==newpart)
{
if (0==i) j = -3;
else j = i;
newpart = 0;
tweb = mem_malloc(sizeof (struct webdata));
tweb->payload = file->data + (j+3)/2 ;
tweb->offset = 0;
tweb->attr = attr;
if (headw != NULL)
{
web->next = tweb;
web->len = (j-1) - (web->payload - file->data)*2;
if (web->attr == DYNAMIC)
if (web_head_alloc(headw,web)!=ERR_OK)
return NULL;
//return headw;
if (web->attr == STATIC)
if (web->len%2) //avoid odd bytes
{
web->len--;
}
}
else
{
headw = tweb;
}
web = tweb;
if (opt == 0) { i = file->len; break; } //pure static web page
}
if (getbyte(file->data,i)=='@')
if (getbyte(file->data,i+1) == '@')
{
if (getbyte(file->data,i+2) == '$')
{
attr = STATIC;
newpart = 1;
}
else if(getbyte(file->data,i+2) == '#')
{
attr = DYNAMIC;
newpart = 1;
}
}//@@
}//for
if (headw!=NULL)
{
web->next = NULL;
web->len = i - (web->payload - file->data)*2;
if (web->attr == DYNAMIC) web_head_alloc(headw,web);
}
return headw;
}
/**********************************************************************
argument:
head: web data point is to be free
return: void
**********************************************************************/
void web_free(struct webdata *head)
{
struct webdata *tw, *stw;
if (head!=NULL)
{
for(tw = head, stw = head->next; tw!=NULL; tw = stw, stw = stw->next)
{
if (tw->attr == DYNAMIC)
mem_free(tw->payload);
mem_free(tw);
}
}
}
/**********************************************************************
argument:
headw: web data header point
web: dynamic web data point,which length is web->len(byte)
return: err_t, if there is no ram return ERR_MEM,or return ERR_OK
**********************************************************************/
err_t web_head_alloc(struct webdata *headw, struct webdata *web)
{
u16_t *payload;
payload = mem_malloc((web->len+1)/2);
if (payload == NULL)
{
web_free(headw);
return ERR_MEM;
}
//if
memcpy(payload,web->payload,(web->len+1)/2);
web->payload = payload;
return ERR_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -