~ubuntu-branches/debian/jessie/arb/jessie

« back to all changes in this revision

Viewing changes to SL/FILE_BUFFER/FileBuffer.cxx

  • Committer: Package Import Robot
  • Author(s): Elmar Pruesse, Andreas Tille, Elmar Pruesse
  • Date: 2014-09-02 15:15:06 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140902151506-jihq58b3iz342wif
Tags: 6.0.2-1
[ Andreas Tille ]
* New upstream version
  Closes: #741890
* debian/upstream -> debian/upstream/metadata
* debian/control:
   - Build-Depends: added libglib2.0-dev
   - Depends: added mafft, mrbayes
* debian/rules
   - Add explicite --remove-section=.comment option to manual strip call
* cme fix dpkg-control
* arb-common.dirs: Do not create unneeded lintian dir
* Add turkish debconf translation (thanks for the patch to Mert Dirik
  <mertdirik@gmail.com>)
  Closes: #757497

[ Elmar Pruesse ]
* patches removed:
   - 10_config.makefiles.patch,
     80_no_GL.patch
       removed in favor of creating file from config.makefile.template via 
       sed in debian/control
   - 20_Makefile_main.patch
       merged upstream
   - 21_Makefiles.patch
       no longer needed
   - 30_tmpfile_CVE-2008-5378.patch: 
       merged upstream
   - 50_fix_gcc-4.8.patch:
       merged upstream
   - 40_add_libGLU.patch:
       libGLU not needed for arb_ntree)
   - 60_use_debian_packaged_raxml.patch:
       merged upstream
   - 70_hardening.patch
       merged upstream
   - 72_add_math_lib_to_linker.patch
       does not appear to be needed
* patches added:
   - 10_upstream_r12793__show_db_load_progress:
       backported patch showing progress while ARB is loading a database
       (needed as indicator/splash screen while ARB is launching)
   - 20_upstream_r12794__socket_permissions:
       backported security fix
   - 30_upstream_r12814__desktop_keywords:
       backported add keywords to desktop (fixes lintian warning)
   - 40_upstream_r12815__lintian_spelling:
       backported fix for lintian reported spelling errors
   - 50_private_nameservers
       change configuration to put nameservers into users home dirs
       (avoids need for shared writeable directory)
   - 60_use_debian_phyml
       use phyml from debian package for both interfaces in ARB
* debian/rules:
   - create config.makefile from override_dh_configure target
   - use "make tarfile" in override_dh_install
   - remove extra cleaning not needed for ARB 6
   - use "dh_install --list-missing" to avoid missing files
   - added override_dh_fixperms target
* debian/control:
   - added libarb-dev package
   - Depends: added phyml, xdg-utils
   - Suggests: removed phyml
   - fix lintian duplicate-short-description (new descriptions)
