3 #include "clave_variable.h"
6 ClaveVariable::ClaveVariable (const std::string &s, uint bd)
12 ClaveVariable::ClaveVariable (uchar *n)
16 memcpy (&len, n, sizeof (uint));
17 uchar *str = new uchar[len+1];
19 memcpy (str, n, sizeof (uchar)*len);
21 data = std::string ((const char *)(str));
23 n += data.size () * sizeof (uchar);
24 memcpy (&block_data, n, sizeof (uint));
29 uint ClaveVariable::Size () const
31 return data.size ()*sizeof (uchar)+sizeof (uint)*2;
34 uchar *ClaveVariable::ToArray () const
37 uint len = data.size ();
38 out = new uchar[Size ()];
40 memcpy (out, &len, sizeof (uint));
42 memcpy (out+x, data.c_str (), data.size ()*sizeof (uchar));
43 x += data.size () * sizeof (uchar);
44 memcpy (out+x, &block_data, sizeof (uint));
49 Clave *ClaveVariable::Clone () const
51 ClaveVariable *k = new ClaveVariable (*this);
55 bool ClaveVariable::operator < (const Clave &c) const
57 return data < ((ClaveVariable&)c).data;
60 bool ClaveVariable::operator == (const Clave &c) const
62 return data == ((ClaveVariable&)c).data;
65 bool ClaveVariable::Abrev (ClaveVariable *c)
67 if (c == NULL) return false;
71 resto = abreviar (c->data, data, iguales);
73 /* No hago nada si solo machea un caracter */
74 if (iguales < 2) return false;
85 bool ClaveVariable::DesAbrev (ClaveVariable *c)
87 if (c == NULL) return false;
89 int pos = data.find ("|");
91 /* No esta abreviada! */
92 if (pos <= 0) return false;
94 std::string siguales = data.substr (0, pos);
95 int iguales = atoi (siguales.c_str ());
97 std::string primera_parte = c->data.substr (0, iguales);
98 std::string segunda_parte = data.substr (pos+1, data.size () - pos - 1);
100 data = primera_parte + segunda_parte;
105 std::string ClaveVariable::abreviar(std::string &p, std::string &a, int &iguales)
107 char *primera = (char *)p.c_str ();
108 char *actual = (char *)a.c_str ();
111 while (((*primera) != '\0') && ((*actual) != '\0')) {
112 if ((*primera) == (*actual)) {
117 /* No coinciden mas! */
122 return std::string (actual);
125 uchar *ClaveVariable::ToRaw (uint &size) const
128 size = raw_data.size ()*sizeof (uchar)+sizeof (uint);
129 uint len = raw_data.size ();
130 out = new uchar[size];
131 memcpy (out, &len, sizeof (uint));
132 memcpy (out+sizeof(uint), raw_data.c_str (), raw_data.size ()*sizeof (uchar));