summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/BuffDoc.cpp
blob: d4541eaae175940109b686b7171620861efb8abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "BuffDoc.h"
//#include <FL/fl_draw.h>
#include "config.h"
#include "CDrawBuffer.h"
#include "plucker.h"


bool BuffDoc::hyperlink(unsigned int n)
{
    bool bRet = false;
    lastword.empty();
    lastsizes[0] = laststartline = n;
    lastispara = false;
    if (exp != NULL)
    {
  bRet = exp->hyperlink(n);
  lastsizes[0] = laststartline = exp->locate();
    }
    return bRet;
}

void BuffDoc::locate(unsigned int n)
{
  //  qDebug("BuffDoc:locating:%u",n);
    lastword.empty();
  lastsizes[0] = laststartline = n;
  lastispara = false;
  //    tchar linebuf[1024];
  if (exp != NULL) exp->locate(n);
  //  qDebug("BuffDoc:Located");
}

bool BuffDoc::getline(CDrawBuffer* buff, int w)
{
    tchar ch = 32;
    CStyle cs;
    buff->empty();
    if (exp == NULL)
    {
//  (*buff)[0] = '\0';
  buff->empty();
  return false;
    }
  int len = 0, lastcheck = 0;
  if (lastword.length() > 0)
  {
      *buff = lastword;
      cs = lastword.laststyle();
  }
  else buff->empty();
//  qDebug("Buff:%s Lastword:%s", (const char*)toQString(buff->data()), (const char*)toQString(lastword.data()));
  lastcheck = len = buff->length();
  unsigned int slen = buff->width(len);
  if (slen > w)
    {
      for ( ; len > 0; len--)
  {
    if (buff->width(len) < w) break;
  }
//      lastword = buff->data() + len - 1;
      laststartline = lastsizes[len-1];
      lastword.setright(*buff, len - 1);
      for (int i = 0; i < buff->length(); i++) lastsizes[i] = lastsizes[i+len-1];
//      (*buff)[len-1] = '-';
      buff->truncate(len-1);
      buff->addch('-', cs);
      (*buff)[len] = '\0';
      return true;
    }
  if (lastispara)
    {
      lastispara = false;
//      lastword[0] = '\0';
      lastword.empty();
      len = buff->length();
      while (buff->width(len) > w) len--;
//      (*buff)[len] = '\0';
      buff->truncate(len);
      laststartline = exp->locate();
      return true;
    }
  lastispara = false;
  for (int i = 0; i < len; i++) allsizes[i] = lastsizes[i];
  while (slen < w)
  {
      lastcheck = len;
      allsizes[len] = exp->locate();
      getch(ch, cs);
      while (ch != ' ' && ch != '\012' && ch != UEOF && len < 128)
      {
    len++;
    buff->addch(ch,cs);
    allsizes[len] = exp->locate();
    getch(ch, cs);
      }
      (*buff)[len] = 0;
      slen = buff->width(len);
      len++;
      buff->addch(' ', cs);
      allsizes[len] = exp->locate();
      if (slen < w && ch != ' ')
      {
    lastcheck = len;
    break;
      }
      lastispara = (ch == '\012');
  }
  (*buff)[len] = '\0';
//  lastword = buff->data()+lastcheck;
  lastword.setright(*buff, lastcheck);
  for (int i = 0; i < lastword.length(); i++) lastsizes[i] = allsizes[i+lastcheck];
  if (lastcheck > 0)
  {
      laststartline = allsizes[lastcheck];
//      (*buff)[lastcheck-1] = '\0';
      buff->truncate(lastcheck-1);
  }
  else
  {
      laststartline = (lastcheck == len) ? exp->locate() : allsizes[lastcheck+1];
//      (*buff)[lastcheck] = '\0';
      buff->truncate(lastcheck);
  }
//  buff->frig();
  return (ch != UEOF);
}

bool BuffDoc::getline(CDrawBuffer* buff, int w, int cw)
{
    buff->empty();
    if (exp == NULL)
    {
  return false;
    }
    tchar ch;
    CStyle cs;
    int i = 0;
    while (i*cw < w)
    {
  getch(ch, cs);
  if (ch == '\12' || ch == UEOF) break;
  buff->addch(ch,cs);
  i++;
    }
    buff->truncate(i);
    laststartline = exp->locate();
    return (ch != UEOF);
}

int BuffDoc::openfile(QWidget* _parent, const char *src)
{
    //    qDebug("BuffDoc:Openfile:%s", src);
    //    qDebug("Trying aportis %x",exp);
    if (exp != NULL) delete exp;
    lastword.empty();
    lastsizes[0] = laststartline = 0;
    lastispara = false;
    /*
      exp = new Text;
      int ret = exp->openfile(src);
    */

    exp = new Aportis;
    int ret = exp->openfile(src);
    if (ret == -1)
    {
  delete exp;
  exp = NULL;
  return ret;
    }
    if (ret == -2)
    {

  delete exp;
  exp = new ztxt;
  ret = exp->openfile(src);
    }
    if (ret != 0)
    {

  delete exp;
  exp = new CPlucker;
  ret = exp->openfile(src);
    }
#ifndef SMALL
    if (ret != 0)
    {
  delete exp;
  qDebug("Trying ppms");
  exp = new ppm_expander;
  ret = exp->openfile(src);
    }
      
    if (ret != 0)
    {
  delete exp;
  exp = new Text;
//    qDebug("Trying text");
  ret = exp->openfile(src);
    }
#else
    if (ret != 0)
    {
  delete exp;
  exp = new Text;
  ret = exp->openfile(src);
    }
#endif
    if (ret != 0)
    {
  delete exp;
  QMessageBox::information(_parent, "OpieReader", "Unknown file compression type","Try another file");
  return ret;
    }
    //        qDebug("Doing final open:%x:%x",exp,filt);

    lastword.empty();
    lastsizes[0] = laststartline = 0;
    lastispara = false;
    exp->locate(0);
    filt->setsource(exp);
    //        qDebug("BuffDoc:file opened");
    return 0;
}