Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1514608
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: C/C++

2012-12-18 08:36:05

-->的意思, 左边是指针变量
.的意思是, 左边是非指针变量
static struct http_request *req;
req->method = UH_HTTP_MSG_POST;

static struct http_request req;
req.method = UH_HTTP_MSG_POST;

//---结构和结构指针
typedef struct {
  U32   fsize;                          /* FAT File Size                     */
  U32   fpos;                           /* FAT File Position Indicator       */
} IOB1;

typedef struct iob {
  U16   fileID;                         /* File Identification Number        */
  U16   flags;                          /* File status flags                 */
  U8    drive;                          /* Device Drive Media type           */
  U8    attrib;                         /* Attribute Flags                   */
  U16   NumSect;                        /* Number of sectors                 */
  U32   InitVal;                        /* Initial content of Flash/RAM      */
  void *DevCfg;                         /* Device configuration              */
  U16   _fblock;                        /* Current Flash Block index         */
  U16   _fidx;                          /* Current File Block index          */
  U32   _fbot;                          /* Flash Block free space bottom     */
  U32   _ftop;                          /* Flash Block free space top        */
  U32   _firstClus;                     /* FAT First Data Cluster            */
  U32   _lastEntClus;                   /* FAT Entry (last) Cluster          */
  U8    _currDatSect;                   /* FAT Curr Data Sect Offs in Cluster*/
  U32   _currDatClus;                   /* FAT Current Data Cluster          */
  U32   fsize;                          /* FAT File Size                     */
  U32   fpos;                           /* FAT File Position Indicator       */
} IOB;

typedef struct falloc {
  U32 end;
  U16 fileID;
  U16 index;
} FALLOC;

//---结构和结构指针的应用
FALLOC fa;
IOB *fcb;
if (fa.fileID == fcb->fileID  &&  fa.index == fidx) {
fcb->_ftop = fa.end;

//---单独1个指针变量也作为结构.
typedef struct {
  I16 * aY;
} PARAM1;

PARAM1 * pParam = (PARAM1 *)p;
GUI_DrawGraph(pParam->aY, NumPoints, x0, y0);
GUI_DrawGraph(pParam->aY+15, NumPoints, x0, y0);

PARAM1 Param;
Param.aY = (I16*)GUI_ALLOC_h2p(hMem);

//--- 结构内再应用本结构. LWIP的 pbuf.h
struct pbuf {
  struct pbuf *next;//next pbuf in singly linked pbuf chain
  void *payload;    //pointer to the actual data in the buffer
  u16_t tot_len; //p->tot_len == p->len + (p->next? p->next->tot_len: 0)
  u16_t len;   //length of this buffer
  u8_t /*pbuf_type*/ type; //pbuf_type as u8_t instead of enum to save space
  u8_t flags;   //misc flags
};

//---链表的应用, 参考xucommon/list.h
struct list_head {
struct list_head *next, *prev;
};
static __inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
static __inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
static __inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
static __inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}

//--- 结构指针和结构变量的赋值
typedef struct {                        /* RL Time format (FFS, TCPnet)      */
  U8  hr;                               /* Hours    [0..23]                  */
  U8  min;                              /* Minutes  [0..59]                  */
  U8  sec;                              /* Seconds  [0..59]                  */
  U8  day;                              /* Day      [1..31]                  */
  U8  mon;                              /* Month    [1..12]                  */
  U16 year;                             /* Year     [1980..2107]             */
} RL_TIME;

typedef struct {                        /* Search info record                */
  S8  name[256];                        /* Name                              */
  U32 size;                             /* File size in bytes                */
  U16 fileID;                           /* System Identification             */
  U8  attrib;                           /* Attributes                        */
  RL_TIME time;                         /* Create/Modify Time                */
} FINFO;

static void set_time_date (FINFO *info, FILEREC *frec) {
  /* Convert date/time info and write to 'info' structure. */
  U32 v;

  v = get_u16 ((U8 *)&frec->WriteTime);
  info->time.hr  = (v >> 11);
  info->time.min = (v >> 5) & 0x3F;
  info->time.sec = (v & 0x1F) << 1;
  v = get_u16 ((U8 *)&frec->WriteDate);
  info->time.day = (v & 0x1F);
  info->time.mon = (v >> 5) & 0x0F;
  info->time.year= ((v >> 9) & 0x7F) + 1980;
}

指针转换
SDK_Node_Real_Data RealData;
memset(&RealData, 0, sizeof(RealData));

//若此为阻塞, 则停在此处而无法判断是否停止
//int ret = recvfrom(PrimaryUDP, pcStr, MAXLEN, 0, (sockaddr *)&sender, &dwSender);
int ret = recvfrom(PrimaryUDP, (char *)&RealData, sizeof(RealData), 0, (sockaddr *)&sender, &dwSender);

typedef int (CALLBACK *fRealDataCallback)(unsigned long handle, int seq, const SDK_Node_Real_Data *data);
static fRealDataCallback g_fRealDataCallback = NULL;
if (g_fRealDataCallback) {
g_fRealDataCallback(handle, seq, (const SDK_Node_Real_Data *)&RealData);
}

//--- 多层结构指针应用, mini-xml. f:\linux-xu\1-wire\mxml
typedef struct mxml_attr_s /**** An XML element attribute value. @private@ ****/
{
  char *name; /* Attribute name */
  char *value; /* Attribute value */
} mxml_attr_t;

