diff mde/mergetag/write.d @ 11:b940f267419e

Options class created & changes to mergetag exception messages. Options class created (barebones). Loading/saving from Init. Init no longer runs cleanup functions after initialisation failure. Improved mergetag exception messages & error reporting. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 21 Feb 2008 09:05:33 +0000
parents 4c3575400769
children
line wrap: on
line diff
--- a/mde/mergetag/write.d	Mon Feb 18 11:54:56 2008 +0000
+++ b/mde/mergetag/write.d	Thu Feb 21 09:05:33 2008 +0000
@@ -51,12 +51,27 @@
  * method parameter and using the filename extension as a fallback.
  *
  * An exception is thrown if neither test can deduce the writing method.
+ *
+ * Use as:
+ * -----------------------
+ * DataSet dataset; // contains data to write
+ * IWriter foo;
+ * try {
+ *   foo = makeWriter("foo.mtt", dataset);
+ *   foo.write();
+ * }
+ * catch (MTException) {}
+ * -----------------------
+ *
+ * Throws:
+ *  MTFileFormatException if unable to determine writing format or use requested format.
  */
 IWriter makeWriter (char[] path, DataSet dataset = null, WriterMethod method = WriterMethod.Unspecified) {
     return makeWriter (new FilePath (path), dataset, method);
 }
 /** ditto */
-IWriter makeWriter (PathView path, DataSet dataset = null, WriterMethod method = WriterMethod.Unspecified) {
+IWriter makeWriter (PathView path, DataSet dataset = null, WriterMethod method = WriterMethod.Unspecified)
+{
     void throwMTErr (char[] msg, Exception exc = new MTException) {
         logger.error (msg);
         throw exc;
@@ -70,6 +85,7 @@
     if (method == WriterMethod.Binary) throwMTErr ("Binary writing not supported yet!", new MTFileFormatException);
     else if (method == WriterMethod.Text) return new TextWriter (path, dataset);
     else if (method == WriterMethod.Both) throwMTErr ("Dual writing not supported yet!", new MTFileFormatException);
+    else debug throwMTErr ("Bad value of method", new MTFileFormatException);
 }
 
 /// Interface for methods and data necessarily available in TextWriter and/or BinaryWriter.
@@ -92,8 +108,17 @@
  * Class to write a dataset to a file.
  *
  * Files are only actually open for writing while the write() method is running.
+ *
+ * Throws:
+ *  $(TABLE
+ *  $(TR $(TH Exception) $(TH Thrown when))
+ *  $(TR $(TD MTNoDataSetException) $(TD No dataset is available to write from))
+ *  $(TR $(TD MTFileIOException) $(TD An error occurs while attemting to write the file))
+ *  $(TR $(TD MTException) $(TD An unexpected error occurs))
+ *  )
+ * Note that all exceptions extend MTException; unlike Reader exceptions don't block further calls.
  */
-scope class TextWriter : IWriter
+class TextWriter : IWriter
 {
 //BEGIN DATA
     /// Get or set the DataSet.
@@ -112,11 +137,13 @@
     /* The container where data is written from. */
     DataSet _dataset;
     
-    PathView path;
+    PathView _path;
 //END DATA
     
 //BEGIN CTOR / DTOR
-    /** Tries to open file path for writing.
+    /** Prepares to open file path for writing.
+    *
+    * The call doesn't actually execute any code so cannot fail (unless out of memory).
     *
     * Params:
     * path = The name or FilePath of the file to open.
@@ -124,12 +151,12 @@
     * dataset_ = If null create a new DataSet, else use existing DataSet *dataset_ and merge read
     *     data into it.
     */
-    public this (char[] _path, DataSet ds = null) {
-        this (new FilePath (_path), ds);
+    public this (char[] path, DataSet ds = null) {
+        this (new FilePath (path), ds);
     }
     /** ditto */
-    public this (PathView _path, DataSet ds = null) {
-        path = _path;
+    public this (PathView path, DataSet ds = null) {
+        _path = path;
         _dataset = ds;
     }
 //END CTOR / DTOR
@@ -141,22 +168,17 @@
      * header should be written only once. This behaviour could, for instance, be used to write
      * multiple DataSets into one file without firstly merging them. Note that this behaviour may
      * be changed when binary support is added.
-     *
-     * Throws:
-     *	MTNoDataSetException if the dataset is null,
-     *	MTFileIOException if a file IO error occurs,
-     *	MTException on any other exception (unexpected).
      */
     public void write ()
     {
-        if (!_dataset) throw new MTNoDataSetException ("write(): Dataset needed to write from!");
+        if (!_dataset) throwMTErr ("write(): no Dataset available to write from!", new MTNoDataSetException ());
         
         try {
             FileConduit conduit;	// actual conduit; don't use directly when there's content in the buffer
             IBuffer buffer;		// write strings directly to this (use opCall(void[]) )
             
             // Open a conduit on the file:
-            conduit = new FileConduit (path, FileConduit.WriteCreate);
+            conduit = new FileConduit (_path, FileConduit.WriteCreate);
             scope(exit) conduit.close();
             
             buffer = new Buffer(conduit);	// And a buffer