著者:後藤大地
これまでシステムに用意されている標準ライブラリだけを使ってソースコードを作成する例を取り上げてきたが、次のステップとしてサードパーティ製のライブラリを使う方法を紹介しよう。システムに標準で用意されているライブラリはすべての処理に対応しているわけではないので、特定の処理をしようと思ったらそれ向けに開発されたサードパーティ製ライブラリを利用することになる。
記事本文掲載のシェルスクリプトマガジンvol.52は以下のリンク先でご購入できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/* This is simple demonstration of how to use expat. This program reads an XML document from standard input and writes a line with the name of each element to standard output indenting child elements by one tab stop more than their parent element. It must be used with Expat compiled for UTF-8 output. __ __ _ ___\ \/ /_ __ __ _| |_ / _ \\ /| '_ \ / _` | __| | __// \| |_) | (_| | |_ \___/_/\_\ .__/ \__,_|\__| |_| XML parser Copyright (c) 1997-2000 Thai Open Source Software Center Ltd Copyright (c) 2000-2017 Expat development team Licensed under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <stdio.h> #include <expat.h> #ifdef XML_LARGE_SIZE # if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400 # define XML_FMT_INT_MOD "I64" # else # define XML_FMT_INT_MOD "ll" # endif #else # define XML_FMT_INT_MOD "l" #endif #ifdef XML_UNICODE_WCHAR_T # include <wchar.h> # define XML_FMT_STR "ls" #else # define XML_FMT_STR "s" #endif static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) { int i; int *depthPtr = (int *)userData; (void)atts; for (i = 0; i < *depthPtr; i++) putchar('\t'); printf("%" XML_FMT_STR "\n", name); *depthPtr += 1; } static void XMLCALL endElement(void *userData, const XML_Char *name) { int *depthPtr = (int *)userData; (void)name; *depthPtr -= 1; } int main(int argc, char *argv[]) { char buf[BUFSIZ]; XML_Parser parser = XML_ParserCreate(NULL); int done; int depth = 0; (void)argc; (void)argv; XML_SetUserData(parser, &depth); XML_SetElementHandler(parser, startElement, endElement); do { size_t len = fread(buf, 1, sizeof(buf), stdin); done = len < sizeof(buf); if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) { fprintf(stderr, "%" XML_FMT_STR " at line %" XML_FMT_INT_MOD "u\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); return 1; } } while (!done); XML_ParserFree(parser); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <stdio.h> #include <expat.h> static void XMLCALL startElement(void *userData, const XML_Char * name, const XML_Char ** atts) { int i; int *depthPtr = (int *)userData; (void)atts; for (i = 0; i < *depthPtr; i++) putchar('\t'); printf("%s\n", name); *depthPtr += 1; } static void XMLCALL endElement(void *userData, const XML_Char * name) { int *depthPtr = (int *)userData; (void)name; *depthPtr -= 1; } int main(int argc, char *argv[]) { char buf[BUFSIZ]; XML_Parser parser = XML_ParserCreate(NULL); int done; int depth = 0; (void)argc; (void)argv; XML_SetUserData(parser, &depth); XML_SetElementHandler(parser, startElement, endElement); do { size_t len = fread(buf, 1, sizeof(buf), stdin); done = len < sizeof(buf); if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) { fprintf(stderr, "%s at line %lu\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); return 1; } } while (!done); XML_ParserFree(parser); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="UTF-8"?> <document> <docgroup> <docinfo> <title><p>タイトル</p></title> <author><p>名前</p></author> <firstedition><p>編集開始日</p></firstedition> <lastmodified><p>編集終了日</p></lastmodified> </docinfo> <docgroup> <docinfo> <title><p>章タイトル</p></title> </docinfo> <p>説明1</p> <p>説明2</p> </docgroup> </docgroup> </document> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// XMLパーサを作成 XML_Parser parser = XML_ParserCreate(NULL); // 開始タグと終了タグを読み込んだタイミングで実行される // 関数を登録 XML_SetElementHandler(parser, startElement, endElement); // パースを実行(データをバッファに読み込みながら、デー // タが終了するまでこの処理を繰り返す) XML_Parse(parser, buf, (int)len, done); // 使用したXMLパーサを解放 XML_ParserFree(parser); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <stdio.h> #include <expat.h> static void XMLCALL begin(void *, const XML_Char*, const XML_Char**); static void XMLCALL end(void *, const XML_Char *); static int depth = 0; int main(int argc, char *argv[]) { char buf[BUFSIZ]; int len, done; XML_Parser parser = XML_ParserCreate(NULL); XML_SetElementHandler(parser, begin, end); do { len = fread(buf, 1, sizeof(buf), stdin); done = len < sizeof(buf); if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) return 1; } while (!done); XML_ParserFree(parser); return 0; } static void XMLCALL begin(void *d, const XML_Char *name, const XML_Char **attr) { for (int i = 0; i < depth; i++) printf("→"); printf("%s\n", name); ++depth; } static void XMLCALL end(void *d, const XML_Char * name) { --depth; } |