File: TitleTable.cpp

package info (click to toggle)
pbseqlib 5.3.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,020 kB
  • sloc: cpp: 77,246; python: 331; sh: 103; makefile: 42
file content (119 lines) | stat: -rw-r--r-- 2,662 bytes parent folder | download | duplicates (3)
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
#include <pbdata/metagenome/TitleTable.hpp>

TitleTable::TitleTable()
{
    table = NULL;
    tableLength = 0;
}

TitleTable::~TitleTable() { Free(); }

void TitleTable::Copy(char **src, int nSrc)
{
    Free();  //Free before copy
    table = ProtectedNew<char *>(nSrc);
    tableLength = nSrc;
    int i;
    for (i = 0; i < nSrc; i++) {
        int lenStrI = strlen(src[i]);
        table[i] = ProtectedNew<char>(lenStrI + 1);
        memcpy(table[i], src[i], lenStrI);
        table[i][lenStrI] = '\0';
    }
}

void TitleTable::Write(std::string &name)
{
    std::ofstream out;
    CrucialOpen(name, out, std::ios::out);
    Write(out);
}

void TitleTable::Write(std::ofstream &out)
{
    int i;
    for (i = 0; i < tableLength; i++) {
        out << table[i] << std::endl;
    }
}

void TitleTable::Read(std::string &inFileName)
{
    std::ifstream in;
    CrucialOpen(inFileName, in, std::ios::in);
    Read(in);
}

void TitleTable::CopyFromVector(std::vector<std::string> &titles)
{
    Free();  //Free before copy.
    tableLength = titles.size();
    table = ProtectedNew<char *>(tableLength);
    int i;
    for (i = 0; i < tableLength; i++) {
        table[i] = ProtectedNew<char>(titles[i].size() + 1);
        memcpy(table[i], titles[i].c_str(), titles[i].size());
        table[i][titles[i].size()] = '\0';
    }
}

void TitleTable::Read(std::ifstream &in)
{
    std::vector<std::string> titles;
    while (in) {
        char title[1024];
        in.getline(title, 1024);
        if (not std::string(title).empty()) titles.push_back(title);
    }
    if (titles.size() > 0) {
        CopyFromVector(titles);
    } else {
        tableLength = 0;
        table = NULL;
    }
}

void TitleTable::Free()
{
    int i;
    for (i = 0; i < tableLength; i++) {
        if (table[i]) {
            delete[] table[i];
            table[i] = NULL;
        }
    }
    if (table) {
        delete[] table;
    };
    table = NULL;
    tableLength = 0;
}

bool TitleTable::Lookup(std::string title, int &index)
{
    int i;
    for (i = 0; i < tableLength; i++) {
        if (table[i] == title) {
            index = i;
            return true;
        }
    }
    return false;
}

void TitleTable::ResetTableToIntegers(char **table, int *tableLengths, int nTable)
{

    int i;
    for (i = 0; i < nTable; i++) {
        delete[] table[i];
        std::stringstream namestrm;
        namestrm << i;
        std::string name;
        name = namestrm.str();
        table[i] = ProtectedNew<char>(name.size() + 1);
        memcpy(table[i], name.c_str(), name.size());
        table[i][name.size()] = '\0';
        tableLengths[i] = (int)name.size() + 1;
    }
}