X3D libraries
The libraries to work with X3D dataset

io.h

Go to the documentation of this file.
00001 /*
00002  *    libx3d io -- definitions to manage the X3D I/O
00003  *    Copyright (C) 2005  Made to Order Software, Corp.
00004  *
00005  *    This library is free software; you can redistribute it and/or
00006  *    modify it under the terms of the GNU Lesser General Public
00007  *    License as published by the Free Software Foundation; either
00008  *    version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *    This library is distributed in the hope that it will be useful,
00011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *    Lesser General Public License for more details.
00014  *
00015  *    You should have received a copy of the GNU Lesser General Public
00016  *    License along with this library; if not, write to the Free Software
00017  *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * File: io/include/x3d/io.h
00020  * License: GNU Lesser General Public License
00022  * $Revision: 1.14 $
00023  * $Date: 2005/11/29 05:52:10 $
00024  *
00025  * $Log: io.h,v $
00026  * Revision 1.14  2005/11/29 05:52:10  alexis
00027  * Mainly documented the entire I/O library.
00028  * Also added the Append(), Update() and Attach() functions.
00029  *
00030  * Revision 1.13  2005/11/28 12:09:02  alexis
00031  * Many changes so I could generate a list of files with their brief comment,
00032  * filename, license, size, number of lines, and last modification date.
00033  *
00034  * Revision 1.12  2005/11/28 03:31:19  alexis
00035  * Added the License: tag
00036  * Fixed many File: tags
00037  *
00038  * Revision 1.11  2005/11/28 01:33:54  alexis
00039  * Fixed the name of the library to libx3dio
00040  *
00041  * Revision 1.10  2005/11/28 01:26:53  alexis
00042  * Fixed the comment about the content of this file
00043  *
00044  * Revision 1.9  2005/11/27 05:01:52  alexis
00045  * Added the version
00046  *
00047  * Revision 1.8  2005/11/26 23:42:06  alexis
00048  * Added const to Write() and Save() inputs
00049  *
00050  * Revision 1.7  2005/11/26 09:45:24  alexis
00051  * Added 2 errors
00052  *
00053  * Revision 1.6  2005/11/26 02:30:48  alexis
00054  * Fixed the signature of the Save() in the Writer.
00055  * Added the ErrorCallback destructor
00056  *
00057  * Revision 1.5  2005/11/25 08:14:37  alexis
00058  * The first compiling version of the libx3dio library.
00059  *
00060  * Revision 1.4  2005/11/25 06:44:35  alexis
00061  * Added some errors
00062  *
00063  * Revision 1.3  2005/11/25 01:50:48  alexis
00064  * Still working on it... saving current version...
00065  *
00066  * Revision 1.2  2005/11/25 01:20:48  alexis
00067  * Added an Error callback
00068  *
00069  * Revision 1.1  2005/11/25 00:39:39  alexis
00070  * The stream handling and XML header
00071  *
00072  *
00073  */
00074 #ifndef __LIBX3D_IO_H__
00075 #define __LIBX3D_IO_H__
00076 
00077 #include        <x3d/node.h>
00078 #include        <stdarg.h>
00079 
00080 #define X3D_IO_VERSION          0.1
00081 
00082 #define X3D_IO_RELEASE          0
00083 #define X3D_IO_REVISION         1
00084 
00085 
00086 /// The I/O handling of the X3D libraries
00087 namespace x3dio
00088 {
00089 
00090 
00091 /** \file io.h
00092  *
00093  * \brief This header file includes the basic I/O interfaces common
00094  * to all the I/O implementations.
00095  *
00096  * This file includes interfaces such as Stream and ErrorCallback
00097  * which are derived to implement Stream's, Reader's, Writer's and
00098  * error handlers.
00099  * 
00100  * If you do not need to handle any specific reader or writer (i.e.
00101  * if just using a Reader interface pointer is enough) then you
00102  * should only include this file.
00103  */
00104 
00105 
00106 
00107 
00108 /// Interface to implement to read and write some file (regular file, HTTP, FTP, etc.)
00109 class Stream
00110 {
00111 public:
00112         virtual                 ~Stream();
00113 
00114         virtual bool            IsOpen(void) const = 0;
00115 
00116         virtual bool            Open(const char *name) = 0;
00117         virtual bool            Create(const char *name) = 0;
00118         virtual bool            Update(const char *name, bool create = true) = 0;
00119         virtual bool            Append(const char *name) = 0;
00120         virtual bool            Close() = 0;
00121 
00122         virtual int             Seek(long offset, int whence) = 0;
00123         virtual int             Read(void *buf, size_t size) = 0;
00124         virtual int             Write(const void *buf, size_t size) = 0;
00125 };
00126 
00127 
00128 
00129 /// An implementation of the Stream interface to work with standard local files (FILE *)
00130 class FileStream : public Stream
00131 {
00132 public:
00133                                 FileStream();
00134         virtual                 ~FileStream();
00135 
00136         virtual bool            IsOpen(void) const;
00137 
00138         virtual bool            Open(const char *name);
00139         virtual bool            Create(const char *name);
00140         virtual bool            Update(const char *name, bool create = true);
00141         virtual bool            Append(const char *name);
00142         virtual bool            Close();
00143 
00144         bool                    Attach(FILE *file, bool manage = true);
00145 
00146         virtual int             Seek(long offset, int whence);
00147         virtual int             Read(void *buf, size_t size);
00148         virtual int             Write(const void *buf, size_t size);
00149 
00150 private:
00151         FILE *                  f_file;
00152         bool                    f_manage;
00153 };
00154 
00155 
00156 
00157 
00158 /// An interface to send error messages to the caller of readers and writers
00159 class ErrorCallback
00160 {
00161 public:
00162         enum errcode_t {
00163                                                 /// not an error
00164                 ERR_NONE,
00165                                                 /// name too long, bad string
00166                 ERR_BAD_ATTRIBUTE,
00167                                                 /// usually unterminated comment
00168                 ERR_BAD_COMMENT,
00169                                                 /// DOCTYPE which is NOT named X3D
00170                 ERR_BAD_FORMAT,
00171                                                 /// most likely unterminated
00172                 ERR_BAD_TAG,
00173                                                 /// no containerField name...
00174                 ERR_MISSING_CONTAINER_FIELD,
00175                                                 /// cannot find <?xml ... ?>
00176                 ERR_NO_HEADER,
00177                                                 /// only scripts accept a <![CDATA[...]]>
00178                 ERR_UNEXPECTED_CDATA,
00179                                                 /// containerField name not recognized...
00180                 ERR_UNKNOWN_CONTAINER_FIELD,
00181                                                 /// do not know how to create this node
00182                 ERR_UNKNOWN_TYPE,
00183                                                 /// some error happened while reading/writing the stream
00184                 ERR_IO,
00185 
00186                 /// end of list
00187                 ERR_max
00188         };
00189 
00190         virtual                 ~ErrorCallback();
00191 
00192         virtual void            Error(errcode_t errcode, const char *message) = 0;
00193 
00194         void                    ErrorVMsg(errcode_t errcode, const char *format, va_list ap);
00195         void                    ErrorMsg(errcode_t errcode, const char *format, ...);
00196 };
00197 
00198 
00199 /// The reader interface, implemented by any object which can read a stream and convert it in a tree of x3d nodes
00200 class Reader
00201 {
00202 public:
00203         virtual                 ~Reader();
00204 
00205         virtual x3d::SFNodePtr  Load(Stream *stream, ErrorCallback *callback = 0) = 0;
00206 };
00207 
00208 /// The writer interface, implemented by any object which can convert a tree of x3d nodes to write it to a stream
00209 class Writer
00210 {
00211 public:
00212         virtual                 ~Writer();
00213 
00214         virtual bool            Save(Stream *stream, const x3d::SFNode* node, ErrorCallback *callback = 0) = 0;
00215 };
00216 
00217 
00218 
00219 
00220 };              // namespace x3dio
00221 
00222 
00223 
00224 // vim: ts=8
00225 #endif          // #ifndef __LIBX3D_IO_H__

00021 * $Id: io.h,v 1.14 2005/11/29 05:52:10 alexis Exp $