[54] | 1 | /*
|
---|
| 2 | * TOPPERS Software
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2007-2011 by TAKAGI Nobuhisa
|
---|
| 6 | *
|
---|
| 7 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 8 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 9 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 10 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 11 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 12 | * スコード中に含まれていること.
|
---|
| 13 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 14 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 15 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 16 | * の無保証規定を掲載すること.
|
---|
| 17 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 18 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 19 | * と.
|
---|
| 20 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 21 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 22 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 23 | * 報告すること.
|
---|
| 24 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 25 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 26 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 27 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 28 | * 免責すること.
|
---|
| 29 | *
|
---|
| 30 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 31 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 32 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 33 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 34 | * の責任を負わない.
|
---|
| 35 | *
|
---|
| 36 | */
|
---|
| 37 | /*!
|
---|
| 38 | * \file toppers/cpp.hpp
|
---|
| 39 | * \brief Cプリプロセッサ代替機能に関する宣言定義
|
---|
| 40 | */
|
---|
| 41 | #ifndef TOPPERS_CPP_HPP_
|
---|
| 42 | #define TOPPERS_CPP_HPP_
|
---|
| 43 |
|
---|
| 44 | #include "toppers/codeset.hpp"
|
---|
| 45 | #include <boost/utility.hpp>
|
---|
| 46 | #include <boost/filesystem/path.hpp>
|
---|
| 47 | #include <boost/filesystem/operations.hpp>
|
---|
| 48 | #include <boost/filesystem/exception.hpp>
|
---|
| 49 |
|
---|
| 50 | namespace toppers
|
---|
| 51 | {
|
---|
| 52 |
|
---|
| 53 | /*!
|
---|
| 54 | * \brief コメントの除去
|
---|
| 55 | * \param[in] first 入力元の先頭位置
|
---|
| 56 | * \param[in] last 入力元の終端位置の次
|
---|
| 57 | * \param[out] result 結果の出力先
|
---|
| 58 | * \param[in] codeset 文字コード
|
---|
| 59 | * \return 処理完了後の result の値
|
---|
| 60 | *
|
---|
| 61 | * CスタイルのブロックコメントとC++スタイルの行コメントを取り除きます。
|
---|
| 62 | * 行コメントは、行末の \\ があっても次の行には続きません。
|
---|
| 63 | */
|
---|
| 64 | template < class ForwardIterator, class OutputIterator >
|
---|
| 65 | OutputIterator remove_comment( ForwardIterator first, ForwardIterator last, OutputIterator result, codeset_t codeset = ascii )
|
---|
| 66 | {
|
---|
| 67 | enum { none, line_comment, block_comment, single_quote, double_quote } state = none;
|
---|
| 68 | char prev = '\0';
|
---|
| 69 |
|
---|
| 70 | for ( ; first != last; ++first, ++result )
|
---|
| 71 | {
|
---|
| 72 | char c = *first;
|
---|
| 73 | ForwardIterator next_iter = boost::next( first );
|
---|
| 74 | char next = ( next_iter != last ? *next_iter : '\0' );
|
---|
| 75 |
|
---|
| 76 | switch ( state )
|
---|
| 77 | {
|
---|
| 78 | case line_comment:
|
---|
| 79 | if ( c == '\n' )
|
---|
| 80 | {
|
---|
| 81 | state = none;
|
---|
| 82 | *result = c;
|
---|
| 83 | }
|
---|
| 84 | break;
|
---|
| 85 | case block_comment:
|
---|
| 86 | if ( c == '*' && next == '/' )
|
---|
| 87 | {
|
---|
| 88 | state = none;
|
---|
| 89 | ++first;
|
---|
| 90 | }
|
---|
| 91 | else if ( c == '\n' )
|
---|
| 92 | {
|
---|
| 93 | *result = c;
|
---|
| 94 | }
|
---|
| 95 | break;
|
---|
| 96 | case single_quote:
|
---|
| 97 | if ( c == '\\'
|
---|
| 98 | && !( codeset == shift_jis && is_lead< shift_jis >( prev ) )
|
---|
| 99 | && next == '\'' ) // '〜\'〜 の場合
|
---|
| 100 | {
|
---|
| 101 | *result++ = *first++;
|
---|
| 102 | }
|
---|
| 103 | else if ( c == '\'' )
|
---|
| 104 | {
|
---|
| 105 | state = none;
|
---|
| 106 | }
|
---|
| 107 | *result = *first;
|
---|
| 108 | break;
|
---|
| 109 | case double_quote:
|
---|
| 110 | if ( c == '\\'
|
---|
| 111 | && !( codeset == shift_jis && is_lead< shift_jis >( prev ) )
|
---|
| 112 | && next == '\"' ) // "〜\"〜 の場合
|
---|
| 113 | {
|
---|
| 114 | *result++ = *first++;
|
---|
| 115 | }
|
---|
| 116 | else if ( c == '\"' )
|
---|
| 117 | {
|
---|
| 118 | state = none;
|
---|
| 119 | }
|
---|
| 120 | *result = *first;
|
---|
| 121 | break;
|
---|
| 122 | default:
|
---|
| 123 | switch ( c )
|
---|
| 124 | {
|
---|
| 125 | case '/': // コメント開始の検出
|
---|
| 126 | if ( next == '*' )
|
---|
| 127 | {
|
---|
| 128 | state = block_comment;
|
---|
| 129 | ++first;
|
---|
| 130 | }
|
---|
| 131 | else if ( next == '/' )
|
---|
| 132 | {
|
---|
| 133 | state = line_comment;
|
---|
| 134 | ++first;
|
---|
| 135 | }
|
---|
| 136 | else
|
---|
| 137 | {
|
---|
| 138 | *result = *first;
|
---|
| 139 | }
|
---|
| 140 | break;
|
---|
| 141 | case '\'':
|
---|
| 142 | state = single_quote;
|
---|
| 143 | *result = c;
|
---|
| 144 | break;
|
---|
| 145 | case '\"':
|
---|
| 146 | state = double_quote;
|
---|
| 147 | *result = c;
|
---|
| 148 | break;
|
---|
| 149 | default:
|
---|
| 150 | *result = c;
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | prev = *first;
|
---|
| 155 | }
|
---|
| 156 | return result;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /*!
|
---|
| 160 | * \brief インクルードパスの探索
|
---|
| 161 | * \param[in] first 探索対象ディレクトリ列の先頭位置
|
---|
| 162 | * \param[in] last 探索対象ディレクトリ列の終端位置の次
|
---|
| 163 | * \param[in] headername 探索対象のヘッダ名
|
---|
| 164 | * \return 探索に成功すればヘッダへの相対またはフルパスを返す。失敗した場合は空文字列を返す。
|
---|
| 165 | */
|
---|
| 166 | template < class InputIterator >
|
---|
| 167 | std::string search_include_file( InputIterator first, InputIterator last, std::string const& headername )
|
---|
| 168 | {
|
---|
| 169 | namespace fs = boost::filesystem;
|
---|
| 170 | // fs::path filename( headername, fs::native );
|
---|
| 171 | fs::path filename( headername ); // filesystem3対応
|
---|
| 172 |
|
---|
| 173 | if ( fs::exists( filename ) && !fs::is_directory( filename ) )
|
---|
| 174 | {
|
---|
| 175 | return headername;
|
---|
| 176 | }
|
---|
| 177 | while ( first != last )
|
---|
| 178 | {
|
---|
| 179 | // fs::path pathname = fs::path( *first, fs::native )/filename;
|
---|
| 180 | fs::path pathname = fs::path( *first )/filename; // filesystem3対応
|
---|
| 181 | if ( fs::exists( pathname ) && !fs::is_directory( pathname ) )
|
---|
| 182 | {
|
---|
| 183 | // return pathname.native_file_string();
|
---|
| 184 | return pathname.string(); // filesysytem3対応
|
---|
| 185 | }
|
---|
| 186 | ++first;
|
---|
| 187 | }
|
---|
| 188 | return std::string();
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | std::string expand_quote( std::string const& str );
|
---|
| 192 | std::string quote_string( std::string const& str );
|
---|
| 193 |
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | #endif // ! TOPPERS_CPP_HPP_
|
---|