* debian/*.install:
   - "unrolled" confusing globbing to select files
   - pick files from debian/tmp
   - moved all config files to /etc/arb
* debian/arb-common.templates: updated
* scripts:
   - removed arb-add-pt-server
   - launch-wrapper: 
     - only add demo.arb to newly created $ARBUSERDATA
     - pass commandline arguments through bin/arb wrapper
   - preinst: removing old PT server index files on upgrade from 5.5*
   - postinst: set setgid on shared PT dir
* rewrote arb.1 manfile
* added file icon for ARB databases
* using upstream arb_tcp.dat

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ================================================================ //
2
 
//                                                                  //
3
 
//   File      : FileBuffer.cxx                                     //
4
 
//   Purpose   :                                                    //
5
 
//                                                                  //
6
 
//   Coded by Ralf Westram (coder@reallysoft.de) in December 2006   //
7
 
//   Institute of Microbiology (Technical University Munich)        //
8
 
//   http://www.arb-home.de/                                        //
9
 
//                                                                  //
10
 
// ================================================================ //
11
 
 
12
 
#include "FileBuffer.h"
13
 
#include <cstdlib>
14
 
#include <cstring>
15
 
#include <cstdio>
16
 
#include <errno.h>
17
 
 
18
 
using namespace std;
19
 
 
20
 
void FileBuffer::fillBuffer()
21
 
{
22
 
    if (read==BUFFERSIZE) {
23
 
        read = fread(buf, sizeof(buf[0]), BUFFERSIZE, fp);
24
 
        offset = 0;
25
 
    }
26
 
    else {
27
 
        offset = read;
28
 
    }
29
 
}
30
 
 
31
 
static char eol[3] = "\n\r";
32
 
static inline bool is_EOL(char c) { return c == eol[0] || c == eol[1]; }
33
 
 
34
 
bool FileBuffer::getLine_intern(string& line)
35
 
{
36
 
    if (offset==read) return false;
37
 
 
38
 
    size_t lineEnd;
39
 
    {
40
 
        size_t  rest   = read-offset;
41
 
        char   *eolPos = (char*)memchr(buf+offset, eol[0], rest);
42
 
 
43
 
        if (!eolPos) {
44
 
            eolPos = (char*)memchr(buf+offset, eol[1], rest);
45
 
            if (!eolPos) {
46
 
                lineEnd = read;
47
 
            }
48
 
            else {
49
 
                swap(eol[0], eol[1]);
50
 
                lineEnd = eolPos-buf;
51
 
            }
52
 
        }
53
 
        else {
54
 
            lineEnd = eolPos-buf;
55
 
            if (lineEnd>0 && buf[lineEnd-1] == eol[1]) {
56
 
                swap(eol[0], eol[1]);
57
 
                lineEnd--;
58
 
            }
59
 
        }
60
 
    }
61
 
 
62
 
    if (lineEnd<read) { // found end of line char
63
 
        line    = string(buf+offset, lineEnd-offset);
64
 
        char lf = buf[lineEnd];
65
 
 
66
 
        offset = lineEnd+1;
67
 
        if (offset == read) fillBuffer();
68
 
 
69
 
        if (offset<read) { // otherwise EOF!
70
 
            char nextChar = buf[offset];
71
 
            if (is_EOL(nextChar) && nextChar != lf) offset++; // skip DOS linefeed
72
 
            if (offset == read) fillBuffer();
73
 
        }
74
 
    }
75
 
    else { // reached end of buffer
76
 
        line = string(buf+offset, read-offset);
77
 
        fillBuffer();
78
 
        string rest;
79
 
        if (getLine_intern(rest)) line = line+rest;
80
 
    }
81
 
 
82
 
    return true;
83
 
}
84
 
 
85
 
string FileBuffer::lineError(const char *msg) {
86
 
    static char   *buffer;
87
 
    static size_t  allocated = 0;
88
 
 
89
 
    size_t len;
90
 
    if (showFilename) {
91
 
        len = strlen(msg)+filename.length()+100;
92
 
    }
93
 
    else {
94
 
        len = strlen(msg)+100;
95
 
    }
96
 
 
97
 
    if (len>allocated) {
98
 
        allocated = len;
99
 
        free(buffer);
100
 
        buffer    = (char*)malloc(allocated);
101
 
    }
102
 
 
103
 
    if (showFilename) {
104
 
#if defined(DEBUG)
105
 
        int printed =
106
 
#endif // DEBUG
107
 
            sprintf(buffer, "while reading %s (line #%li):\n%s", filename.c_str(), lineNumber, msg);
108
 
        fb_assert((size_t)printed < allocated);
109
 
    }
110
 
    else {
111
 
#if defined(DEBUG)
112
 
        int printed =
113
 
#endif // DEBUG
114
 
            sprintf(buffer, "while reading line #%li:\n%s", lineNumber, msg);
115
 
        fb_assert((size_t)printed < allocated);
116
 
    }
117
 
    
118
 
    return buffer;
119
 
}
120
 
 
121
 
void FileBuffer::rewind() {
122
 
    errno = 0;
123
 
    std::rewind(fp);
124
 
    fb_assert(errno == 0); // not handled yet
125
 
    
126
 
    read = BUFFERSIZE;
127
 
    fillBuffer();
128
 
 
129
 
    if (next_line) {
130
 
        delete next_line;
131
 
        next_line = 0;
132
 
    }
133
 
    lineNumber = 0;
134
 
}
135
 
 
136
 
// --------------------------------------------------------------------------------
137
 
// C interface
138
 
 
139
 
inline FileBuffer *to_FileBuffer(FILE_BUFFER fb) {
140
 
    FileBuffer *fileBuffer = reinterpret_cast<FileBuffer*>(fb);
141
 
    fb_assert(fileBuffer);
142
 
    fb_assert(fileBuffer->good());
143
 
    return fileBuffer;
144
 
}
145
 
 
146
 
extern "C" FILE_BUFFER create_FILE_BUFFER(const char *filename, FILE *in) {
147
 
    FileBuffer *fb = new FileBuffer(filename, in);
148
 
    return reinterpret_cast<FILE_BUFFER>(fb);
149
 
}
150
 
 
151
 
extern "C" void destroy_FILE_BUFFER(FILE_BUFFER file_buffer) {
152
 
    delete to_FileBuffer(file_buffer);
153
 
}
154
 
 
155
 
extern "C" const char *FILE_BUFFER_read(FILE_BUFFER file_buffer, size_t *lengthPtr) {
156
 
    static string  line;
157
 
 
158
 
    if (to_FileBuffer(file_buffer)->getLine(line)) {
159
 
        if (lengthPtr) *lengthPtr = line.length();
160
 
        return line.c_str();
161
 
    }
162
 
    return 0;
163
 
}
164
 
 
165
 
extern "C" void FILE_BUFFER_back(FILE_BUFFER file_buffer, const char *backline) {
166
 
    static string line;
167
 
    line = backline;
168
 
    to_FileBuffer(file_buffer)->backLine(line);
169
 
}
170
 
 
171
 
extern "C" void FILE_BUFFER_rewind(FILE_BUFFER file_buffer) {
172
 
    to_FileBuffer(file_buffer)->rewind();
173
 
}