Code

patch [ 1503869 ] ODF formatting fix
authoracspike <acspike@users.sourceforge.net>
Tue, 13 Jun 2006 02:13:02 +0000 (02:13 +0000)
committeracspike <acspike@users.sourceforge.net>
Tue, 13 Jun 2006 02:13:02 +0000 (02:13 +0000)
13 files changed:
src/dom/io/base64stream.cpp
src/dom/io/base64stream.h
src/dom/io/bufferstream.cpp
src/dom/io/bufferstream.h
src/dom/io/domstream.cpp
src/dom/io/domstream.h
src/dom/io/gzipstream.cpp
src/dom/io/gzipstream.h
src/dom/io/stringstream.cpp
src/dom/io/stringstream.h
src/dom/io/uristream.cpp
src/dom/io/uristream.h
src/extension/internal/odf.cpp

index f71532e8b97937cc2473f0b7927a78c7d8539bc8..2cab2b3f4d2c305db3ddc65dddba263a1b38a89a 100644 (file)
@@ -292,12 +292,12 @@ void Base64OutputStream::putCh(int ch)
 /**
  * Writes the specified byte to this output stream.
  */
-void Base64OutputStream::put(XMLCh ch)
+int Base64OutputStream::put(XMLCh ch)
 {
     if (closed)
         {
         //probably throw an exception here
-        return;
+        return -1;
         }
 
     outBuf   <<=  8;
@@ -324,6 +324,8 @@ void Base64OutputStream::put(XMLCh ch)
         bitCount = 0;
         outBuf   = 0L;
         }
+
+    return 1;
 }
 
 
index 23ec1ce07b801446eda9566a055180756ac83670..c6d0ad35d1147688648cb087711dc688d74d9121 100644 (file)
@@ -108,7 +108,7 @@ public:
 
     virtual void flush();
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
     /**
      * Sets the maximum line length for base64 output.  If
index 4664b07e91359f8fdb296d896965dce07fa1d42a..6c3956a60cd4fa3be6d9e6d6b2c5b3fb6f3eb070 100644 (file)
@@ -146,11 +146,12 @@ void BufferOutputStream::flush()
 /**
  * Writes the specified byte to this output stream.
  */
-void BufferOutputStream::put(XMLCh ch)
+int BufferOutputStream::put(XMLCh ch)
 {
     if (closed)
-        return;
+        return -1;
     buffer.push_back(ch);
+    return 1;
 }
 
 
index a8f7ea31964be079edfbbd8fbd67709b41ae0965..bdf4eb2ab42cb4f16c8ccac8d2e156589431a17d 100644 (file)
@@ -100,7 +100,7 @@ public:
 
     virtual void flush();
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
     virtual std::vector<unsigned char> &getBuffer()
         { return buffer; }
index 3ffba53feefab4f27719624cd18b41b7f6b0425f..7b0a7e1d2b6cfa1bd6e14d5aef2350cca49f1b27 100644 (file)
@@ -34,6 +34,7 @@
  *
  */
 
+#include <math.h>
 #include <stdarg.h>
 
 #include "domstream.h"
@@ -65,6 +66,305 @@ void pipeStream(InputStream &source, OutputStream &dest)
     dest.flush();
 }
 