typedef struct mxml_element_s /**** An XML element value. @private@ ****/
{
  char *name; /* Name of element */
  int num_attrs; /* Number of attributes */
  mxml_attr_t *attrs; /* Attributes */
} mxml_element_t;

typedef union mxml_value_u /**** An XML node value. @private@ ****/
{
  mxml_element_t element; /* Element */
} mxml_value_t;

struct mxml_node_s /**** An XML node. @private@ ****/
{
  mxml_value_t value; /* Node value */
  struct mxml_node_s *next; /* Next node under same parent */
  struct mxml_node_s *prev; /* Previous node under same parent */
};

//调用例程, 第一层为指针用 ->, 其余层无论是否指针用 .
mxml_node_t *state,*url;

state->value.element.num_attrs
state->value.element.attrs[i].name
state->value.element.attrs[i].value

//-------------- 良好的代码可扩展性实例 --------------------------------------------------
typedef struct {
    const char *name;
    u8_t shtml;
} default_filename;

const default_filename g_psDefaultFilenames[] = {
  {"/index.shtml", true },
  {"/index.ssi", true },
  {"/index.shtm", true },
  {"/index.html", false },
  {"/index.htm", false }
};
#define NUM_DEFAULT_FILENAMES (sizeof(g_psDefaultFilenames) / sizeof(default_filename))

for(loop = 0; loop < NUM_DEFAULT_FILENAMES; loop++) {
            uri = (char *)g_psDefaultFilenames[loop].name;
            file = fs_open((char *)g_psDefaultFilenames[loop].name);
            if(file != NULL) {
              DEBUG_PRINT("Opened %s\n", uri);
#ifdef INCLUDE_HTTPD_SSI
              hs->tag_check = g_psDefaultFilenames[loop].shtml;
#endif
              break;
            }
        }

tWidget *g_psScreens[] = {
    (tWidget *)&g_sHomeScreen,
    (tWidget *)&g_sIOScreen,
    (tWidget *)&g_sDemoScreen,
    (tWidget *)&g_sImageScreen,
    (tWidget *)&g_sAudioScreen,
};
#define NUM_SCREENS (sizeof(g_psScreens) / sizeof(tWidget *))

//--- 应用结构以获得对应的参数
#ifdef DYNAMIC_HTTP_HEADERS
typedef struct {
    const char *pszExtension;
    unsigned long ulHeaderIndex;
} tHTTPHeader;

const char *g_psHTTPHeaderStrings[] = {
 "Content-type: text/html\r\n\r\n",
 "Content-type: text/html\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\n"    \
   "Pragma: no-cache\r\n\r\n",
 "Content-type: image/gif\r\n\r\n",
 "Content-type: image/png\r\n\r\n",
 "Content-type: image/jpeg\r\n\r\n",
 "Content-type: image/bmp\r\n\r\n",
 "Content-type: application/octet-stream\r\n\r\n",
 "Content-type: application/x-javascript\r\n\r\n",
 "Content-type: audio/x-pn-realaudio\r\n\r\n",
 "Content-type: text/css\r\n\r\n",
 "Content-type: application/x-shockwave-flash\r\n\r\n",
 "Content-type: text/plain\r\n\r\n",
 "HTTP/1.0 200 OK\r\n",
 "HTTP/1.0 404 File not found\r\n",
 "Server: lwIP/1.3.0 \r\n",
 "\r\n

404: The requested file cannot be found."             \

   " \r\n",
 "Content-type: application/xml\r\n\r\n"
};

#define HTTP_HDR_HTML           0
#define HTTP_HDR_SSI            1
#define HTTP_HDR_GIF            2
#define HTTP_HDR_JPG            3
#define HTTP_HDR_PNG            4
#define HTTP_HDR_BMP            5
#define HTTP_HDR_APP            6
#define HTTP_HDR_JS             7 //"Content-type: application/x-javascript\r\n\r\n",
#define HTTP_HDR_RA             8
#define HTTP_HDR_CSS            9
#define HTTP_HDR_SWF            10
#define HTTP_HDR_DEFAULT_TYPE   11
#define HTTP_HDR_OK             12
#define HTTP_HDR_NOT_FOUND      13
#define HTTP_HDR_SERVER         14
#define DEFAULT_404_HTML        15
#define HTTP_HDR_XML            16 //"Content-type: application/xml\r\n\r\n"

//列出所有的文件类型, 必须包含所有的. 09-3
tHTTPHeader g_psHTTPHeaders[] = {
 { "html", HTTP_HDR_HTML},
 { "htm",  HTTP_HDR_HTML},
 { "shtml",HTTP_HDR_SSI},
 { "shtm", HTTP_HDR_SSI},
 { "ssi",  HTTP_HDR_SSI},
 { "gif",  HTTP_HDR_GIF},
 { "png",  HTTP_HDR_PNG},
 { "jpg",  HTTP_HDR_JPG},
 { "bmp",  HTTP_HDR_BMP},
 { "class",HTTP_HDR_APP},
 { "cls",  HTTP_HDR_APP},
 { "js",   HTTP_HDR_JS},
 { "ram",  HTTP_HDR_RA},
 { "css",  HTTP_HDR_CSS},
 { "swf",  HTTP_HDR_SWF},
 { "xml",  HTTP_HDR_XML} //09-3
};
#define NUM_HTTP_HEADERS (sizeof(g_psHTTPHeaders) / sizeof(tHTTPHeader))
#endif

阅读(2010) | 评论(0) | 转发(0) |
0

上一篇:sprintf 和 sscanf

下一篇:extern "C" 的说明

给主人留下些什么吧!~~