/*
** combine.c for Combine in /
**
** Made by Averous Julien-Pierre
** Login   <j.averous@sourcemac.com>
**
** Started on  Sat May 12 12:48:19 2007 Averous Julien-Pierre
** Last update Sat May 12 12:48:35 2007 Averous Julien-Pierre
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Stock a word */
typedef struct
{
  char*		word;
  unsigned int	size;
}		s_word;

/* Stock parameters */
typedef struct
{
  char*	fpath_in;
  char*	fpath_out;
  char	sep_in;
  char* sep_out;
  char* sepw_out;
  int	sz_sep_out;
  int	sz_sepw_out;
}	s_usage;

/* Prototype of parameter manager */
int usage (int argc, char** argv, s_usage* u);


/*!
** Main callpoint
**
** @param argc Argument count
** @param argv Argument tab
**
** @return Error code
*/
int main (int argc, char* argv[])
{
  s_word*		list = NULL;
  unsigned int		sz_list = 0;
  s_word		word;
  unsigned int		i, j;
  s_usage		use;

  FILE*			f_in;
  FILE*			f_out;

  int			c;

  /* Get parameter */
  memset ((void*)&use, 0, sizeof (s_usage));
  if (!usage (argc, argv, &use))
    return 1;

  /* Open/create files */
  if (!(f_in = fopen (use.fpath_in, "r")))
  {
    fprintf (stderr, "Can't open file\n");
    return 2;
  }
  if (!(f_out = fopen (use.fpath_out, "w")))
  {
    fprintf (stderr, "Can't create file\n");
    return 3;
  }

  /* Loading word list */
  while (!feof (f_in))
  {
    word.word = NULL;
    word.size = 0;

    while ((c = fgetc (f_in)) != EOF && c != use.sep_in)
    {
      word.word = realloc (word.word, word.size + 1);
      word.word[word.size] = c;
      word.size++;
    }
    if (!word.word)
      continue;

    list = realloc (list, (sz_list + 1) * sizeof (s_word));
    list[sz_list] = word;
    sz_list++;
  }

  /* Generating combine with word list */
  for (i = 0; i < sz_list; i++)
  {
    for (j = 0; j < sz_list; j++)
    {
      fwrite (list[i].word, list[i].size, 1, f_out);

      if (use.sep_out)
	fwrite (use.sep_out, use.sz_sep_out, 1, f_out);

      fwrite (list[j].word, list[j].size, 1, f_out);

      if (use.sepw_out)
	fwrite (use.sepw_out, use.sz_sepw_out, 1, f_out);
    }
  }

  /* Close file */
  fclose (f_in);
  fclose (f_out);

  /* No error */
  return 0;
}


/*!
** Get application arguments
**
** @param argc Argument count
** @param argv Argument list
** @param u Output getted parameters
**
** @return false (0) if there is an error, true (1) else
*/
int usage (int argc, char** argv, s_usage* u)
{
  if (!u)
    return 0;

  /* There is not a good number of argument : print usage */
  if (argc < 4 || argc > 6)
  {
    fprintf (stderr, "usage : %s input-word-file input-word-separator output-word-file [output-combine-separator] [output-word-separator]\n", argv[0]);
    return 0;
  }


  u->fpath_in = argv[1];
  u->sep_in = argv[2][0];

  u->fpath_out = argv[3];

  if (argc >= 5)
  {
    u->sep_out = argv[4];
    u->sz_sep_out = strlen (u->sep_out);
  }
  if (argc >= 6)
  {
    u->sepw_out = argv[5];
    u->sz_sepw_out = strlen (u->sepw_out);
  }

  return 1;
}
