00001 /* 00002 This file is part of KWrite 00003 Copyright (c) 2000 Waldo Bastian <bastian@kde.org> 00004 Copyright (c) 2002 Joseph Wenninger <jowenn@kde.org> 00005 00006 $Id: katebuffer.cpp,v 1.4 2004/05/03 21:35:19 ar Exp $ 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License version 2 as published by the Free Software Foundation. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00020 Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 #include "katebuffer.h" 00025 #include "kdebug.h" 00026 00027 /* OPIE */ 00028 #include <opie2/odebug.h> 00029 00030 /* QT */ 00031 #include <qfile.h> 00032 #include <qtextstream.h> 00033 #include <qtimer.h> 00034 #include <qtextcodec.h> 00035 00036 /* STD */ 00037 // Includes for reading file 00038 #include <sys/types.h> 00039 #include <sys/stat.h> 00040 #include <fcntl.h> 00041 #include <errno.h> 00042 #include <unistd.h> 00043 #include <assert.h> 00044 00048 KWBuffer::KWBuffer() 00049 { 00050 clear(); 00051 } 00052 00053 void 00054 KWBuffer::clear() 00055 { 00056 m_stringListIt=0; 00057 m_stringListCurrent=0; 00058 m_stringList.clear(); 00059 m_lineCount=1; 00060 m_stringListIt = m_stringList.append(new TextLine()); 00061 } 00062 00066 void 00067 KWBuffer::insertFile(int line, const QString &file, QTextCodec *codec) 00068 { 00069 if (line) { 00070 odebug << "insert File only supports insertion at line 0 == file opening" << oendl; 00071 return; 00072 } 00073 clear(); 00074 QFile iofile(file); 00075 iofile.open(IO_ReadOnly); 00076 QTextStream stream(&iofile); 00077 stream.setCodec(codec); 00078 QString qsl; 00079 int count=0; 00080 for (count=0;((qsl=stream.readLine())!=QString::null); count++) 00081 { 00082 if (count==0) 00083 { 00084 (*m_stringListIt)->append(qsl.unicode(),qsl.length()); 00085 } 00086 else 00087 { 00088 TextLine::Ptr tl=new TextLine(); 00089 tl ->append(qsl.unicode(),qsl.length()); 00090 m_stringListIt=m_stringList.append(tl); 00091 } 00092 } 00093 if (count!=0) 00094 { 00095 m_stringListCurrent=count-1; 00096 m_lineCount=count; 00097 } 00098 } 00099 00100 void 00101 KWBuffer::loadFilePart() 00102 { 00103 } 00104 00105 00106 void 00107 KWBuffer::insertData(int line, const QByteArray &data, QTextCodec *codec) 00108 { 00109 } 00110 00111 void 00112 KWBuffer::slotLoadFile() 00113 { 00114 loadFilePart(); 00115 // emit linesChanged(m_totalLines); 00116 emit linesChanged(20); 00117 } 00118 00122 int 00123 KWBuffer::count() 00124 { 00125 odebug << "m_stringList.count " << m_stringList.count() << "" << oendl; 00126 return m_lineCount; 00127 // return m_stringList.count(); 00128 // return m_totalLines; 00129 } 00130 00131 00132 void KWBuffer::seek(int i) 00133 { 00134 if (m_stringListCurrent == i) 00135 return; 00136 while(m_stringListCurrent < i) 00137 { 00138 ++m_stringListCurrent; 00139 ++m_stringListIt; 00140 } 00141 while(m_stringListCurrent > i) 00142 { 00143 --m_stringListCurrent; 00144 --m_stringListIt; 00145 } 00146 } 00147 00148 00149 TextLine::Ptr 00150 KWBuffer::line(int i) 00151 { 00152 if (i>=m_stringList.count()) return 0; 00153 seek(i); 00154 return *m_stringListIt; 00155 } 00156 00157 void 00158 KWBuffer::insertLine(int i, TextLine::Ptr line) 00159 { 00160 seek(i); 00161 m_stringListIt = m_stringList.insert(m_stringListIt, line); 00162 m_stringListCurrent = i; 00163 m_lineCount++; 00164 } 00165 00166 00167 void 00168 KWBuffer::removeLine(int i) 00169 { 00170 seek(i); 00171 m_stringListIt = m_stringList.remove(m_stringListIt); 00172 m_stringListCurrent = i; 00173 m_lineCount--; 00174 } 00175 00176 void 00177 KWBuffer::changeLine(int i) 00178 { 00179 } 00180
1.4.2