2011年12月16日 星期五

Linux指令筆記

samba mount
mount -t cifs //ip/path /localpath -o username=user,password=pass

2011年12月2日 星期五

[轉貼] mkimage使用詳解

uboot源代碼的tools/目錄下有mkimage工具,這個工具可以用來製作不壓縮或者壓縮的多種可啟動映像檔案。

mkimage在製作映像檔案的時候,是在原來的可執行映像檔案的前面加上一個0x40位元組的頭,記錄參數所指定的訊息,這樣uboot才能識別這個映像是針對哪個CPU體系架構的,哪個OS的,哪種類型,加載內存中的哪個位置,入口點在內存的那個位置以及映像名是什麼

root@Glym:/tftpboot# ./mkimage
Usage: ./mkimage -l image
-l ==> list image header information
./mkimage -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image
-A ==> set architecture to 'arch'
-O ==> set operating system to 'os'
-T ==> set image type to 'type'
-C ==> set compression type 'comp'
-a ==> set load address to 'addr' (hex)
-e ==> set entry point to 'ep' (hex)
-n ==> set image name to 'name'
-d ==> use image data from 'datafile'
-x ==> set XIP (execute in place)
參數說明︰

-A 指定CPU的體系架構︰

取值 表示的體系架構
alpha Alpha 
arm A RM 
x86 Intel x86
ia64 IA64
mips MIPS
mips64 MIPS 64 Bit
ppc PowerPC
s390 IBM S390
sh SuperH
sparc SPARC
sparc64 SPARC 64 Bit
m68k MC68000

-O 指定作業系統類型,可以取以下值︰
openbsd、netbsd、freebsd、4_4bsd、linux、svr4、esix、solaris、irix、sco、dell、ncr、lynxos、vxworks、psos、qnx、u-boot、rtems、artos

-T 指定映像類型,可以取以下值︰
standalone、kernel、ramdisk、multi、firmware、script、filesystem

-C 指定映像壓縮模式,可以取以下值︰
none 不壓縮
gzip 用gzip的壓縮模式
bzip2 用bzip2的壓縮模式

-a 指定映像在內存中的加載位址,映像下載到內存中時,要按照用mkimage製作映像時,這個參數所指定的位址值來下載

-e 指定映像營運的入口點位址,這個位址就是-a參數指定的值加上0x40(因為前面有個mkimage添加的0x40個位元組的頭)

-n 指定映像名

-d 指定製作映像的源檔案
Reference from http://www.wretch.cc/blog/poemsoul/15441093

2011年11月14日 星期一

const的意義(const pointer, pointer to const, const pointer to a const, const function...)

#include
#include
using namespace std;
class A
{
private:
     int y;
public:
 A(){
  y=0;
 }
 int f1(int x) const // 此const代表此function不可以修改Data Member
 {
  x++;
  // y++; // Error! y為Data Member,因此不可以修改
  return y;
 }
 const int f2(int x) // 回傳值為const
 {
  x++;
  y++;
  return y;
 }
 int f3(int const x) // 參數為const
 {
  // x++; // Error! x為const,不可以修改
  y++;
  return y;
 }
 void f4(int const *x) // x is variable pointer to a constant integer,也可以寫成f4(const int *x)
 {
  // (*x)++; // Error! pointer x所指向的實體為const,因此不可以修改其值
  *x++; // 此行的意思是*(x++),先修改pointer x的值,在指向實體
 }
 void f5(int const &x) // x is variable reference to a constant integer
 {
  // x++; // Error! x就是一個const
 }
 void f6(int *const x) // x constant pointer to a variable integer
 {
  (*x)++;
  // *x++; // Error! Constant pointer x 不可以指向其他實體
 }
 const char * f7() // 回傳一個variable pointer to a constant char
 {
  return "ABC";
 }
 void f8(const int * const x) // x is constant pointer to a const integer
 {
  // (*x)++; // Error! x所指向的實體為const,因此不可以修改其值
  // *x++; // Error! Constant pointer x 不可以指向其他實體
 }
};
int main(){
 A a;
 int x=3;
 a.f1(x);
 a.f2(x);
 a.f3(x); // int x 會自動轉成const int x傳入
 a.f4(&x) ;
 a.f5(x) ;
 const char *s = a.f7(); // 必須使用一個const來接const
 // s[0]='0'; // Error! 無法對const 變數進行改變動作
 return 0;
}

Reference: http://csie-tw.blogspot.com/2010/03/c-constconst-pointer-pointer-to-const.html

2011年11月9日 星期三

How to use cpplint.py

First, you should install python

Second, download cpplint.py

Third, run below script in command line

cpplint.py –output=vs7 helloworld.cpp

