2015年3月26日 星期四

Linux/C/line counter / 最簡單的方式取得 行數

這邊直接來介紹如果檔案中美行資料一樣長

如何取得一個檔案有幾行的最快方式。



    file=fopen(/tmp/123,"rb");
    uint64 len=0;
    fseek(file,0L,SEEK_END);
    len= ftell( file);
    fseek(file,0, SEEK_SET);

原理就是先把檔案移動到檔案尾巴,就可以知道整個檔案的長度了,

之後再把總長除以每行長就好囉。

2015年3月25日 星期三

Linux / Ubunut Shell 分割字串 / Separete String by space or delimiter.


如何在Linux Shell中分割字串呢?

直接來看個範例吧!

#!/bin/sh

String1 = "AA BB CC"
String2 = "DD;EE;FF"

arr1=$(echo $String1 | tr " " "\n")
arr2=$(echo $String2 | tr ";" "\n")

for x1 in $arr1
do
    echo "[$x1]
done


echo ""
echo ""



for x2 in $arr2
do
    echo "[$x2]
done


最後的結果為

AA
BB
CC

DD
EE
FF


是不是很簡單呢?

原理就是運用tr這個指令把你想要切割的字元(例如空白或者 ';') 替換成換行符號,
之後就可以用for 迴圈來做分割了!

2015年2月2日 星期一

2015/02/02 AllSeen /AllJoyn in openwrt 12.09 with TP-Link 1043nd V1

最近開始研究 AllSeen 也就是Alljoyn.

我的測試機器為TP-Link1043nd V1 ,上面跑的OS為 openwrt 12.09的版本,
並不是原生TPLink的 OS哦!


2014年12月23日 星期二

ld: warning: cannot find entry symbol _start;

如題,今天compile一個東西,只想compile他的lib而不是整個daemon,

所以就下了 make XXXlib 這樣的指令,

實際執行起來大概像是  /usr/bin/ld hello.c -o hello.o 這樣的東西

就跑出了如標題這樣的錯誤,雖然只是一個warning 但是根本可以看做一個error,因為位置

根本全部都錯亂,所以下面一定跑出一堆undefined reference,

研究了許久

終於發現要在後面加上 -lc



/usr/bin/ld hello.c -o hello.o -lc

-lc 代表要跟compiler 說使用標準的C 函式庫

這樣終於可以過了 -.-


2014年12月15日 星期一

C++中函式輸入泛型並型態轉換為enum / In C++ function generic parameter casting to enum / casting void* to enum


在C++中不能直接cast成 enum 但在C 中可以
如以下source code 在C中可以COMPILE過 
情況類似此篇文章 typdef enum void* 的用法請益

typedef enum {
     enum1
     enum2,
     enum3}
myenum;
void myfunc(void *data)
{
    if((myenum)data==enum1)
      ...
    else if ((myenum)data==enum2)
     ...
    else
     ...
}

2014年10月28日 星期二

[LuaDec51] Compile for Ubuntu

首先先到  luadec的網站下載Luadec51的source code : http://files.luaforge.net/releases/
之後再把lua5.1版本的source code下載下來 :  http://www.lua.org/versions.html



由於luadec51會用到Lua51的LIB   (liblua.a    and  liblualib.a)
所以先進到lua5.1的資料夾中去把 Lua compile起來,
$> cd lua5.1...
$> make all

則 liblua.a   and liblualib.a 在  luadec51/src  中可以找到

之後只要自己寫個Makefile指向這兩個lib就可以compile成功並執行囉

如果對於如何寫一個Makefile有問題可以留言在底下。



2013年11月5日 星期二

[Python][suds][errno 111] 如果你用Python suds library 遇見了 errno111 的問題請進/If you using python suds library and error happen which errno is 111 , please come in

作業系統 /operating system: Fedora 17
Python :2.6

直接看code 這個問題困擾我很久,今天終於解決了!


PROBLEM CODE: 

#python code start #
from suds.client import Client

url = 'someurl?wsdl'
client = Client(url,cache=None)
print  client.service.yourfunction();


然後就會發生下面這個錯誤/ And some error cause



  File "/usr/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/client.py", line 542, in __call__
  File "/usr/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/client.py", line 602, in invoke
  File "/usr/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/client.py", line 637, in send
  File "/usr/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/transport/https.py", line 64, in send
  File "/usr/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/transport/http.py", line 77, in send
  File "/usr/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/transport/http.py", line 118, in u2open
  File "/usr/lib64/python2.6/urllib2.py", line 383, in open
    response = self._open(req, data)
  File "/usr/lib64/python2.6/urllib2.py", line 401, in _open
    '_open', req)
  File "/usr/lib64/python2.6/urllib2.py", line 361, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 1130, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib64/python2.6/urllib2.py", line 1105, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>