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));
25 uint ClaveVariable::Size () const
27 return data.size ()*sizeof (uchar)+sizeof (uint);
30 uchar *ClaveVariable::ToArray () const
33 uint len = data.size ();
34 out = new uchar[Size ()];
35 memcpy (out, &len, sizeof (uint));
36 memcpy (out+sizeof(uint), data.c_str (), data.size ()*sizeof (uchar));
40 Clave *ClaveVariable::Clone () const
42 ClaveVariable *k = new ClaveVariable (*this);
46 bool ClaveVariable::operator < (const Clave &c) const
48 return data < ((ClaveVariable&)c).data;
51 bool ClaveVariable::operator == (const Clave &c) const
53 return data == ((ClaveVariable&)c).data;
56 bool ClaveVariable::Abrev (ClaveVariable *c)
58 if (c == NULL) return false;
62 resto = abreviar (c->data, data, iguales);
64 /* No hago nada si solo machea un caracter */
65 if (iguales < 2) return false;
76 bool ClaveVariable::DesAbrev (ClaveVariable *c)
78 if (c == NULL) return false;
80 int pos = data.find ("|");
82 /* No esta abreviada! */
83 if (pos <= 0) return false;
85 std::string siguales = data.substr (0, pos);
86 int iguales = atoi (siguales.c_str ());
88 std::string primera_parte = c->data.substr (0, iguales);
89 std::string segunda_parte = data.substr (pos+1, data.size () - pos - 1);
91 data = primera_parte + segunda_parte;
96 std::string ClaveVariable::abreviar(std::string &p, std::string &a, int &iguales)
98 char *primera = (char *)p.c_str ();
99 char *actual = (char *)a.c_str ();
102 while (((*primera) != '\0') && ((*actual) != '\0')) {
103 if ((*primera) == (*actual)) {
108 /* No coinciden mas! */
113 return std::string (actual);
116 uchar *ClaveVariable::ToRaw (uint &size) const
119 size = raw_data.size ()*sizeof (uchar)+sizeof (uint);
120 uint len = raw_data.size ();
121 out = new uchar[size];
122 memcpy (out, &len, sizeof (uint));
123 memcpy (out+sizeof(uint), raw_data.c_str (), raw_data.size ()*sizeof (uchar));