X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/blobdiff_plain/2ab8041933e77875de62bc73e76febc528af258e..58f5f2e6ba6f33bc457e98ec883f2834c9a5fe6f:/src/clave_variable.cpp?ds=inline diff --git a/src/clave_variable.cpp b/src/clave_variable.cpp index 6534764..47d81ba 100644 --- a/src/clave_variable.cpp +++ b/src/clave_variable.cpp @@ -18,7 +18,6 @@ ClaveVariable::ClaveVariable (uchar *n) memcpy (str, n, sizeof (uchar)*len); str[len] = '\0'; data = std::string ((const char *)(str)); - std::cout << "CREE : " << data << std::endl; delete [] str; } @@ -53,3 +52,64 @@ 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); + std::cout << siguales << std::endl; + 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); +} +