2011年11月4日 星期五

Struct for sockaddr,sockaddr_in,sockaddr_in6,addrinfo

struct sockaddr {
      unsigned short sa_family;        /* address family, AF_xxx */
      char sa_data[14];                /* 14 bytes of protocol address */
};

struct sockaddr_in {
      short int sin_family;            /* Address family AF_INET */
      unsigned short int sin_port;     /* Port number */
      struct in_addr sin_addr;         /* Internet address */
      unsigned char sin_zero[8];       /* Same size as struct sockaddr */
};
struct in_addr {
      unsigned long s_addr;            /* Internet address */
};

struct sockaddr_in6 {
      sa_family_t sin6_family;         /* AF_INET6 */
      in_port_t sin6_port;             /* transport layer port # */
      uint32_t sin6_flowinfo;          /* IPv6 traffic class & flow info */
      struct in6_addr sin6_addr;       /* IPv6 address */
      uint32_t sin6_scope_id;          /* set of interfaces for a scope */
};
struct in6_addr {
      uint8_t s6_addr[16];             /* IPv6 address */
};

struct addrinfo{
      int ai_flags;                    /* AI_PASSIVE,AI_CANONNAME,AI_NUMERICHOST */
      int ai_family;                   /* AF_INET,AF_INET6 */
      int ai_socktype;                 /* SOCK_STREAM,SOCK_DGRAM */
      int ai_protocol;                 /* IPPROTO_IP, IPPROTO_IPV4, IPPROTO_IPV6 */
      size_t ai_addrlen;               /* Length */
      char *ai_cannoname;              /* */
      struct sockaddr *ai_addr;        /* struct sockaddr */
      struct addrinfo *ai_next;        /* pNext */
};

2011年10月24日 星期一

Dichotomy Sort 二分排序法

二分法排序(Dichotomy Sort)的精神就是將目標對切,與中間元素相比,較小就把左邊剩餘的陣列再對切,然後重複做一樣的事直到最後
反之亦然,把所有元素都做過這個動作後就是排序好的陣列

int array[100];
for (int i = 0; i < 100; i++)
{
 int start, end, mid;
 start = 0;
 end = i - 1;
 mid = 0;
 int temp = array[i];
 while (start <= end)
 {
  mid = (start + end) / 2;
  if (array[mid] > temp)//目標元素在陣列左邊
  {
   end = mid - 1;
  }
  else
  {
   start = mid + 1;
  }
 }
 for (int j = i - 1; j > end; j--)//找到插入的位置,將其後的元素後移一位
 {
  array[j + 1] = array[j];
 }
 array[end + 1] = temp;

}

2011年10月5日 星期三

不定參數的使用 (Who to use Variable-length argument)

所謂的不定參數指的就是在定義function時,我們無法事前得知會傳入的參數個數(如printf)時的一種function撰寫技巧
以下是使用不定參數另外定義一個function,讓我們可以在print時可為console畫面加上顏色。

#define DBG_RED "\033[22;31m"
#define DBG_GREEN "\033[35;32m"
#define DBG_BLUE "\033[22;34m"
#define DBG_MAGENTA "\033[22;35m"
#define DBG_WHITE "\033[01;37m"
#define DBG_YELLOW "\033[01;33m"

#define DBG_LRED "\033[01;31m" 
#define DBG_LGREEN "\033[01;32m"
#define DBG_LBLUE "\033[01;34m"


void mydebug(const char *color,const char *format, ...);//__attribute__((format(printf,2,3)));

#define DBG(color, fmt, arg...)  mydebug(color, fmt, ## arg)

void mydebug(const char *color,const char *format, ...)
{
 static char text[512];
 va_list ap; 
 
 va_start(ap, format);
 vsnprintf((char *)text,512,format, ap);
 va_end(ap);
 if (text[strlen(text) - 1] == '\n')
 {
  text[strlen(text) - 1] = 0;
  printf("%s%s\033[0m\n",color,text);
 }
 else
  printf("%s%s\033[0m",color,text);
}

USAGE:
DBG(DBG_RED, "Himiro Test:%s\n", "Hello world");

2011年10月3日 星期一

在Access 2007找到表單

access2007
左上角的「office選項」點下去→access選項→目前資料庫→顯示表單

2011年9月19日 星期一

Tera Term VT - 為不同 serial port 設定專屬 shortcut

如果你手上有多個Device,同時每個Device都有自己的baud rate,可以試著做以下的設定,

這樣就不用每次開起Tera Term的時候都有另外再調整設定

先建立一個Tera Term的捷徑,然後右鍵->內容,在目標的最後面加上

/BAUD=57600 /C=3

就可以了,以下是說明。

/BAUD=
baud rate of serial port. If the baud rate not supported by Tera Term is specified, this option is ignored.

/C=
Serial port
/C=1 COM1
/C=2 COM2
/C=3 COM3
:
/C=256 COM256

參考連結:
Tera Term command line

2011年8月4日 星期四

How to calculate function time cost in C

#include <sys/time.h> 
#include <stdio.h> 

int main(){
 struct timeval tv1, tv2;
 gettimeofday(&tv1,NULL);

 usleep(1000);

 gettimeofday(&tv2,NULL);

 printf("Time Cost is %d ms\n", (tv1.tv_sec - tv2.tv_sec) * 1000 );
}

2011年8月3日 星期三

SQLite Compile

今天在Compile sqlite要用configure時遇到一些錯誤 
checking host system type... Invalid configuration `cr16-uclinux': machine `cr16 ' not recognized 
configure: error: /bin/sh ./config.sub cr16-uclinux failed 

 copy /usr/share/libtool/config/config.guess and config.sub to your working folder can fix this problem 

And... 

#error It appears you have defined _FILE_OFFSET_BITS=64. Unfortunately, uClibc was built without large file support enabled. 
#error Sorry... uClibc was built without large file support! 

只要在Makefile加上下面兩個flags就可以過了
-DSQLITE_OMIT_LOAD_EXTENSION
-DSQLITE_OMIT_COMPLETE

11/10/18補充:

-DSQLITE_DISABLE_LFS
#error Sorry... uClibc was built without large file support!