/*
#include <stdio.h>
*/

#define MAXXEMOT 2000


char xe_gifs[MAXXEMOT][30];
char xe_pars[MAXXEMOT][30];
int xe_count = 0;

int
xemoticon_insert (char *gif, char *parse)
{
  int i, len;

  len = strlen (parse);
  if (strlen (gif) > 29)
    return 0;
  if (len > 29)
    return 0;

  if (xe_count == MAXXEMOT)
    return 0;

  for (i = xe_count; i > 0; i--)
    {
      if (len > strlen (xe_pars[i - 1]))
	{
	  sprintf (xe_gifs[i], "%s", gif);
	  sprintf (xe_pars[i], "%s", parse);
	  xe_count++;
	  return 0;
	}
      else
	{
	  sprintf (xe_gifs[i], "%s", xe_gifs[i - 1]);
	  sprintf (xe_pars[i], "%s", xe_pars[i - 1]);
	}
    }

  sprintf (xe_gifs[0], "%s", gif);
  sprintf (xe_pars[0], "%s", parse);

  xe_count++;
  return 0;
}

int
xemoticon_load (char *dir)
{
  FILE *plik;
  char filename[512];
  char line[3001];
  char *k, *r, *w;
  char *gif, *egif;

  snprintf (filename, sizeof (filename), "%s/1/emots.txt", dir);
  if (!(plik = fopen (filename, "r")))
    {
      // no file found
      return 0;
    }
  while (fgets (line, 3000, plik))
    {
      k = (char *) index (line, 10);
      if (k)
	k[0] = 0;
      k = (char *) index (line, 13);
      if (k)
	k[0] = 0;
      if (strlen (line) > 0)
	{
	  k = line;
	  if (k[0] == '*')
	    k++;
	  if (k[0] == '(')
	    {
	      k++;
	      gif = (char *) strstr (k, "\"),\"");
	      if (gif)
		{
		  gif += 4;
		  egif = (char *) index (gif, '"');
		  if (egif)
		    {
		      egif[0] = 0;
		      for (r = k; r < gif; r++)
			{
			  w = (char *) index (r + 1, '"');
			  if (w)
			    {
			      w++[0] = 0;
			      if (strlen (r + 1) > 0)
				xemoticon_insert (gif, r + 1);
			      r = w;
			    }
			}
		    }
		}
	    }
	  else
	    {
	      gif = (char *) strstr (k, "\",\"");
	      if (gif)
		{
		  gif += 3;
		  egif = (char *) index (gif, '"');
		  if (egif)
		    {
		      egif[0] = 0;
		      w = (char *) index (k + 1, '"');
		      if (w)
			{
			  w++[0] = 0;
			  if (strlen (k + 1) > 0)
			    xemoticon_insert (gif, k + 1);
			}
		    }
		}
	    }
	}
    }
  fclose (plik);
  return 0;
}

/* main testowy
int main(void) {
int i;

xemoticon_load("/home/serek/.gg/emots/1");
printf("%d\n",xe_count);
for(i=0;i<xe_count;i++)
    printf("%s %s\n",xe_pars[i],xe_gifs[i]);

return 0;
}
*/
