X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/blobdiff_plain/ed3aa487f3460bdd8387d80a63589b0e04aa0396..59b7476481f01388fcfd13492adbba7ef841755c:/src/clave_variable.cpp diff --git a/src/clave_variable.cpp b/src/clave_variable.cpp index c4fd1f1..c97b9d9 100644 --- a/src/clave_variable.cpp +++ b/src/clave_variable.cpp @@ -3,9 +3,10 @@ #include "clave_variable.h" #include -ClaveVariable::ClaveVariable (const std::string &s) +ClaveVariable::ClaveVariable (const std::string &s, uint bd) { data = s; + block_data = bd; } ClaveVariable::ClaveVariable (uchar *n) @@ -19,11 +20,15 @@ ClaveVariable::ClaveVariable (uchar *n) str[len] = '\0'; data = std::string ((const char *)(str)); delete [] str; + n += data.size () * sizeof (uchar); + memcpy (&block_data, n, sizeof (uint)); + + raw_data = data; } uint ClaveVariable::Size () const { - return data.size ()+sizeof (uint); + return data.size ()*sizeof (uchar)+sizeof (uint)*2; } uchar *ClaveVariable::ToArray () const @@ -31,8 +36,13 @@ uchar *ClaveVariable::ToArray () const uchar *out; uint len = data.size (); out = new uchar[Size ()]; + int x = 0; memcpy (out, &len, sizeof (uint)); - memcpy (out+sizeof(uint), data.c_str (), data.size ()); + x += sizeof (uint); + memcpy (out+x, data.c_str (), data.size ()*sizeof (uchar)); + x += data.size () * sizeof (uchar); + memcpy (out+x, &block_data, sizeof (uint)); + return out; } @@ -52,3 +62,74 @@ bool ClaveVariable::operator == (const Clave &c) const return data == ((ClaveVariable&)c).data; } +bool ClaveVariable::Abrev (ClaveVariable *c) +{ + if (c == NULL) return false; + int iguales = 0; + std::string resto; + + resto = abreviar (c->data, data, iguales); + + /* No hago nada si solo machea un caracter */ + if (iguales < 2) return false; + + std::stringstream ss; + ss << iguales; + ss << "|"; + ss << resto; + data = ss.str (); + + return true; +} + +bool ClaveVariable::DesAbrev (ClaveVariable *c) +{ + if (c == NULL) return false; + + int pos = data.find ("|"); + + /* No esta abreviada! */ + if (pos <= 0) return false; + + std::string siguales = data.substr (0, pos); + int iguales = atoi (siguales.c_str ()); + + std::string primera_parte = c->data.substr (0, iguales); + std::string segunda_parte = data.substr (pos+1, data.size () - pos - 1); + + data = primera_parte + segunda_parte; + + return true; +} + +std::string ClaveVariable::abreviar(std::string &p, std::string &a, int &iguales) +{ + char *primera = (char *)p.c_str (); + char *actual = (char *)a.c_str (); + + iguales = 0; + while (((*primera) != '\0') && ((*actual) != '\0')) { + if ((*primera) == (*actual)) { + primera++; + actual++; + iguales++; + } else { + /* No coinciden mas! */ + break; + } + } + + return std::string (actual); +} + +uchar *ClaveVariable::ToRaw (uint &size) const +{ + uchar *out; + size = raw_data.size ()*sizeof (uchar)+sizeof (uint); + uint len = raw_data.size (); + out = new uchar[size]; + memcpy (out, &len, sizeof (uint)); + memcpy (out+sizeof(uint), raw_data.c_str (), raw_data.size ()*sizeof (uchar)); + return out; +} +