~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/CLucene/util/inputstreambuffer.h

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*------------------------------------------------------------------------------
2
 
* Copyright (C) 2003-2006 Jos van den Oever
3
 
4
 
* Distributable under the terms of either the Apache License (Version 2.0) or 
5
 
* the GNU Lesser General Public License, as specified in the COPYING file.
6
 
------------------------------------------------------------------------------*/
7
 
/* This file is part of Strigi Desktop Search
8
 
 *
9
 
 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
10
 
 *
11
 
 * This library is free software; you can redistribute it and/or
12
 
 * modify it under the terms of the GNU Library General Public
13
 
 * License as published by the Free Software Foundation; either
14
 
 * version 2 of the License, or (at your option) any later version.
15
 
 *
16
 
 * This library is distributed in the hope that it will be useful,
17
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 
 * Library General Public License for more details.
20
 
 *
21
 
 * You should have received a copy of the GNU Library General Public License
22
 
 * along with this library; see the file COPYING.LIB.  If not, write to
23
 
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24
 
 * Boston, MA 02110-1301, USA.
25
 
 */
26
 
#ifndef INPUTSTREAMBUFFER_H
27
 
#define INPUTSTREAMBUFFER_H
28
 
 
29
 
#include <cstdlib>
30
 
 
31
 
namespace jstreams {
32
 
 
33
 
template <class T>
34
 
class InputStreamBuffer {
35
 
private:
36
 
public:
37
 
    T* start;
38
 
    int32_t size;
39
 
    T* readPos;
40
 
    int32_t avail;
41
 
 
42
 
    InputStreamBuffer();
43
 
    ~InputStreamBuffer();
44
 
    void setSize(int32_t size);
45
 
    int32_t read(const T*& start, int32_t max=0);
46
 
 
47
 
    /**
48
 
     * This function prepares the buffer for a new write.
49
 
     * returns the number of available places.
50
 
     **/
51
 
     int32_t makeSpace(int32_t needed);
52
 
};
53
 
 
54
 
template <class T>
55
 
InputStreamBuffer<T>::InputStreamBuffer() {
56
 
    readPos = start = 0;
57
 
    size = avail = 0;
58
 
}
59
 
template <class T>
60
 
InputStreamBuffer<T>::~InputStreamBuffer() {
61
 
    free(start);
62
 
}
63
 
template <class T>
64
 
void
65
 
InputStreamBuffer<T>::setSize(int32_t size) {
66
 
    // store pointer information
67
 
    int32_t offset = (int32_t)(readPos - start);
68
 
 
69
 
        // allocate memory in the buffer
70
 
    if ( start == 0 )
71
 
                start = (T*)malloc(size*sizeof(T));
72
 
        else
73
 
                start = (T*)realloc(start, size*sizeof(T));
74
 
    this->size = size;
75
 
 
76
 
    // restore pointer information
77
 
    readPos = start + offset;
78
 
}
79
 
template <class T>
80
 
int32_t
81
 
InputStreamBuffer<T>::makeSpace(int32_t needed) {
82
 
    // determine how much space is available for writing
83
 
    int32_t space = size - ((int32_t)(readPos - start)) - avail;
84
 
    if (space >= needed) {
85
 
        // there's enough space
86
 
        return space;
87
 
    }
88
 
 
89
 
    if (avail) {
90
 
        if (readPos != start) {
91
 
//            printf("moving\n");
92
 
            // move data to the start of the buffer
93
 
            memmove(start, readPos, avail*sizeof(T));
94
 
            space += (int32_t)(readPos - start);
95
 
            readPos = start;
96
 
        }
97
 
    } else {
98
 
        // we may start writing at the start of the buffer
99
 
        readPos = start;
100
 
        space = size;
101
 
    }
102
 
    if (space >= needed) {
103
 
        // there's enough space now
104
 
        return space;
105
 
    }
106
 
 
107
 
    // still not enough space, we have to allocate more
108
 
//    printf("resize %i %i %i %i %i\n", avail, needed, space, size + needed - space, size);
109
 
    setSize(size + needed - space);
110
 
    return needed;
111
 
}
112
 
template <class T>
113
 
int32_t
114
 
InputStreamBuffer<T>::read(const T*& start, int32_t max) {
115
 
    start = readPos;
116
 
    if (max <= 0 || max > avail) {
117
 
        max = avail;
118
 
    }
119
 
    readPos += max;
120
 
    avail -= max;
121
 
    return max;
122
 
}
123
 
 
124
 
} // end namespace jstreams
125
 
 
126
 
#endif