source: branches/profile/SECEDIT/SEC_abspos.cxx

Last change on this file was 8905, checked in by westram, 12 years ago
  • fixed cppcheck warnings
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : SEC_abspos.cxx                                    //
4//   Purpose   : Encapsulates helix position access                //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in July 2007      //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include <cstdlib>
13#include <cstring>
14
15#include "SEC_abspos.hxx"
16
17using namespace std;
18
19void XString::set_length(size_t len) {
20    if (number_found && x_string_len<len) freenull(number_found);
21    x_string_len = len;
22    initialized  = false;
23}
24
25XString::XString(size_t ali_length)
26    : abspos(0)
27    , number_found(0)
28{
29    int len = ali_length+1; // need one more (cause 'x's are written behind position)
30    x_string = (char*)malloc((len+1)*sizeof(*x_string));
31    memset(x_string, '.', len);
32    x_string[len] = 0;
33    set_length(len);
34    initialize();
35}
36
37XString::XString(const char *saved_x_string, size_t saved_length, size_t ali_length)
38    : abspos(0)
39    , number_found(0)
40{
41    size_t xlen = ali_length+1;
42
43    sec_assert(saved_length == strlen(saved_x_string));
44    sec_assert(saved_length == xlen || saved_length == xlen-1);
45
46    x_string = (char*)malloc((xlen+1)*sizeof(*x_string));
47    memcpy(x_string, saved_x_string, saved_length+1);
48
49    if (saved_length == xlen-1) { // normal case
50        x_string[xlen-1] = '.'; // additional position is a gap (SAI 'HELIX' should have a gap at end)
51        x_string[xlen]   = 0;   // (see also comments in get_x_string())
52    }
53
54    set_length(xlen);
55    initialize();
56}
57
58void XString::initialize()
59{
60    // detect number of 'x's in x_string :
61    {
62        size_t len = 0;
63        int    x   = 0;
64
65        while (char c = x_string[len]) {
66            if (c == 'x') x++;
67            len++;
68        }
69
70        sec_assert(len == x_string_len);
71
72        if (abspos) { // re-initialization
73            if (x_count<x) freenull(abspos); // abspos array too small
74        }
75        x_count = x;
76    }
77
78    if (!abspos)       abspos       = (size_t*)malloc(x_count * sizeof(*abspos));
79    if (!number_found) number_found = (int*)malloc(x_string_len * sizeof(*number_found));
80
81    // init abspos and number_found :
82    {
83        int pos = 0;
84        int x   = 0;
85
86        while (char c = x_string[pos]) {
87            number_found[pos] = x;
88            if (c == 'x') {
89                abspos[x] = pos;
90                x++;
91            }
92            pos++;
93        }
94    }
95
96    initialized = true;
97}
98
99XString::~XString()
100{
101    free(x_string);
102    free(abspos);
103    free(number_found);
104}
105
106size_t XString::getAbsPos(int x) const
107{
108    sec_assert(initialized);
109    sec_assert(x >= 0 && x<x_count);
110    size_t pos = abspos[x];
111    sec_assert(pos<x_string_len);
112    return pos;
113}
114
115int XString::getXleftOf(size_t pos) const
116{
117    sec_assert(initialized);
118    sec_assert(pos<x_string_len);
119    int x = number_found[pos];
120    sec_assert(x >= 0 && x<x_count);
121    return x;
122}
123
124const char *XString::get_x_string() const {
125    sec_assert(initialized);
126
127    static char   *copy       = 0;
128    static size_t  copy_alloc = 0;
129
130    size_t bufsize = x_string_len+1;
131
132    if (!copy || copy_alloc<bufsize) {
133        freeset(copy, (char*)malloc(bufsize));
134        copy_alloc = bufsize;
135    }
136
137    memcpy(copy, x_string, x_string_len+1);
138
139    int add_pos = x_string_len-1;
140
141    if (copy[add_pos] == '.') { // can be removed - added again after reload
142        copy[add_pos] = 0; // hide internal additional position
143    }
144    else {
145        // happens only if there's a helix on last alignment position.
146        // In this case we save the additional position to DB, which will
147        // lead the user to reformat his alignment (which will add a gap
148        // at end of SAI HELIX)
149    }
150    return copy;
151}
152
Note: See TracBrowser for help on using the repository browser.