
#include <exec/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/exec.h>

#define _USEOLDEXEC_ 

struct DosLibrary		*DOSBase 		= NULL;


struct MainData
{
    LONG DosBase,           

     GfxBase,            
     IntuitionBase,      
     LayersBase,         
     DiskFontBase,       
     WBBase,             
     ASLBase,
     IFFParseBase,
     RexxBase,
     AmigaGuideBase,
     LocaleBase,
     FirstWindow ,       
     FirstSheet,         
     FirstFont,          
     FontList,           
     LocaleCatalog,      
     AmigaGuideHandle,   
     Screen,             
     Com_DiskErrorAdr;   
};


#define Decine \
switch(cur)\
{\
    case '1':	switch(next)\
                {\
                    case '0': strcat(to,"dieci"); break;\
                    case '1': strcat(to,"undici"); break;\
                    case '2': strcat(to,"dodici"); break;\
                    case '3': strcat(to,"tredici"); break;\
                    case '4': strcat(to,"quattordici"); break;\
                    case '5': strcat(to,"quindici"); break;\
                    case '6': strcat(to,"sedici"); break;\
                    case '7': strcat(to,"diciassette"); break;\
                    case '8': strcat(to,"diciotto"); break;\
                    case '9': strcat(to,"diciannove"); break;\
                }\
    break;\
    case '2':   if ((next=='1')||(next=='8')) strcat(to,"vent");\
                else strcat(to,"venti");\
    break;\
    case '3':   if ((next=='1')||(next=='8')) strcat(to,"trent");\
                else strcat(to,"trenta");\
    break;\
    case '4':   if ((next=='1')||(next=='8')) strcat(to,"quarant");\
                else strcat(to,"quaranta");\
    break;\
    case '5':   if ((next=='1')||(next=='8')) strcat(to,"cinquant");\
                else strcat(to,"cinquanta");\
    break;\
    case '6':   if ((next=='1')||(next=='8')) strcat(to,"sessant");\
                else strcat(to,"sessanta");\
    break;\
    case '7':   if ((next=='1')||(next=='8')) strcat(to,"settant"); \
                else strcat(to,"settanta"); \
    break;\
    case '8':   if ((next=='1')||(next=='8')) strcat(to,"ottant");\
                else strcat(to,"ottanta"); \
    break;\
    case '9':   if ((next=='1')||(next=='8')) strcat(to,"novant");\
                else  strcat(to,"novanta");\
    break;\
}

#define Unita \
switch(cur)                                        \
{                                                  \
    case '1':    strcat(to,"un"     ); break; \
    case '2':    strcat(to,"due"	 );	break; \
    case '3':    strcat(to,"tre"    );	break; \
    case '4':    strcat(to,"quattro"); break; \
    case '5':    strcat(to,"cinque" );	break; \
    case '6':    strcat(to,"sei"    );	break; \
    case '7':    strcat(to,"sette"  );	break; \
    case '8':    strcat(to,"otto"   );	break; \
    case '9':    strcat(to,"nove"   );	break; \
} \


// Converts a 3 digit string to a character string

void __inline ncc(char from[] , char to[])
{
	char cur,next;

	cur  = from[0];

	if (cur != '1' && cur != '0')
	{  
		Unita

		strcat(to,"cento"); 
	}
	else if (cur=='1')
		strcat(to,"cento"); 

	cur  = from[1];
	next = from[2];

    Decine; 

	if(cur != '1')
	{
		cur  = from[2];
	    Unita; 
	}
}

void __inline n2c(char from[] , char to[], LONG opt)
{
	int len,p,parti,resto;

	char tmp[13];						

	char g[4];

	tmp[0] = '0';
	tmp[1] = '0';
	tmp[2] = '\0';


	len   = strlen(from);
	resto = len % 3;


	if(resto != 0)
	{
		LONG rsp = 3 - resto;

		strcpy(tmp+rsp,from); 			// Sovrascrive partendo da 1 o da 2, ovvero da (3 - resto) (Max: 2)

		len += rsp;						
	}
	else
		strcpy(tmp,from);             	// Sovrascrive partendo da 0


	parti = len / 3;					


	#define fg (3 * p)				


	#define cond0 ((g[2] == '1') && (g[1] != '1'))
	#define cond1 (((g[2]!= '1') && (g[2] != '0'))|| (g[1] != '0') || (g[0] != '0'))
	#define cond2 ((g[2] == '1') && (g[1] == '0') && (g[0] == '0'))



	for(p = 0; p < parti; p++)
	{
		g[0] = tmp[fg  ];
		g[1] = tmp[fg+1];
		g[2] = tmp[fg+2];
		g[3] = '\0';

		switch(parti-p)
		{
			case 1:						

				if(opt == 0)			// IF options > 1 -> if(opt > 0)
				{
					ncc(g,to);

					if(cond0)
						strcat(to,"o");
				}
				else
					strcat(to,g);

				break;

			case 2:						
				if(cond1)
				{
					ncc(g,to);
					strcat(to,"mila");
				}
				else if(cond2) strcat(to,"mille");

				break;

			case 3:						
				if(cond1)
				{
					ncc(g,to);
					strcat(to,"milioni");
				}
				else if(cond2) strcat(to,"unmilione");

				break;

			case 4:						
				if(cond1)
				{
					ncc(g,to);
					strcat(to,"miliardi");
				}
				else if(cond2) strcat(to,"unmiliardo");
				break;
		}
	}
}



// a2: char *		(destination buffer)
// d2: LONG			(source number	   )
// d3: LONG			(options		   )
// a5: MainData * 	


// Options:			(mutual exclusive)
//
// 0 = normal form	i.e. millecinquecento
// 1 = short form	i.e. mille500

extern __asm __saveds void CTCL_n2c(register __a2 char to[], register __d2 LONG from, register __d3 LONG opt, register __a5 struct MainData *md)
{
	char num[13];

	DOSBase = (struct DosLibrary *) md->DosBase;

	stcl_d(num,from);					// From LONG to char	(from -> num)

	n2c(num,to,opt);					// From numeric string to char string (num -> to)
}

