00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef FILE_DESCRIPTOR_HPP
00021 #define FILE_DESCRIPTOR_HPP
00022
00023 #include <ErrorHandler.hpp>
00024 #include <StreamCenter.hpp>
00025
00026 #include <cstring>
00027
00035 class FileDescriptor
00036 {
00037 public:
00038 enum FormatType {
00039 am_fmt,
00040 gmsh,
00041 mesh,
00042 medit,
00043 raw,
00044 vtk
00045 };
00046
00047 enum FileType {
00048 binary,
00049 dos,
00051 formatDefault,
00053 mac,
00055 unices
00057 };
00058
00066 static FileDescriptor::FormatType
00067 formatType(const char* c)
00068 {
00069 if (std::strcmp(c,"gmsh") == 0) {
00070 return FileDescriptor::gmsh;
00071 }
00072 if (std::strcmp(c,"mesh") == 0) {
00073 return FileDescriptor::mesh;
00074 }
00075 if (std::strcmp(c,"medit") == 0) {
00076 return FileDescriptor::medit;
00077 }
00078 if (std::strcmp(c,"opendx") == 0) {
00079 fferr(1) << "warning: \"opendx\" is deprecated, use \"raw\" keyword instead\n";
00080 return FileDescriptor::raw;
00081 }
00082 if (std::strcmp(c,"raw") == 0) {
00083 return FileDescriptor::raw;
00084 }
00085 if (std::strcmp(c,"vtk") == 0) {
00086 return FileDescriptor::vtk;
00087 }
00088 if (std::strcmp(c,"am_fmt") == 0) {
00089 return FileDescriptor::am_fmt;
00090 }
00091
00092 throw ErrorHandler(__FILE__,__LINE__,
00093 "Unknown File Type: '"+std::string(c)+"'",
00094 ErrorHandler::unexpected);
00095 return FileDescriptor::raw;
00096 }
00097
00105 static FileDescriptor::FileType
00106 fileType(const char* c)
00107 {
00108 if (std::strcmp(c,"unix") == 0) {
00109 return FileDescriptor::unices;
00110 }
00111 if (std::strcmp(c,"dos") == 0) {
00112 return FileDescriptor::dos;
00113 }
00114 if (std::strcmp(c,"mac") == 0) {
00115 return FileDescriptor::mac;
00116 }
00117 if (std::strcmp(c,"binary") == 0) {
00118 return FileDescriptor::binary;
00119 }
00120 throw ErrorHandler(__FILE__,__LINE__,
00121 "unknown file type",
00122 ErrorHandler::unexpected);
00123 return FileDescriptor::unices;
00124 }
00125
00126 private:
00127 const FileDescriptor::FormatType
00128 __formatType;
00129 const FileDescriptor::FileType
00130 __fileType;
00132 public:
00138 const FileDescriptor::FormatType&
00139 format() const
00140 {
00141 return __formatType;
00142 }
00143
00149 const FileDescriptor::FileType&
00150 type() const
00151 {
00152 return __fileType;
00153 }
00154
00160 const std::string
00161 cr() const
00162 {
00163 std::string CR;
00164
00165 switch (__fileType) {
00166 case FileDescriptor::binary:
00167 case FileDescriptor::formatDefault:
00168 case FileDescriptor::unices: {
00169 CR = '\n';
00170 break;
00171 }
00172 case FileDescriptor::dos: {
00173 #ifndef __MINGW32__
00174 CR = "\r\n";
00175 #else // __MINGW32__
00176 CR = "\n";
00177 #endif // __MINGW32__
00178 break;
00179 }
00180 case FileDescriptor::mac: {
00181 CR = '\r';
00182 break;
00183 }
00184 default: {
00185 throw ErrorHandler(__FILE__,__LINE__,
00186 "unknown file type",
00187 ErrorHandler::unexpected);
00188 }
00189 }
00190 return CR;
00191 }
00192
00198 std::string formatName() const
00199 {
00200 switch (__formatType) {
00201 case am_fmt:
00202 return "am_fmt";
00203 case gmsh:
00204 return "gmsh";
00205 case mesh:
00206 return "mesh";
00207 case medit:
00208 return "medit";
00209 case raw:
00210 return "raw";
00211 case vtk:
00212 return "vtk";
00213 default:
00214 throw ErrorHandler(__FILE__,__LINE__,
00215 "unknown file format",
00216 ErrorHandler::unexpected);
00217 return "error";
00218 }
00219 }
00220
00226 std::string fileTypeName() const
00227 {
00228 switch (__fileType) {
00229 case binary: {
00230 return "binary";
00231 }
00232 case dos: {
00233 return "dos";
00234 }
00235 case formatDefault: {
00236 return "unspecified";
00237 }
00238 case mac: {
00239 return "mac";
00240 }
00241 case unices: {
00242 return "unix";
00243 }
00244 default: {
00245 throw ErrorHandler(__FILE__,__LINE__,
00246 "unknown file type",
00247 ErrorHandler::unexpected);
00248 return "error";
00249 }
00250 }
00251 }
00252
00259 FileDescriptor(const FileDescriptor::FormatType& format,
00260 const FileDescriptor::FileType& type)
00261 : __formatType(format),
00262 __fileType(type)
00263 {
00264 ;
00265 }
00266
00272 FileDescriptor(const FileDescriptor& f)
00273 : __formatType(f.__formatType),
00274 __fileType(f.__fileType)
00275 {
00276 ;
00277 }
00278
00283 ~FileDescriptor()
00284 {
00285 ;
00286 }
00287 };
00288
00289 #endif // FILE_DESCRIPTOR_HPP