/* 

GERADORDENOME.C - Automated project name generator

Copyright (C) 2006 Ricardo Birmann

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
MA 02110-1301, USA.

Comments and variable names are in brazilian portugese, sorry!

*/




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


int randAte(int max) {
  int aux;
  float faux;
  
 getRand:
  
  aux = rand();
 
  if(aux <= 0) goto getRand;
  
  faux = (float)aux / RAND_MAX;
  
  aux = (int)(faux * max);

  return aux;
  
}

char pegaUmaLetra(int *anteriores) {

  int vocalidade;
  char letra;

  letra = (char)(97 + randAte(26));
  if (letra=='a' || letra=='e' || letra=='i' || letra=='o' || letra=='u')
    vocalidade = 0;
  else vocalidade = 1;
  
  if(*(anteriores + 1) == 1 && vocalidade == 1) 
    // trava para impedir duas consoantes seguidas
    return pegaUmaLetra(anteriores);

  if(*anteriores == vocalidade && *(anteriores + 1) == vocalidade) 
    /* as duas ultimas letras tem a mesma vocalidade que a atual
       precisamos pegar uma outra */
    return pegaUmaLetra(anteriores);

  *anteriores = *(anteriores + 1);
  *(anteriores + 1) = vocalidade;

  return letra;
}
  
void geraEImprimeUmNome() {

  int numeroDeLetras=0, i=0;
  int anteriores[2]={-1,-1};// vocalidades anteriores: 0 = vogal; 1 = consoante; -1 = ?

  numeroDeLetras = 4 + randAte(3);

  for(i = 0;i < numeroDeLetras; i++) {      
    //    printf("ant: %d %d \t letra %c\n", anteriores[0], anteriores[1], letra);
    printf("%c", pegaUmaLetra(anteriores));
  }

  return;
}
  
  
int main()
{

  int i;
  float faux;

  srand( (unsigned)time( NULL ) );

  geraEImprimeUmNome();
  //  printf("\n");
  
}
