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
|
// Copyright (C) 2008-2010 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
//
#ifdef HAVE_CONFIG_H
#include "../../src/config.h"
#endif
#undef GETDATA_LEGACY_API
#include "getdata/dirfile.h"
#include <stdlib.h>
#include <cstring>
using namespace GetData;
Entry::Entry()
{
memset(&E, 0, sizeof(E));
D = NULL;
}
Entry::Entry(const GetData::Dirfile *dirfile, const char* field_code)
{
D = dirfile;
gd_entry(D->D, field_code, &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:
return 0;
case GD_LINCOM_ENTRY:
if (index > n_fields)
return 0;
break;
case GD_MULTIPLY_ENTRY:
case GD_DIVIDE_ENTRY:
if (index > 2)
return 0;
default:
if (index > 1)
return 0;
}
return 1;
}
int Entry::Move(int new_fragment, int move_data)
{
int ret = -1;
if (D != NULL)
ret = gd_move(D->D, E.field, new_fragment, move_data);
if (!ret)
E.fragment_index = new_fragment;
return ret;
}
int Entry::Rename(const char* new_name, int move_data)
{
char* ptr;
int ret = -1;
if (D != NULL)
ret = gd_rename(D->D, E.field, new_name, move_data);
if (ret) {
if (E.field == NULL) {
E.field = strdup(new_name);
} else {
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:
if (index >= 2)
return 0;
break;
case GD_RAW_ENTRY:
case GD_PHASE_ENTRY:
case GD_RECIP_ENTRY:
if (index >= 1)
return 0;
break;
case GD_LINTERP_ENTRY:
case GD_MULTIPLY_ENTRY:
case GD_DIVIDE_ENTRY:
case GD_INDEX_ENTRY:
case GD_CONST_ENTRY:
case GD_CARRAY_ENTRY:
case GD_STRING_ENTRY:
case GD_NO_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;
}
}
|