+
+
+//#########################################################################
+//# F O R M A T T E D    P R I N T I N G
+//#########################################################################
+
+static char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
+
+static int dprintInt(Writer &outs,
+                     long arg, int base,
+                     int flag, int width, int precision)
+{
+
+    DOMString buf;
+
+    //### Get the digits
+    while (arg > 0)
+        {
+        int ch = arg % base;
+        buf.insert(buf.begin(), digits[ch]);        
+        arg /= base;        
+        }
+
+    if (flag == '#' && base == 16)
+        {
+        buf.insert(buf.begin(), 'x');
+        buf.insert(buf.begin(), '0');
+        }
+
+    //### Output the result
+    for (unsigned int i=0 ; i<buf.size() ; i++)
+        {
+        if (outs.put(buf[i]) < 0)
+            return -1;
+        }
+
+    return 1;
+}
+
+
+
+static int dprintDouble(Writer &outs,
+                        double val, 
+                        int flag, int width, int precision)
+{
+
+    DOMString buf;
+
+    //printf("int:%f  frac:%f\n", intPart, fracPart);
+
+    bool negative = false;
+    if (val < 0)
+        {
+        negative = true;
+        val = -val;
+        }
+
+    int intDigits = 0;
+    double scale = 1.0;
+    while (scale < val)
+        {
+        intDigits++;
+        scale *= 10.0;
+        }
+
+    double intPart;
+    double fracPart = modf(val, &intPart);
+
+    if (precision <= 0)
+        precision = 5;
+
+    //### How many pad digits?
+    int pad = width - intDigits;
+    if (precision > 0)
+        pad -= precision + 1;
+    else if (flag == '#')
+        pad--;
+
+
+    //### Signs
+    if (negative)
+        buf.push_back('-');
+    else if (flag == '+')
+        buf.push_back('+');
+
+    //### Prefix pad
+    if (pad > 0 && flag == '0')
+        {
+        while (pad--)
+            buf.push_back('0');
+        }
+
+    //### Integer digits
+    intPart = (intPart + 0.1 ) / scale;    // turn 12345.678 to .12345678
+    while (intDigits--)
+        {
+        intPart *= 10.0;
+        double dig;
+        intPart = modf(intPart, &dig);
+        char ch = '0' + (int)dig;
+        buf.push_back(ch);
+        }
+
+    //### Decimal point
+    if (flag == '#' || precision > 0)
+        {
+        buf.push_back('.');
+        }    
+
+    //### Fractional digits
+    while (precision--)
+        {
+        fracPart *= 10.0;
+        double dig;
+        fracPart = modf(fracPart, &dig);
+        char ch = '0' + (int)dig;
+        buf.push_back(ch);
+        }
+
+    //### Left justify if requested
+    if (pad > 0 && flag == '-')
+        {
+        while (pad--)
+            buf.push_back(' ');
+        }
+
+    //### Output the result
+    for (unsigned int i=0 ; i<buf.size() ; i++)
+        {
+        if (outs.put(buf[i]) < 0)
+            return -1;
+        }
+    return 1;
+}
+
+static int dprintString(Writer &outs,
+                        char *arg, 
+                        int flags, int width, int precision)
+{
+    while (*arg)
+        {
+        if (outs.put(*arg++) < 0)
+            return -1;
+        }
+
+    return 1;
+}
+
+
+
+static char *getint(char *s, int *ret)
+{
+    bool has_sign = false;
+    int val = 0;
+    if (*s == '-')
+        {
+        has_sign = true;
+        s++;
+        }
+    while (*s >= '0' && *s <= '9')
+        {
+        val = val * 10 + (*s - '0');
+        s++;
+        }
+    if (has_sign)
+        val = -val;
+
+    *ret = val;
+
+    return s;
+}
+
+
+
+static int dprintf(Writer &outs, const char *fmt, va_list ap)
+{
+
+    char *s = (char *) fmt;
+
+    while (*s)
+        {
+        unsigned char ch = *s++;
+
+        if (ch != '%')
+            {
+            if (outs.put(ch)<0)
+                {
+                return -1;
+                }
+            }
+        else
+            //expecting  %[flag][width][.precision][length][char]
+            {
+            if (!*s)
+                {
+                return -1;
+                }
+            if (*s == '%') // escaped '%'
+                {
+                if (outs.put('%')<0)
+                    {
+                    return -1;
+                    }
+                s++;
+                continue;
+                }
+            char flag = '\0';
+            if (*s == '-' || *s == '+' || *s == ' ' ||
+                *s == '#' || *s == '0')
+                {
+                flag = *s++;
+                if (!*s)
+                    {
+                    return -1;
+                    }
+                }
+            int width     = 0;
+            int precision = 0;
+            s = getint(s, &width);
+            if (!*s)
+                {
+                return -1;
+                }
+            if (*s == '.')
+                {
+                s++;
+                if (!*s)
+                    {
+                    return -1;
+                    }
+                s = getint(s, &precision);
+                }
+            char length = '\0';
+            if (*s == 'l' || *s == 'h')
+                {
+                length = *s++;
+                if (!*s)
+                    {
+                    return -1;
+                    }
+                }
+            ch = *s++;
+            if (!ch)
+                {
+                return -1;
+                }
+            switch (ch)
+                {
+                case 'f':
+                case 'g':
+                    {
+                    double val = va_arg(ap, double);
+                    dprintDouble(outs, val, flag, width, precision);
+                    break;
+                    }
+                case 'd':
+                    {
+                    long val = 0;
+                    if (length == 'l')
+                        val = va_arg(ap, long);
+                    else if (length == 'h')
+                        val = (long)va_arg(ap, int);
+                    else
+                        val = (long)va_arg(ap, int);
+                    dprintInt(outs, val, 10, flag, width, precision);
+                    break;
+                    }
+                case 'x':
+                    {
+                    long val = 0;
+                    if (length == 'l')
+                        val = va_arg(ap, long);
+                    else if (length == 'h')
+                        val = (long)va_arg(ap, int);
+                    else
+                        val = (long)va_arg(ap, int);
+                    dprintInt(outs, val, 16, flag, width, precision);
+                    break;
+                    }
+                case 's':
+                    {
+                    char *val = va_arg(ap, char *);
+                    dprintString(outs, val, flag, width, precision);
+                    break;
+                    }
+                default:
+                    {
+                    break;
+                    }
+                }
+            }
+
+        }
+
+
+    return 1;
+}
+
+
 //#########################################################################
 //# B A S I C    I N P U T    S T R E A M
 //#########################################################################