Then you can get the result.

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!

2011年6月17日 星期五

warning: this decimal constant is unsigned only in ISO C90

今天compile時看到一個Warning message寫著

warning: this decimal constant is unsigned only in ISO C90

當做Pointer是否溢位的判斷時,因為用到了4294967295這個值來做比對而導致C90的問題

對一個Pointer來說 4294967295,當表著一個最大的位置,本身應當是沒錯的

可是當我們直接拿來使用時compiler不知道它真正的用途而報警告

這時只要我們將4294967295加上一個u,即4294967295u,讓compiler知道這是一個unsigned的值,就不會報錯了。

2011年6月9日 星期四

invalid project description

Today , when I went to create a new Android project from existing source , Eclipse give me a message with "invalid project description" and cannot create the project.

After I move the source from Eclipse workspace to other place, everything is ok.

今天我要從一個現有的專案去建立一個新專案時
Eclipse給了我一個訊息 "invalid project description"
然後也沒建出來新的專案


當我把原先想加入的專案從Eclipse的workspace移到別的地方後這個問題就解決了




PS:我怎麼覺得中文比英文難寫…

2011年5月26日 星期四

Linux中的likely,unlikely

likely跟unlikely其實是兩個define,其內容如下:
#define likely(x) __builtin_expect(!!(x), 1)  
#define unlikely(x) __builtin_expect(!!(x), 0)  


它的功用比較像是提醒compiler這個判斷式比較容易為真或比較容易為假
進而調整pipeline...
比較可能執行的放到較近的地方,比較不可能執行的放到較遠的地方
以下是 __builtin_expect的說明


__builtin_expect(),查閲GCC手册,發現其定義如下:
long __builtin_expect (long exp, long c) [Built-in Function]
You may use __builtin_expect to provide the compiler with branch prediction
information. In general, you should prefer to use actual profile feedback for this
(‘-fprofile-arcs’), as programmers are notoriously bad at predicting how their
programs actually perform. However, there are applications in which this data is
hard to collect.
The return value is the value of exp, which should be an integral expression. The
value of c must be a compile-time constant. The semantics of the built-in are that it
is expected that exp == c. For example:
if (__builtin_expect (x, 0))
foo ();
would indicate that we do not expect to call foo, since we expect x to be zero. Since
you are limited to integral expressions for exp, you should use constructions such as
if (__builtin_expect (ptr != NULL, 1))
error ();
when testing pointer or floating-point values.

大概是說我們可以用__builtin_expect()來提供compiler分支預測
下面懶的翻了…反正就我們預測它比較可能為真就用likely
比較不會成立就用unlikely...

2011年5月25日 星期三

隱藏Blogger上面的導航列(Navbar)

如果有人覺得上方的那列很礙眼可以這樣做

在"設計"的"修改HTML"中
找到
/* Primary layout */
然後在它下面加上

#navbar-iframe {
display: none !important;
}


就OK了

於是我就不知道怎麼登入了…

2011年5月16日 星期一

sscanf心得

%*s => 加上星號表視忽略該字串,簡單來說,就是無法在後面的不定參數中接收該值

For example:
假設source為
<changecount attr="500">100</changecount>

sscanf(text,"%*[^>]>%[^<]",tmp); %*[^>] 就是取到第一次的>前,加上星號代表這是用不到的部份
也就是<changecount attr="500" 為止,
此時的目標變成>100</changecount>
接著%[^<] 代表</changecount>的<前 於是就剩下>100啦
再加上上面的>符號取出來的東西就是100了。


int main()
{
char text[]="1005";
char text2[]= "attr = \"1000\" str = \"50000\"";
double number=0;
char tmp[100];

sscanf(text,"%*[^>]>%d",&number);
printf("Himiro Test:number is %d\n",number);

//sscanf(text,"%*[^>]>%[^<]<%*[^\n]",tmp);
sscanf(text,"%*[^>]>%[^<]",tmp);
printf("Himiro Test:number is %s\n",tmp);

sscanf(text2,"%*[^\"]\"%d",&number);
printf("Himiro Test:value is %d\n",number);

//sscanf(text2,"%*[^\"]\"%[^\"][^\"]",tmp);
sscanf(text2,"%*[^\"]\"%[^\"]",tmp);   //%*[^"]"%[^"]
printf("Himiro Test:value is %s\n",tmp);
}

Result:
Himiro Test:number is 1005
Himiro Test:number is 1005
Himiro Test:value is 1000
Himiro Test:value is 1000

Print colored line in C


#include <stdio.h>
#include <stdarg.h>

#define DEBUG_GREEN "\033[35;32m"
#define DEBUG_LIGHT_BLUE "\033[01;34m"
#define DEBUG_RESTORE "\033[0m"

