Wexus2 0.20
wexus2.src/wexus/ActiveFile.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_FILERECORD_H__
00009 #define __INCLUDED_WEXUS_FILERECORD_H__
00010 
00011 #include <QString>
00012 #include <QRegExp>
00013 #include <QDirIterator>
00014 
00015 #include <wexus/TR1.h>
00016 #include <wexus/IDAble.h>
00017 
00018 namespace wexus
00019 {
00020   class ActiveFile;
00021 }
00022 
00023 /**
00024   * A database-record like representation
00025   * of a file. Instead of a table, it uses a directory
00026   * (plus a filtering regexp). Instead of record,
00027   * it uses an individual file.
00028   *
00029   * This concept is still being fleshed out. Future
00030   * ideas include:
00031   *
00032   * a) filename->field like decomposition based on regexp
00033   * (with custom user-made field lists)
00034   *
00035   * b) a query system for a)
00036   *
00037   * c) more file load types, like images and xml.
00038   *
00039   * d) rename this to ActiveFile?
00040   *
00041   * @author Aleksander Demko
00042   */ 
00043 class wexus::ActiveFile : public wexus::IDAble
00044 {
00045   public:
00046     QString id;       /// just the filename, no directory
00047 
00048   public:
00049     /**
00050      * Constructor
00051      *
00052      * @author Aleksander Demko
00053      */ 
00054     ActiveFile(const QString &dirname, const QRegExp &regexp);
00055 
00056     /// implementation
00057     virtual QVariant getIDAsVariant(void);
00058 
00059     /**
00060      * Security routine.
00061      * throws if the filename:
00062      *  - is empty
00063      *  - contains bad chars, like / or \
00064      *  - starts with a .
00065      *  - doesnt pass the reg ex
00066      *
00067      * @author Aleksander Demko
00068      */ 
00069     void checkFileName(const QString &filename);
00070 
00071     /**
00072      * Runs checkFileName on id
00073      *
00074      * @author Aleksander Demko
00075      */
00076     void checkFileName(void) { checkFileName(id); }
00077 
00078     /**
00079      * Finds the record that that has the given
00080      * value as its primary key.
00081      * Throws an exception on not-found.
00082      *
00083      * @author Aleksander Demko
00084      */
00085     void find(const QVariant &keyVal);
00086 
00087     /**
00088      * Same as find(), except returns false on failure.
00089      * find() raises an exception.
00090      *
00091      * @author Aleksander Demko
00092      */
00093     bool exists(const QVariant &keyVal);
00094 
00095     /**
00096      * Demand load the curren file. Throws on errors.
00097      *
00098      * @author Aleksander Demko
00099      */ 
00100     QByteArray & asByteArray(void);
00101 
00102     /**
00103      * Returns all the records.
00104      *
00105      * @author Aleksander Demko
00106      */ 
00107     void all(bool reverseOrder = false);
00108 
00109     /**
00110      * Returns the next record in file list, or false if
00111      * done.
00112      *
00113      * @author Aleksander Demko
00114      */ 
00115     bool next(void);
00116 
00117   protected:
00118     /**
00119      * This is called after loading a record.
00120      * id (the filename) is already set up entering this function.
00121      * You can also examine the full filename parameter.
00122      *
00123      * Decendants should be able to work with empty-string ids
00124      * and can throw exceptions on errors.
00125      *
00126      * TODO in the future, this might be implemented
00127      * in a more automatic/regex manner using .eh-like files.
00128      *
00129      * The default implementation does nothing but return true;
00130      *
00131      * @return true on success (false causes this record to not
00132      * be loaded)
00133      * @author Aleksander Demko
00134      */ 
00135     virtual bool onLoad(const QString &fullfilename);
00136 
00137   protected:
00138     struct DirSpec
00139     {
00140       QString dirname;
00141       QRegExp regexp;
00142     };
00143 
00144     std::shared_ptr<DirSpec> dm_dirspec;
00145 
00146     struct DataSpec
00147     {
00148       QString filename;
00149       QByteArray bytearray;
00150     };
00151     // the data, if any
00152     // for multi-types, this will have to be a union-like thing
00153     std::shared_ptr<DataSpec> dm_data;
00154 
00155     struct IteratorSpec
00156     {
00157       QStringList filenames;
00158       QStringList::const_iterator iterator;
00159     };
00160 
00161     // current iteration, if any
00162     std::shared_ptr<IteratorSpec> dm_iterator;
00163 };
00164 
00165 #endif
00166 
 All Classes Namespaces Functions Variables Enumerations Enumerator