@@ -155,11 +455,13 @@ void BasicOutputStream::flush()
 /**
  * Writes the specified byte to this output stream.
  */
-void BasicOutputStream::put(XMLCh ch)
+int BasicOutputStream::put(XMLCh ch)
 {
     if (closed)
-        return;
-    destination.put(ch);
+        return -1;
+    if (destination.put(ch) < 0)
+        return -1;
+    return 1;
 }
 
 
@@ -529,15 +831,17 @@ void BasicWriter::flush()
 /**
  * Writes the specified byte to this output writer.
  */
-void BasicWriter::put(XMLCh ch)
+int BasicWriter::put(XMLCh ch)
 {
-    if (destination)
-        destination->put(ch);
+    if (destination && destination->put(ch)>=0)
+        return 1;
+    return -1;
 }
 
 /**
  * Provide printf()-like formatting
  */
+/*
 Writer &BasicWriter::printf(char *fmt, ...)
 {
     va_list args;
@@ -549,6 +853,17 @@ Writer &BasicWriter::printf(char *fmt, ...)
 
     return *this;
 }
+*/
+Writer &BasicWriter::printf(char *fmt, ...)
+{
+    va_list args;
+    va_start(args, fmt);
+    dprintf(*this, fmt, args);
+
+    return *this;
+}
+
+
 /**
  * Writes the specified character to this output writer.
  */
@@ -710,11 +1025,13 @@ void OutputStreamWriter::flush()
  *  Overloaded to redirect the output chars from the next Writer
  *  in the chain to an OutputStream instead.
  */
-void OutputStreamWriter::put(XMLCh ch)
+int OutputStreamWriter::put(XMLCh ch)
 {
     //Do we need conversions here?
     int intCh = (int) ch;
-    outputStream.put(intCh);
+    if (outputStream.put(intCh) < 0)
+        return -1;
+    return 1;
 }
 
 //#########################################################################
@@ -762,11 +1079,13 @@ void StdWriter::flush()
  *  Overloaded to redirect the output chars from the next Writer
  *  in the chain to an OutputStream instead.
  */
-void StdWriter::put(XMLCh ch)
+int StdWriter::put(XMLCh ch)
 {
     //Do we need conversions here?
     int intCh = (int) ch;
-    outputStream->put(intCh);
+    if (outputStream->put(intCh) < 0)
+        return -1;
+    return 1;
 }
 
 
index 05862f9449b2af99eba34aa7076af38de57c638a..4a68ba1c134874e4d25c28119c59839f30cf9d9e 100644 (file)
@@ -215,7 +215,7 @@ public:
     /**
      * Send one byte to the destination stream.
      */
-    virtual void put(XMLCh ch) = 0;
+    virtual int put(XMLCh ch) = 0;
 
 
 }; // class OutputStream
@@ -238,7 +238,7 @@ public:
 
     virtual void flush();
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
 protected:
 
@@ -264,8 +264,8 @@ public:
     void flush()
         { }
 
-    void put(XMLCh ch)
-        {  putchar(ch); }
+    int put(XMLCh ch)
+        {  putchar(ch); return 1; }
 
 };
 
@@ -489,7 +489,7 @@ public:
 
     virtual void flush() = 0;
 
-    virtual void put(XMLCh ch) = 0;
+    virtual int put(XMLCh ch) = 0;
 
     /* Formatted output */
     virtual Writer& printf(char *fmt, ...) = 0;
@@ -539,7 +539,7 @@ public:
 
     virtual void flush();
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
 
 
@@ -626,7 +626,7 @@ public:
 
     virtual void flush();
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
 
 private:
@@ -654,7 +654,7 @@ public:
     virtual void flush();
 
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
 
 private:
index 19369b6dcb68dfbd2990f323f31be2f349dbe7ac..f634acbe72b895a73bc73daac088e72298025490 100644 (file)
@@ -214,17 +214,17 @@ void GzipOutputStream::flush()
 /**
  * Writes the specified byte to this output stream.
  */
-void GzipOutputStream::put(XMLCh ch)
+int GzipOutputStream::put(XMLCh ch)
 {
     if (closed)
         {
         //probably throw an exception here
-        return;
+        return -1;
         }
 
     //Add char to buffer
     buffer.push_back(ch);
-
+    return 1;
 }
 
 
index 6396772a5eddde051253bf8d92e3c1c4e33da2ab..4af5ada2b49f05284554ada30ab448320fb653d2 100644 (file)
@@ -101,7 +101,7 @@ public:
 
     virtual void flush();
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
 private:
 
