C

Streaming API で XMLの読み出し

Libxml2 の Streaming API っていうのは、 XML を頭から解析するときに役立つ API。SAXと違ってハンドラを登録する必要がないから楽。 #include <stdio.h> #include <libxml/tree.h> #include <libxml/xmlreader.h> void processNode(xmlTextReaderPtr reader) { const xmlChar *name; const xmlChar *val</libxml/xmlreader.h></libxml/tree.h></stdio.h>…

Writing API で XMLの書き出し

Libxml2 の Writing API っていうのは、 XML を頭から出力するときに役立つ API。 #include <stdio.h> #include <string.h> #include <libxml/encoding.h> #include <libxml/xmlwriter.h> #define MY_ENCODING "UTF-8" /*****************************************************************************************/ /*</libxml/xmlwriter.h></libxml/encoding.h></string.h></stdio.h>…

属性の削除

xmlHasPropで属性を持っているか確認しあと、xmlRemovePropでその属性を削除。 #include <stdio.h> #include <string.h> #include <libxml/tree.h> #include <libxml/parser.h> #include <libxml/xpath.h> int del_attribute(xmlNodeSetPtr nodes) { xmlAttrPtr attr; int size; int i; size = (nodes) ? nodes->nodeNr : 0; for(</libxml/xpath.h></libxml/parser.h></libxml/tree.h></string.h></stdio.h>…

ノードの削除

xmlUnlinkNodeでノードを切り話したあと、xmlFreeで削除する。 #include <stdio.h> #include <string.h> #include <libxml/tree.h> #include <libxml/parser.h> #include <libxml/xpath.h> int del_node(xmlNodeSetPtr nodes) { int size; int i; size = (nodes) ? nodes->nodeNr : 0; for(i = 0; i < nodes->nodeNr; ++i) { xmlU</libxml/xpath.h></libxml/parser.h></libxml/tree.h></string.h></stdio.h>…

属性の追加

xmlNewPropで属性を追加できる。 #include <stdio.h> #include <string.h> #include <libxml/tree.h> #include <libxml/parser.h> #include <libxml/xpath.h> int add_attribute(xmlNodeSetPtr nodes) { int size; int i; size = (nodes) ? nodes->nodeNr : 0; for(i = 0; i < nodes->nodeNr; ++i) { xmlNewProp(nodes->nodeTab[i</libxml/xpath.h></libxml/parser.h></libxml/tree.h></string.h></stdio.h>…

テキストの追加

テキストを追加するには、ノードを追加するのと同じようにxmlNewNodeでノードを作って、xmlAddChildで親にくっつればいい。テキストもツリーの一部に過ぎないので。 #include <stdio.h> #include <string.h> #include <libxml/tree.h> #include <libxml/parser.h> #include <libxml/xpath.h> int add_text(xmlNodeSetPtr nodes) {</libxml/xpath.h></libxml/parser.h></libxml/tree.h></string.h></stdio.h>…

ノードの追加

ノードを追加するには、xmlNewNodeでノードを作って、xmlAddChildで親にくっつればいい。以下、XPathでノードを検索して、そのノードの下に新しいノードをくっけている。 #include <stdio.h> #include <string.h> #include <libxml/tree.h> #include <libxml/parser.h> #include <libxml/xpath.h> int add_node(xmlNodeSetPtr nod</libxml/xpath.h></libxml/parser.h></libxml/tree.h></string.h></stdio.h>…

Libxml2 で XPath

xmlXPathEvalExpression()でXPathに一致したノードのリストを取得できる。 #include <stdio.h> #include <string.h> #include <libxml/tree.h> #include <libxml/parser.h> #include <libxml/xpath.h> void trim(char *str) { int i; for (i = strlen(str) - 1; 0 <= i; i--) { if ((str[i] == '\r') || (str[i] == '\n') || (str</libxml/xpath.h></libxml/parser.h></libxml/tree.h></string.h></stdio.h>…

DOM で tree walk (Libxml2)

DOM で XML のツリー構造を解析する。ノードはリストになって、同じ階層のノードは node->next で、子のノードは cur_node->children で辿ることができる。 #include <stdio.h> #include <string.h> #include <libxml/tree.h> #include <libxml/parser.h> void trim(char *str) { int i; for (i = strlen(str) - </libxml/parser.h></libxml/tree.h></string.h></stdio.h>…

DOM でXML 作成 (Libxml2)

DOMでXMLファイル書き出す。xmlNewDoc()でDOMの雛形をつくる。xmlNewNode()でノードを作成して、ルートノードからくっつけていく。 #include <stdio.h> #include <libxml/tree.h> #include <libxml/parser.h> xmlNodePtr add_node(xmlNodePtr node, char *node_name, char *text, char *attr, char *at</libxml/parser.h></libxml/tree.h></stdio.h>…

Libxml2 環境

Libxml2でコンパイルするにはコンパイルオプションを以下のようにする。 gcc `xml2-config --cflags` srouce.c -o a.out `xml2-config --libs` この後のエントリで役に立つ例を書く予定。とりあえず、Libxml2の使い方の例。役に立たないけど、XMLファイルを…

Doxygen 向け C/C++ ファイルのコメントテンプレート

C C++

Doxygen 高機能なんだけど、高機能すぎてどうやって Doxygen 用のコメントを書けばいいのかよくわからん。コメントとルール作りも兼ねてテンプレートを作っておこう。Doxygen のいいところはソースコードが可視化されることもあるけど、コメントを付けてない…

C プログラミングでの 設定ファイル読み込み

Windows だと iniファイルを GetPrivateProfileString などで読めるが、 C 単体だとそんなのない。 C で ini ファイルのパーサを作るか、何かライブラリを入れるしかない。そこで、 ini ファイルの変わりにシェルスクリプトと環境変数を使うことを考える。設…

pthread で タイマスレッドの実装

pthread で 指定したタイムアウト時間にコールバック関数を実行するタイマスレッドを実装。 nanosleep で 10ms 毎に起床しタイマアウトをチェックする。起床タイミングは nanosleep の性能 によるので、古い Linux とかだと正確に 10ms にならない。 10 ms …

pthread で itron ライクな sng_msg と rcv_msg (例)

http://d.hatena.ne.jp/l1o0/20100119/1263910139 の使用例 #include <mbox_test.h> #define NUM_OF_THREAD 3 void test_example_init() { int ercd; int i; u_int mbox_ids[NUM_OF_THREAD]; ercd = init_mbox_table(); if (E_OK != ercd) { fprintf(stderr, "init_mbox_t</mbox_test.h>…

nanosleep の精度

nanosleep が pthread のスケジューリングポリシーと優先度で違いが出るか実験。結果は、変わらない(当たり前?)。それより、 10 ms の nanosleep からオーバヘッド 1ms 程度以内 で復帰できるとは最近の Linux は精度があがってる。 #include <stdio.h> #include <stdlib.h> #in</stdlib.h></stdio.h>…

pthread で itron ライクな sng_msg と rcv_msg (ソースコード)

pthread で itron ライクなメッセージ機能の実装の続き http://d.hatena.ne.jp/l1o0/20100118/1263812821メモリブロックは malloc 使用。リングバッファをメールキューとして、リングバッファのアクセスを mutex でガード。 pthread_cond_signal で送信通知…

pthread で itron ライクな sng_msg と rcv_msg

pthread で itron ライクなメッセージ機能の実装。とりあえずヘッダファイル。メールボックスの数は NUM_OF_MBOX 、メールキューの大きさは NUM_OF_MSG で決め打ちにした。 #ifndef _MBOX_H_ #define _MBOX_H_ #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #inc</stdlib.h></stdio.h>…