Example #1
0
int any(char const s1[], char const s2[])
{
  int i, pos;

  if (s1 == NULL || s2 == NULL)
  {
    return -1;
  }

  for (i = 0; s1[i] != '\0'; ++i)
  {
    if ((pos = getIndexOfString(s1[i], s2)) != -1)
    {
      return pos;
    }
  }

  return -1;
}
/**
 * This method performs the real encoding process. It opens
 * the files for input and output, and then executes the LZ78
 * algorithm, writing the outputs in the output file.
 *
 * @param inputFileName The name of the input file.
 */
void LempelZiv78Encoder::doEncoding(char *inputFileName)
{
  FILE *input;
  FILE *output;
  std::string outputFileName;
  char* dotPointer;
  char c;
  std::string x("");
  Binarizer* binarizer = new Binarizer();

  input = fopen(inputFileName, "r");

  if (input == NULL)
  {
    printf("Fichero de entrada no encontrado.\n");
    exit(-1);
  }

  dotPointer = strchr(inputFileName, '.');

  if (dotPointer != NULL)

    *dotPointer = '\0';
    
  outputFileName = inputFileName;
  outputFileName += ".lz78";

  output = fopen(outputFileName.c_str(), "w");
  
  if (output == NULL)
  {
    printf("Fichero de salida no pudo crearse.\n");
    exit(-1);
  }

  c = fgetc(input);

  while (c != EOF)
  {
    if (stringExistsInDict(x + c))
      x = x + c;
    else
    {
      if (stringExistsInDict(x))
      {
        outputs.push_back(std::make_pair(getIndexOfString(x), c));
        printf("%d %c ", getIndexOfString(x), c);
        printf(" -- %d -- %s\n", dictionary.size() + 1, (x + c).c_str()); 
        dictionary[dictionary.size() + 1] = x + c;
        
      }
      else
      {
        outputs.push_back(std::make_pair(0, c));
        dictionary[dictionary.size() + 1] = c;
        printf("0 %c\n", c);
      }
      x = "";
    }
    c = fgetc(input);
  }
  
  if (x.size() != 0)
  {
    // emitimos la salida correspondiente a
    std::string pref_x(x);
    pref_x.erase(x.size() - 1);
    outputs.push_back(std::make_pair(getIndexOfString(pref_x), x[x.size() - 1]));
    printf("%d ", getIndexOfString(x));
    printf(" -- ?? -- %s\n", x.c_str()); 
  }

  // guardamos eof en dicc. y imprimimos su entrada.
  outputs.push_back(std::make_pair(0, c));
  dictionary[dictionary.size() + 1] = c;
  printf("0 %c\n", c);

  // Ahora hay que imprimir las salidas

  unsigned int index_bits = 1;

  while (pow(2, index_bits) < dictionary.size()) index_bits++;
  printf("ds: %d bits: %d\n", dictionary.size(), index_bits);

  for (unsigned int i = 0; i < outputs.size(); i++)
  {
     binarizer->addStringToCode(toBinaryString(outputs[i].first, index_bits).c_str());
     binarizer->addStringToCode(instant_codes[outputs[i].second].c_str());
     //printf("%s %s\n", toBinaryString(outputs[i].first, index_bits).c_str(), instant_codes[outputs[i].second].c_str());
  }

  fprintf(output, "%d %d %d\n", dictionary.size(), binarizer->getCodeLength(), binarizer->getOffset());
  for (unsigned int i = 1; i <= dictionary.size(); i++)
    fprintf(output, "%d %s\n", dictionary[i].size(), dictionary[i].c_str());

  std::string code = binarizer->getBinaryCode();

  for (unsigned int i = 0; i < binarizer->getCodeLength(); i++)
    fprintf(output, "%c", code[i]);

  fclose(input);
  fclose(output);
}
/**
 * This method performs the real encoding process. It opens
 * the files for input and output, and then executes the LZW
 * algorithm, writing the outputs in the output file.
 *
 * @param inputFileName The name of the input file.
 */
void LempelZivWEncoder::doEncoding(char *inputFileName)
{
  FILE *input;
  FILE *output;
  std::string outputFileName;
  char* dotPointer;
  char c;
  std::string x("");
  Binarizer* binarizer = new Binarizer();

  input = fopen(inputFileName, "r");

  if (input == NULL)
  {
    printf("Fichero de entrada no encontrado.\n");
    exit(-1);
  }

  dotPointer = strchr(inputFileName, '.');

  if (dotPointer != NULL)

    *dotPointer = '\0';
    
  outputFileName = inputFileName;
  outputFileName += ".lzw";

  output = fopen(outputFileName.c_str(), "w");
  
  if (output == NULL)
  {
    printf("Fichero de salida no pudo crearse.\n");
    exit(-1);
  }

  c = fgetc(input);

  while (c != EOF)
  {
    if (stringExistsInDict(x + c))
      x = x + c;
    else
    {
        outputs.push_back(getIndexOfString(x));
        printf("%d ", getIndexOfString(x));
        printf(" -- %d -- %s\n", dictionary.size() + 1, (x + c).c_str()); 
        dictionary[dictionary.size() + 1] = x + c;
        x = c;
    }
    c = fgetc(input);
  }

  if (stringExistsInDict(x + c))
    x = x + c;
  else
  {
      outputs.push_back(getIndexOfString(x));
      printf("%d ", getIndexOfString(x));
      printf(" -- %d -- %s\n", dictionary.size() + 1, (x + c).c_str()); 
      dictionary[dictionary.size() + 1] = x + c;
      x = c;
  }
    outputs.push_back(getIndexOfString(x));
    printf("%d ", getIndexOfString(x));
    printf(" -- %d -- %s\n", dictionary.size() + 1, (x).c_str()); 

  // Ahora hay que imprimir las salidas

  unsigned int index_bits = 1;

  while (pow(2, index_bits) < dictionary.size()) index_bits++;
  printf("ds: %d bits: %d\n", dictionary.size(), index_bits);

  for (unsigned int i = 0; i < outputs.size(); i++)
     binarizer->addStringToCode(toBinaryString(outputs[i], index_bits).c_str());

  fprintf(output, "%d %d\n", dictionary.size(), binarizer->getOffset());
  for (unsigned int i = 0; i < dictionary.size(); i++)
    fprintf(output, "%s\n", dictionary[i].c_str());

  std::string code = binarizer->getBinaryCode();

  fprintf(output, "%s", code.c_str());

  fclose(input);
  fclose(output);
}