index 93fb0c0b5be1b7e0d433b613487334ddd55b927e..f2745a1073e62080455dd0494b1a0ab5f4602e78 100644 (file)
@@ -140,9 +140,10 @@ void StringOutputStream::flush()
 /**
  * Writes the specified byte to this output stream.
  */
-void StringOutputStream::put(XMLCh ch)
+int StringOutputStream::put(XMLCh ch)
 {
     buffer.push_back(ch);
+    return 1;
 }
 
 
index 8bb4e76b147946427c7bdd9b8ce43bca93ea65e6..38aaf72353777965086c3a6c13c9b6bb340237c1 100644 (file)
@@ -98,7 +98,7 @@ public:
 
     virtual void flush();
 
-    virtual void put(XMLCh ch);
+    virtual int put(XMLCh ch);
 
     virtual DOMString &getString()
         { return buffer; }
index 1cc540bd5d7b54ad49a53cd9c1989500c57a175d..8928921e4cb3bc9d41f25d6f82f47e0bbc93adc6 100644 (file)
@@ -411,10 +411,10 @@ void UriOutputStream::flush() throw(StreamException)
 /**
  * Writes the specified byte to this output stream.
  */
-void UriOutputStream::put(XMLCh ch) throw(StreamException)
+int UriOutputStream::put(XMLCh ch) throw(StreamException)
 {
     if (closed)
-        return;
+        return -1;
 
     switch (scheme)
         {
@@ -422,7 +422,7 @@ void UriOutputStream::put(XMLCh ch) throw(StreamException)
         case URI::SCHEME_FILE:
             {
             if (!outf)
-                return;
+                return -1;
             unsigned char uch = (unsigned char)(ch & 0xff);
             fputc(uch, outf);
             //fwrite(uch, 1, 1, outf);
@@ -436,7 +436,7 @@ void UriOutputStream::put(XMLCh ch) throw(StreamException)
             }
 
         }//switch
-
+    return 1;
 }
 
 
@@ -479,10 +479,12 @@ void UriWriter::flush() throw(StreamException)
 /**
  *
  */
-void UriWriter::put(XMLCh ch) throw(StreamException)
+int UriWriter::put(XMLCh ch) throw(StreamException)
 {
     int ich = (int)ch;
-    outputStream->put(ich);
+    if (outputStream->put(ich) < 0)
+        return -1;
+    return 1;
 }
 
 
index 892ac5c688ff5f0f5e53be9a40e1b5fa7088c760..45d44b71d16241912ddaa8c68b50cdf030d00b4d 100644 (file)
@@ -146,7 +146,7 @@ public:
 
     virtual void flush() throw(StreamException);
 
-    virtual void put(XMLCh ch) throw(StreamException);
+    virtual int put(XMLCh ch) throw(StreamException);
 
 private:
 
@@ -187,7 +187,7 @@ public:
 
     virtual void flush() throw(StreamException);
 
-    virtual void put(XMLCh ch) throw(StreamException);
+    virtual int put(XMLCh ch) throw(StreamException);
 
 private:
 
index 1d364804843780d23cdc42b2f68ab89bd333ba68..3e62ea999b3e33fc04bb272ba3b45d64b1d5629f 100644 (file)
@@ -1361,9 +1361,9 @@ bool OdfOutput::writeStyle(Writer &outs)
     outs.printf("  <style:graphic-properties draw:stroke=\"none\" draw:fill=\"none\"\n");
     outs.printf("       draw:textarea-horizontal-align=\"center\"\n");
     outs.printf("       draw:textarea-vertical-align=\"middle\" draw:color-mode=\"standard\"\n");
-    outs.printf("       draw:luminance=\"0%\" draw:contrast=\"0%\" draw:gamma=\"100%\" draw:red=\"0%\"\n");
-    outs.printf("       draw:green=\"0%\" draw:blue=\"0%\" fo:clip=\"rect(0cm 0cm 0cm 0cm)\"\n");
-    outs.printf("       draw:image-opacity=\"100%\" style:mirror=\"none\"/>\n");
+    outs.printf("       draw:luminance=\"0%%\" draw:contrast=\"0%%\" draw:gamma=\"100%%\" draw:red=\"0%%\"\n");
+    outs.printf("       draw:green=\"0%%\" draw:blue=\"0%%\" fo:clip=\"rect(0cm 0cm 0cm 0cm)\"\n");
+    outs.printf("       draw:image-opacity=\"100%%\" style:mirror=\"none\"/>\n");
     outs.printf("</style:style>\n");
     outs.printf("<style:style style:name=\"P1\" style:family=\"paragraph\">\n");
     outs.printf("  <style:paragraph-properties fo:text-align=\"center\"/>\n");