3 #include "clave_variable.h"
6 ClaveVariable::ClaveVariable (const std::string &s)
11 ClaveVariable::ClaveVariable (uchar *n)
15 memcpy (&len, n, sizeof (uint));
16 uchar *str = new uchar[len+1];
18 memcpy (str, n, sizeof (uchar)*len);
20 data = std::string ((const char *)(str));
22 n += data.size () * sizeof (uchar);
23 memcpy (&block_data, n, sizeof (uint));
28 uint ClaveVariable::Size () const
30 return data.size ()*sizeof (uchar)+sizeof (uint)*2;
33 uchar *ClaveVariable::ToArray () const
36 uint len = data.size ();
37 out = new uchar[Size ()];
39 memcpy (out, &len, sizeof (uint));
41 memcpy (out+x, data.c_str (), data.size ()*sizeof (uchar));
42 x += data.size () * sizeof (uchar);
43 memcpy (out+x, &block_data, sizeof (uint));
48 Clave *ClaveVariable::Clone () const
50 ClaveVariable *k = new ClaveVariable (*this);
54 bool ClaveVariable::operator < (const Clave &c) const
56 return data < ((ClaveVariable&)c).data;
59 bool ClaveVariable::operator == (const Clave &c) const
61 return data == ((ClaveVariable&)c).data;
64 bool ClaveVariable::Abrev (ClaveVariable *c)
66 if (c == NULL) return false;
70 resto = abreviar (c->data, data, iguales);
72 /* No hago nada si solo machea un caracter */
73 if (iguales < 2) return false;
84 bool ClaveVariable::DesAbrev (ClaveVariable *c)
86 if (c == NULL) return false;
88 int pos = data.find ("|");
90 /* No esta abreviada! */
91 if (pos <= 0) return false;
93 std::string siguales = data.substr (0, pos);
94 int iguales = atoi (siguales.c_str ());
96 std::string primera_parte = c->data.substr (0, iguales);
97 std::string segunda_parte = data.substr (pos+1, data.size () - pos - 1);
99 data = primera_parte + segunda_parte;
104 std::string ClaveVariable::abreviar(std::string &p, std::string &a, int &iguales)
106 char *primera = (char *)p.c_str ();
107 char *actual = (char *)a.c_str ();
110 while (((*primera) != '\0') && ((*actual) != '\0')) {
111 if ((*primera) == (*actual)) {
116 /* No coinciden mas! */
121 return std::string (actual);
124 uchar *ClaveVariable::ToRaw (uint &size) const
126 std::stringstream ss;
138 std::string s = ss.str ();
140 uchar *out = new uchar[s.size ()];
141 memcpy (out, s.c_str (), s.size ());