<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>LibOFX: file_preproc.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.0 --> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li id="current"><a href="files.html"><span>Files</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> </ul></div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul></div> <h1>file_preproc.cpp</h1><a href="file__preproc_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/***************************************************************************</span> <a name="l00002"></a>00002 <span class="comment"> file_preproc.cpp </span> <a name="l00003"></a>00003 <span class="comment"> -------------------</span> <a name="l00004"></a>00004 <span class="comment"> copyright : (C) 2004 by Benoit Grégoire</span> <a name="l00005"></a>00005 <span class="comment"> email : bock@step.polymtl.ca</span> <a name="l00006"></a>00006 <span class="comment">***************************************************************************/</span> <a name="l00012"></a>00012 <span class="comment">/***************************************************************************</span> <a name="l00013"></a>00013 <span class="comment"> * *</span> <a name="l00014"></a>00014 <span class="comment"> * This program is free software; you can redistribute it and/or modify *</span> <a name="l00015"></a>00015 <span class="comment"> * it under the terms of the GNU General Public License as published by *</span> <a name="l00016"></a>00016 <span class="comment"> * the Free Software Foundation; either version 2 of the License, or *</span> <a name="l00017"></a>00017 <span class="comment"> * (at your option) any later version. *</span> <a name="l00018"></a>00018 <span class="comment"> * *</span> <a name="l00019"></a>00019 <span class="comment"> ***************************************************************************/</span> <a name="l00020"></a>00020 <span class="preprocessor">#include <iostream></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <fstream></span> <a name="l00022"></a>00022 <span class="preprocessor">#include <stdlib.h></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <stdio.h></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <string></span> <a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="libofx-0_89_81_2inc_2libofx_8h.html">libofx.h</a>"</span> <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="messages_8hh.html">messages.hh</a>"</span> <a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="ofx__preproc_8hh.html">ofx_preproc.hh</a>"</span> <a name="l00028"></a>00028 <span class="preprocessor">#include "context.hh"</span> <a name="l00029"></a>00029 <span class="preprocessor">#include "<a class="code" href="file__preproc_8hh.html">file_preproc.hh</a>"</span> <a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="keyword">using namespace </span>std; <a name="l00032"></a><a class="code" href="file__preproc_8cpp.html#3a4f15870bd5573d14fceb17c65162ee">00032</a> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="file__preproc_8cpp.html#3a4f15870bd5573d14fceb17c65162ee">READ_BUFFER_SIZE</a> = 1024; <a name="l00033"></a>00033 <a name="l00034"></a>00034 <span class="comment">/* get_file_type_description returns a string description of a LibofxFileType </span> <a name="l00035"></a>00035 <span class="comment"> * suitable for debugging output or user communication.</span> <a name="l00036"></a>00036 <span class="comment"> */</span> <a name="l00037"></a><a class="code" href="libofx-0_89_81_2inc_2libofx_8h.html#efc89267742d989b5f17708076807b5e">00037</a> <span class="keyword">const</span> <span class="keywordtype">char</span> * <a class="code" href="inc_2libofx_8h.html#efc89267742d989b5f17708076807b5e">libofx_get_file_format_description</a>(<span class="keyword">const</span> <span class="keyword">struct</span> <a class="code" href="structLibofxFileFormatInfo.html">LibofxFileFormatInfo</a> format_list[], <span class="keyword">enum</span> <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de">LibofxFileFormat</a> file_format) <a name="l00038"></a>00038 { <a name="l00039"></a>00039 <span class="keyword">const</span> <span class="keywordtype">char</span> * retval = <span class="stringliteral">"UNKNOWN (File format couldn't be sucessfully identified)"</span>; <a name="l00040"></a>00040 <a name="l00041"></a>00041 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; <a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>[i].<a class="code" href="structLibofxFileFormatInfo.html#e4b1aa0050ffdc43ce68f07e6330d2f7">format</a>!=<a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de990cd45e93be2b4661f3499e0a658a33">LAST</a>; i++) <a name="l00042"></a>00042 { <a name="l00043"></a>00043 <span class="keywordflow">if</span>(<a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>[i].format==file_format) <a name="l00044"></a>00044 { <a name="l00045"></a>00045 retval = <a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>[i].description; <a name="l00046"></a>00046 } <a name="l00047"></a>00047 } <a name="l00048"></a>00048 <span class="keywordflow">return</span> retval; <a name="l00049"></a>00049 }; <a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="comment">/*</span> <a name="l00052"></a>00052 <span class="comment">libofx_get_file_type returns a proper enum from a file type string. </span> <a name="l00053"></a>00053 <span class="comment">*/</span> <a name="l00054"></a><a class="code" href="libofx-0_89_81_2inc_2libofx_8h.html#5fef31e6dc9df467c269a860aa4af12a">00054</a> <span class="keyword">enum</span> <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de">LibofxFileFormat</a> <a class="code" href="inc_2libofx_8h.html#5fef31e6dc9df467c269a860aa4af12a">libofx_get_file_format_from_str</a>(<span class="keyword">const</span> <span class="keyword">struct</span> <a class="code" href="structLibofxFileFormatInfo.html">LibofxFileFormatInfo</a> format_list[], <span class="keyword">const</span> <span class="keywordtype">char</span> * file_type_string) <a name="l00055"></a>00055 { <a name="l00056"></a>00056 <span class="keyword">enum</span> <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de">LibofxFileFormat</a> retval = <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de6ce26a62afab55d7606ad4e92428b30c">UNKNOWN</a>; <a name="l00057"></a>00057 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; <a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>[i].<a class="code" href="structLibofxFileFormatInfo.html#e4b1aa0050ffdc43ce68f07e6330d2f7">format</a>!=<a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de990cd45e93be2b4661f3499e0a658a33">LAST</a>; i++) <a name="l00058"></a>00058 { <a name="l00059"></a>00059 <span class="keywordflow">if</span>(strcmp(<a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>[i].format_name, file_type_string)==0) <a name="l00060"></a>00060 { <a name="l00061"></a>00061 retval = <a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>[i].format; <a name="l00062"></a>00062 } <a name="l00063"></a>00063 } <a name="l00064"></a>00064 <span class="keywordflow">return</span> retval; <a name="l00065"></a>00065 } <a name="l00066"></a>00066 <a name="l00067"></a><a class="code" href="libofx-0_89_81_2inc_2libofx_8h.html#f5c4a0387f06b5cccd483a1fd392f805">00067</a> <a class="code" href="inc_2libofx_8h.html#47f3130aac7543264fac840fb657cc3d">CFCT</a> <span class="keywordtype">int</span> <a class="code" href="inc_2libofx_8h.html#f5c4a0387f06b5cccd483a1fd392f805">libofx_proc_file</a>(<a class="code" href="inc_2libofx_8h.html#cb05196108aab07092d1b4931577b87c">LibofxContextPtr</a> p_libofx_context, <span class="keyword">const</span> <span class="keywordtype">char</span> * p_filename, <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de">LibofxFileFormat</a> p_file_type) <a name="l00068"></a>00068 { <a name="l00069"></a>00069 <a class="code" href="classLibofxContext.html">LibofxContext</a> * libofx_context = (<a class="code" href="classLibofxContext.html">LibofxContext</a> *) p_libofx_context; <a name="l00070"></a>00070 <a name="l00071"></a>00071 <span class="keywordflow">if</span>(p_file_type==<a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de9e87dd56d47acdcc5f86eedf6ae0f7a0">AUTODETECT</a>) <a name="l00072"></a>00072 { <a name="l00073"></a>00073 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f69748005382152808a72b1a9177d9dc806">INFO</a>, string(<span class="stringliteral">"libofx_proc_file(): File format not specified, autodecting..."</span>)); <a name="l00074"></a>00074 libofx_context-><a class="code" href="classLibofxContext.html#4d2c3cd388505cb022910f1a90add712">setCurrentFileType</a>(<a class="code" href="file__preproc_8cpp.html#34e14069654b16390599d24154b1f8d0">libofx_detect_file_type</a>(p_filename)); <a name="l00075"></a>00075 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(INFO, string(<span class="stringliteral">"libofx_proc_file(): Detected file format: "</span>)+ <a name="l00076"></a>00076 <a class="code" href="inc_2libofx_8h.html#efc89267742d989b5f17708076807b5e">libofx_get_file_format_description</a>(<a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>, <a name="l00077"></a>00077 libofx_context-><a class="code" href="classLibofxContext.html#0b9abc65fe4c7f063da8aa5fb769a4e8">currentFileType</a>() )); <a name="l00078"></a>00078 } <a name="l00079"></a>00079 <span class="keywordflow">else</span> <a name="l00080"></a>00080 { <a name="l00081"></a>00081 libofx_context-><a class="code" href="classLibofxContext.html#4d2c3cd388505cb022910f1a90add712">setCurrentFileType</a>(<a class="code" href="file__preproc_8cpp.html#34e14069654b16390599d24154b1f8d0">libofx_detect_file_type</a>(p_filename)); <a name="l00082"></a>00082 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f69748005382152808a72b1a9177d9dc806">INFO</a>, <a name="l00083"></a>00083 string(<span class="stringliteral">"libofx_proc_file(): File format forced to: "</span>)+ <a name="l00084"></a>00084 <a class="code" href="inc_2libofx_8h.html#efc89267742d989b5f17708076807b5e">libofx_get_file_format_description</a>(<a class="code" href="inc_2libofx_8h.html#b1ad431f1a4db406c34db2833ca80748">LibofxImportFormatList</a>, <a name="l00085"></a>00085 libofx_context-><a class="code" href="classLibofxContext.html#0b9abc65fe4c7f063da8aa5fb769a4e8">currentFileType</a>() )); <a name="l00086"></a>00086 } <a name="l00087"></a>00087 <a name="l00088"></a>00088 <span class="keywordflow">switch</span> (libofx_context-><a class="code" href="classLibofxContext.html#0b9abc65fe4c7f063da8aa5fb769a4e8">currentFileType</a>()) <a name="l00089"></a>00089 { <a name="l00090"></a>00090 <span class="keywordflow">case</span> <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de08ab55dc6141faaa0b67a401e9a8801d">OFX</a>: <a class="code" href="ofx__preproc_8cpp.html#f2eca18ef5a500e7f76ed180d0af8a0a">ofx_proc_file</a>(libofx_context, p_filename); <a name="l00091"></a>00091 <span class="keywordflow">break</span>; <a name="l00092"></a>00092 <span class="keywordflow">case</span> <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736debd42446b99427483c7f3bee70bb217f7">OFC</a>: <a class="code" href="ofx__preproc_8cpp.html#f2eca18ef5a500e7f76ed180d0af8a0a">ofx_proc_file</a>(libofx_context, p_filename); <a name="l00093"></a>00093 <span class="keywordflow">break</span>; <a name="l00094"></a>00094 <span class="keywordflow">default</span>: <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f692fd6f336d08340583bd620a7f5694c90">ERROR</a>, string(<span class="stringliteral">"libofx_proc_file(): Detected file format not yet supported ou couldn't detect file format; aborting."</span>)); <a name="l00095"></a>00095 } <a name="l00096"></a>00096 <span class="keywordflow">return</span> 0; <a name="l00097"></a>00097 } <a name="l00098"></a>00098 <a name="l00099"></a><a class="code" href="fx-0_89_81_2lib_2file__preproc_8hh.html#34e14069654b16390599d24154b1f8d0">00099</a> <span class="keyword">enum</span> <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de">LibofxFileFormat</a> <a class="code" href="file__preproc_8cpp.html#34e14069654b16390599d24154b1f8d0">libofx_detect_file_type</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> * p_filename) <a name="l00100"></a>00100 { <a name="l00101"></a>00101 <span class="keyword">enum</span> <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de">LibofxFileFormat</a> retval = <a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de6ce26a62afab55d7606ad4e92428b30c">UNKNOWN</a>; <a name="l00102"></a>00102 ifstream input_file; <a name="l00103"></a>00103 <span class="keywordtype">char</span> buffer[<a class="code" href="file__preproc_8cpp.html#3a4f15870bd5573d14fceb17c65162ee">READ_BUFFER_SIZE</a>]; <a name="l00104"></a>00104 string s_buffer; <a name="l00105"></a>00105 <span class="keywordtype">bool</span> type_found=<span class="keyword">false</span>; <a name="l00106"></a>00106 <a name="l00107"></a>00107 <span class="keywordflow">if</span>(p_filename!=NULL&&strcmp(p_filename,<span class="stringliteral">""</span>)!=0) <a name="l00108"></a>00108 { <a name="l00109"></a>00109 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f690593585da9181e972974c1274d8f2b4f">DEBUG</a>, string(<span class="stringliteral">"libofx_detect_file_type():Opening file: "</span>)+p_filename); <a name="l00110"></a>00110 <a name="l00111"></a>00111 input_file.open(p_filename); <a name="l00112"></a>00112 <a name="l00113"></a>00113 <span class="keywordflow">if</span>(!input_file) <a name="l00114"></a>00114 { <a name="l00115"></a>00115 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f692fd6f336d08340583bd620a7f5694c90">ERROR</a>,<span class="stringliteral">"libofx_detect_file_type():Unable to open the input file "</span>+string(p_filename)); <a name="l00116"></a>00116 <span class="keywordflow">return</span> retval; <a name="l00117"></a>00117 } <a name="l00118"></a>00118 <span class="keywordflow">else</span> <a name="l00119"></a>00119 { <a name="l00120"></a>00120 <span class="keywordflow">do</span> <a name="l00121"></a>00121 { <a name="l00122"></a>00122 input_file.getline(buffer, <span class="keyword">sizeof</span>(buffer),<span class="charliteral">'\n'</span>); <a name="l00123"></a>00123 <span class="comment">//cout<<buffer<<"\n";</span> <a name="l00124"></a>00124 s_buffer.assign(buffer); <a name="l00125"></a>00125 <span class="comment">//cout<<"input_file.gcount(): "<<input_file.gcount()<<" sizeof(buffer): "<<sizeof(buffer)<<endl;</span> <a name="l00126"></a>00126 <span class="keywordflow">if</span>(input_file.gcount()<(<span class="keyword">sizeof</span>(buffer)-1)) <a name="l00127"></a>00127 { <a name="l00128"></a>00128 s_buffer.append(<span class="stringliteral">"\n"</span>);<span class="comment">//Just in case...</span> <a name="l00129"></a>00129 } <a name="l00130"></a>00130 <span class="keywordflow">else</span> <span class="keywordflow">if</span>( !input_file.eof()&&input_file.fail()) <a name="l00131"></a>00131 { <a name="l00132"></a>00132 input_file.clear(); <a name="l00133"></a>00133 } <a name="l00134"></a>00134 <a name="l00135"></a>00135 <span class="keywordflow">if</span>(s_buffer.find(<span class="stringliteral">"<OFX>"</span>)!=string::npos||s_buffer.find(<span class="stringliteral">"<ofx>"</span>)!=string::npos) <a name="l00136"></a>00136 { <a name="l00137"></a>00137 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f690593585da9181e972974c1274d8f2b4f">DEBUG</a>,<span class="stringliteral">"libofx_detect_file_type():<OFX> tag has been found"</span>); <a name="l00138"></a>00138 retval=<a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de08ab55dc6141faaa0b67a401e9a8801d">OFX</a>; <a name="l00139"></a>00139 type_found=<span class="keyword">true</span>; <a name="l00140"></a>00140 } <a name="l00141"></a>00141 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(s_buffer.find(<span class="stringliteral">"<OFC>"</span>)!=string::npos||s_buffer.find(<span class="stringliteral">"<ofc>"</span>)!=string::npos) <a name="l00142"></a>00142 { <a name="l00143"></a>00143 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f690593585da9181e972974c1274d8f2b4f">DEBUG</a>,<span class="stringliteral">"libofx_detect_file_type():<OFC> tag has been found"</span>); <a name="l00144"></a>00144 retval=<a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736debd42446b99427483c7f3bee70bb217f7">OFC</a>; <a name="l00145"></a>00145 type_found=<span class="keyword">true</span>; <a name="l00146"></a>00146 } <a name="l00147"></a>00147 <a name="l00148"></a>00148 }<span class="keywordflow">while</span>(type_found==<span class="keyword">false</span>&&!input_file.eof()&&!input_file.bad()); <a name="l00149"></a>00149 } <a name="l00150"></a>00150 input_file.close(); <a name="l00151"></a>00151 } <a name="l00152"></a>00152 <span class="keywordflow">else</span>{ <a name="l00153"></a>00153 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f692fd6f336d08340583bd620a7f5694c90">ERROR</a>,<span class="stringliteral">"libofx_detect_file_type(): No input file specified"</span>); <a name="l00154"></a>00154 } <a name="l00155"></a>00155 <span class="keywordflow">if</span>(retval==<a class="code" href="inc_2libofx_8h.html#954c5e33f48105c5d932d4842e7736de6ce26a62afab55d7606ad4e92428b30c">UNKNOWN</a>) <a name="l00156"></a>00156 <a class="code" href="messages_8cpp.html#6af6947c1ec6fcbefd793f80c26d90fa">message_out</a>(<a class="code" href="messages_8hh.html#7cd03dafa59895bc306bf220b7b85f692fd6f336d08340583bd620a7f5694c90">ERROR</a>,<span class="stringliteral">"libofx_detect_file_type(): Failed to identify input file format"</span>); <a name="l00157"></a>00157 <span class="keywordflow">return</span> retval; <a name="l00158"></a>00158 } <a name="l00159"></a>00159 <a name="l00160"></a>00160 <a name="l00161"></a>00161 <a name="l00162"></a>00162 <a name="l00163"></a>00163 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Feb 9 21:21:59 2009 for LibOFX by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.0 </small></address> </body> </html>