#define DBG(x,fmt, arg...) debug(x,fmt, ## arg)
extern void debug(const char *color,const char *format, ...)__attribute__((forma t(printf,2,3)));

void debug(const char *color,const char *format, ...)
{
char text[1024];
va_list ap;
//if (!debug_enabled)
// //return;

va_start(ap, format);
vsnprintf((char *)text,1024,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);
}

int main()
{
DBG(DEBUG_LIGHT_BLUE,"Himiro Test: %s\n",Hello World);
return 0;
}

Patterns of POSIX.2 REGEX


^ 定位規則,文字開頭
$ 定位規則,文字尾端
. 單一規則,代表任意字元
[chars] 單一規則,有 chars 裡其中一個字元
[^chars] 單一規則,沒有 chars 裡其中一個字元
? 倍數規則, 0 或 1 個的前導符號
* 倍數規則, 0 或多個的前導符號
+ 倍數規則, 1 或多個的前導符號
{n,m} 表示前一符號在字串中的重覆次數。
例如 A{2} 表示 'A' 重覆兩次 (即 'AA') ;
A{2,} 表示字串含有 2 到無數多個 'A' ;
A{2,5} 表示含有 2 到 5 個 'A' 。
\char 轉義,將 char 視為一般字元,而非樣式規則字元
(string) 子樣式規則,將 string 記憶起來,歸於一組。
稍後可利用 \n 的方式,將第 n 組 string 提出。

2011年5月12日 星期四

Typeless swap

Today, one of my colleagues ask me how to create a typeless swap marco,
This is the solution.

#define MySWAP(a, b) \
do { \
typeof (a) _temp = (a);\
(a) = (b); \
(b) = _temp; \
} while(false)

int main()
{
float a=1.33;
float b=2.75;
MySWAP(a,b);
}

2011年4月20日 星期三

Adding an SD Card to an existing AVD

You can easily do that manually:
1- find the path where your avd is stored
$ SDK/tools/android list avd
....
Path: /home/user/.android/avd/myAVD.avd
...
2- create an "sdcard.img" in that directory -- that name is the
default and the emulator will pick it up automatically:
$ SDK/tools/mksdcard 16M /home/user/.android/avd/myAVD.avd/sdcard.img
Run mksdcard without arguments to see the options. Size is something
like 128K or 128M.
Note that "list avd" will not display it after (it only displays
what's in the myAVD.avd/config.ini file... you can add a line like
"sdcard.size=16M" if you want "list avd" to show it.

How to use external JARs in an Android project

Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar

How To Generating R File

Project => Clean
Clean proects selected below
choice your project.

2011年4月2日 星期六

What's the “dot” for when registering an Activity

<activityandroid:name=".NewActivity"></activity>

http://developer.android.com/guide/topics/manifest/activity-element.html#nm

android:name
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the .



As you have noticed the point is not necessary but it basically means: the activity class lives in the same package of the app. So, if your app package is: com.my.package then:

.YourActivity means that your class is inside com.my.package.
YourActivity means that your class is inside com.my.package (same as above).
.activities.YourActivity means that your class is inside com.my.package.activitites.
You can even do something like: com.my.package.activities.YourActivity which is useful when you want to have different versions of your app and use Ant to change the references to the package automatically.

2011年3月17日 星期四

typedef 用法 (struct)


struct Student {
char name[32];
int number;
};

要使用時就必需宣告為


struct Student mystudent = {"John", 10};


我們可以為它加上別名


typedef struct {
char name[32];
int number;
} Student;


這樣就可以直接有一個Student型別可以使用


Student mystudent = {"John", 10};

2011年2月24日 星期四

記錄Windows XP 使用者每次登入的時間

從「開始」→「控制台」→「系統管理工具」,在「系統管理工具」的項目,接著點選「本機安全性原則」
在 「本機原則」→「稽核原則」,於右方 雙擊「稽核帳戶登入事件」,彈出「本機安全性設定」, 勾選 「成功」及「失敗」;同樣又雙擊「稽核登入事件」,勾選 「成功」及「失敗」,就能夠在「事件檢視器」中的「安全性記錄」看到登入的資料了

2011年2月12日 星期六

Drupal 7.0 日期變成 @datetime 的解決方法

會出這樣的問題是因為中文語系檔出狀況

把drupal-7.0.zh-hant.po這個檔案的

msgid "Submitted by !username on !datetime"
msgstr "由 !username 在 @datetime 發表"

改成

msgid "Submitted by !username on !datetime"
msgstr "由 !username 在 !datetime 發表"

然後去後臺把繁中刪除在新增就好了…

記得先取消預設才能刪除