00001 /****************************************************************************** 00002 * 00003 * $Id: outputgen.cpp,v 1.15 2001/03/19 19:27:41 root Exp $ 00004 * 00005 * Copyright (C) 1997-2008 by Dimitri van Heesch. 00006 * 00007 * Permission to use, copy, modify, and distribute this software and its 00008 * documentation under the terms of the GNU General Public License is hereby 00009 * granted. No representations are made about the suitability of this software 00010 * for any purpose. It is provided "as is" without express or implied warranty. 00011 * See the GNU General Public License for more details. 00012 * 00013 * Documents produced by Doxygen are derivative works derived from the 00014 * input used in their production; they are not affected by this license. 00015 * 00016 */ 00017 00018 #include <stdlib.h> 00019 00020 #include "qtbc.h" 00021 #include "outputgen.h" 00022 #include "message.h" 00023 #include "portable.h" 00024 00025 OutputGenerator::OutputGenerator() 00026 { 00027 //printf("OutputGenerator::OutputGenerator()\n"); 00028 file=0; 00029 b.setBuffer(a); 00030 b.open( IO_WriteOnly ); 00031 t.setDevice(&b); 00032 t.setEncoding(QTextStream::UnicodeUTF8); 00033 active=TRUE; 00034 genStack = new QStack<bool>; 00035 genStack->setAutoDelete(TRUE); 00036 } 00037 00038 OutputGenerator::~OutputGenerator() 00039 { 00040 //printf("OutputGenerator::~OutputGenerator()\n"); 00041 delete file; 00042 delete genStack; 00043 } 00044 00045 void OutputGenerator::startPlainFile(const char *name) 00046 { 00047 //printf("startPlainFile(%s)\n",name); 00048 QCString fileName=dir+"/"+name; 00049 file = new QFile(fileName); 00050 if (!file) 00051 { 00052 err("Could not create file object for %s\n",fileName.data()); 00053 exit(1); 00054 } 00055 if (!file->open(IO_WriteOnly)) 00056 { 00057 err("Could not open file %s for writing\n",fileName.data()); 00058 exit(1); 00059 } 00060 fs.setDevice(file); 00061 } 00062 00063 void OutputGenerator::endPlainFile() 00064 { 00065 bool converted=false; 00066 if (!encoding.isEmpty()) 00067 { 00068 QByteArray enc(a.size()*4); 00069 void *cd = portable_iconv_open(encoding,"UTF-8"); 00070 if (cd!=(void *)(-1)) 00071 { 00072 size_t iLeft=a.size(); 00073 size_t oLeft=enc.size(); 00074 const char *inputPtr = a.data(); 00075 char *outputPtr = enc.data(); 00076 if (!portable_iconv(cd, &inputPtr, &iLeft, &outputPtr, &oLeft)) 00077 { 00078 enc.resize(enc.size()-oLeft); 00079 postProcess(enc); 00080 //printf("a.size()=%d enc.size()=%d iLeft=%d oLeft=%d enc2.size()=%d\n", 00081 // a.size(),enc.size(),iLeft,oLeft,enc2.size()); 00082 fs.writeRawBytes(enc.data(),enc.size()) ; // write string buffer to file 00083 converted=TRUE; 00084 } 00085 portable_iconv_close(cd); 00086 } 00087 } 00088 if (!converted) 00089 { 00090 //printf("endPlainFile(%s)\n",file->name()); 00091 fs.writeRawBytes(a.data(),a.size()) ; // write string buffer to file 00092 } 00093 b.close(); 00094 delete file; 00095 file=0; 00096 a.resize(0); 00097 b.setBuffer(a); 00098 b.open(IO_WriteOnly); 00099 t.setDevice(&b); 00100 } 00101 00102 QCString OutputGenerator::getContents() const 00103 { 00104 QCString s; 00105 s.resize(a.size()+1); 00106 memcpy(s.data(),a.data(),a.size()); 00107 s.at(a.size())='\0'; 00108 return s; 00109 } 00110 00111 void OutputGenerator::pushGeneratorState() 00112 { 00113 genStack->push(new bool(isEnabled())); 00114 } 00115 00116 void OutputGenerator::popGeneratorState() 00117 { 00118 bool *lb = genStack->pop(); 00119 ASSERT(lb!=0); 00120 if (lb==0) return; // for some robustness against superfluous \endhtmlonly commands. 00121 if (*lb) enable(); else disable(); 00122 delete lb; 00123 } 00124