File: entry.cpp

package info (click to toggle)
libgetdata 0.11.0-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,144 kB
  • sloc: ansic: 100,814; cpp: 4,843; fortran: 4,548; f90: 2,561; python: 2,406; perl: 2,274; makefile: 1,487; php: 1,465; sh: 86
file content (228 lines) | stat: -rw-r--r-- 5,187 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
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
225
226
227
228
// Copyright (C) 2008-2014 D. V. Wiebe
//
///////////////////////////////////////////////////////////////////////////
//
// This file is part of the GetData project.
//
// GetData is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 2.1 of the License, or (at your
// option) any later version.
//
// GetData is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GetData; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
#include "internal.h"

using namespace GetData;

Entry::Entry()
{
  memset(&E, 0, sizeof(E));
  D = NULL;
}

Entry::Entry(const GetData::Dirfile *dirfile, const char* field_code)
{
  D = dirfile;
  if (gd_entry(D->D, field_code, &E))
    memset(&E, 0, sizeof(E));
}

Entry::~Entry()
{
  gd_free_entry_strings(&E);
}

int Entry::CheckIndex(gd_entype_t field_type, int n_fields, int index)
{
  if (index < 0)
    return 0;

  switch (field_type) {
    case GD_RAW_ENTRY:
    case GD_INDEX_ENTRY:
    case GD_CONST_ENTRY:
    case GD_CARRAY_ENTRY:
    case GD_SARRAY_ENTRY:
    case GD_STRING_ENTRY:
    case GD_NO_ENTRY:
    case GD_ALIAS_ENTRY:
      return 0;
    case GD_LINCOM_ENTRY:
      if (index > n_fields)
        return 0;
      break;
    case GD_MULTIPLY_ENTRY:
    case GD_DIVIDE_ENTRY:
    case GD_INDIR_ENTRY:
    case GD_SINDIR_ENTRY:
    case GD_WINDOW_ENTRY:
    case GD_MPLEX_ENTRY:
      if (index > 2)
        return 0;
      /* Fallthrough */
    case GD_LINTERP_ENTRY:
    case GD_BIT_ENTRY:
    case GD_PHASE_ENTRY:
    case GD_POLYNOM_ENTRY:
    case GD_SBIT_ENTRY:
    case GD_RECIP_ENTRY:
      if (index > 1)
        return 0;
  }

  return 1;
}

int Entry::Move(int new_fragment, unsigned flags)
{
  int ret = -1;

  if (D != NULL)
    ret = gd_move(D->D, E.field, new_fragment, flags);

  if (!ret)
    E.fragment_index = new_fragment;

  return ret;
}

int Entry::Rename(const char* new_name, unsigned flags)
{
  char* ptr;
  int ret = -1;

  if (D != NULL)
    ret = gd_rename(D->D, E.field, new_name, flags);

  if (ret) {
    if (E.field == NULL) {
      E.field = strdup(new_name);
    } else {
      /* this buffer is used if E is a metafield, in which case we'll
       * replace the subfield name in E.field with new_name.  The length
       * of the new code is
       *
       *   strlen(E.field) - strlen(<subfield-name>) + strlen(new_name)
       *     + 1 (for the trailing NUL).
       *
       * The subfield name in E.field must be at least one character long, so
       * the length of the new code is at most:
       *
       *   strlen(E.field) - 1 + strlen(new_name) + 1
       *   = strlen(E.field) + strlen(new_name)
       */
      char* nn = (char*)malloc(strlen(E.field) + strlen(new_name));
      strcpy(nn, E.field);
      ptr = strchr(nn, '/');

      if (ptr) { /* metafield */
        strcpy(ptr + 1, new_name);
      } else {
        free(nn);
        nn = strdup(new_name);
      }

      free(E.field);
      E.field = nn;
    }
  }

  return ret;
}

void Entry::SetDirfile(const GetData::Dirfile* dirfile)
{
  D = dirfile;
}

void Entry::SetName(const char* name)
{
  this->Rename(name);
}

void Entry::SetFragmentIndex(int fragment_index)
{
  this->Move(fragment_index);
}

static inline int scalar_ok(const gd_entry_t &E, int index)
{
  if (index < 0)
    return 0;

  switch (E.field_type) {
    case GD_LINCOM_ENTRY:
      if (index >= GD_MAX_LINCOM + E.u.lincom.n_fields ||
          (index >= E.u.lincom.n_fields && index < GD_MAX_LINCOM))
      {
        return 0;
      }
      break;
    case GD_POLYNOM_ENTRY:
      if (index > E.u.polynom.poly_ord)
        return 0;
      break;
    case GD_BIT_ENTRY:
    case GD_SBIT_ENTRY:
    case GD_MPLEX_ENTRY:
      if (index >= 2)
        return 0;
      break;
    case GD_RAW_ENTRY:
    case GD_PHASE_ENTRY:
    case GD_RECIP_ENTRY:
    case GD_WINDOW_ENTRY:
      if (index >= 1)
        return 0;
      break;
    case GD_LINTERP_ENTRY:
    case GD_MULTIPLY_ENTRY:
    case GD_DIVIDE_ENTRY:
    case GD_INDIR_ENTRY:
    case GD_SINDIR_ENTRY:
    case GD_INDEX_ENTRY:
    case GD_CONST_ENTRY:
    case GD_CARRAY_ENTRY:
    case GD_SARRAY_ENTRY:
    case GD_STRING_ENTRY:
    case GD_NO_ENTRY:
    case GD_ALIAS_ENTRY:
      return 0;
  }

  return 1;
}

const char *Entry::Scalar(int index) const
{
  return scalar_ok(E, index) ? E.scalar[index] : NULL;
}

int Entry::ScalarIndex(int index) const
{
  return scalar_ok(E, index) ? E.scalar_ind[index] : 0;
}

void Entry::SetScalar(int n, const char *code)
{
  free(E.scalar[n]);
  if (code == NULL)
    E.scalar[n] = NULL;
  else {
    E.scalar[n] = strdup(code);
    char *ptr = strchr(E.scalar[n], '<');
    if (ptr) {
      *ptr = '\0';
      E.scalar_ind[n] = atoi(ptr + 1);
    } else
      E.scalar_ind[n] = -1;
  }
}