#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "charfilter.h"
#include "configuration.h"

char *original_chars="¡ÆÊ£ÑÓ¦¬¯"
                     "±æê³ñó¶¼¿";
char *asciionly_chars="A,C'E,L/N'O'S'Z'Z."
                      "a,c'e,l/n'o's'z'z.";
#ifdef OS_AMIGAOS
char *amigapl_chars="ÂÊËÎÏÓÔÚÛ"
                    "âêëîïóôúû";
#endif

int alt_fprintf(int asciionly_mode, FILE *fd, const char *given_string, ...)
{
    char my_tempy1[8192];
    char my_tempy2[8192];
    int retcode;
    va_list marker;

    va_start(marker, given_string);
    vsprintf(my_tempy1, given_string, marker);
    if(asciionly_mode){
        filter_to_ascii(my_tempy1, my_tempy2, 8192);
        retcode=fprintf(fd, my_tempy2);
    }else{
#ifdef OS_AMIGAOS
        filter_fromto_amigapl_iso(my_tempy1, my_tempy2);
        retcode=fprintf(fd, my_tempy2);
#else
        retcode=fprintf(fd, my_tempy1);
#endif
    }
    va_end(marker);
    return(retcode);
}

void transform_from_ascii(int asciionly_mode, char *given_string)
{
    if(asciionly_mode)
        filter_from_ascii(given_string, given_string, -1);
}

#ifdef OS_AMIGAOS
void transform_fromto_amigapl_iso(char *given_string)
{
    filter_fromto_amigapl_iso(given_string, given_string);
}
#endif

const char *filter_to_ascii(const char *given_string, char *output_buffer, int maxchars)
{
    const char *current_read=given_string;
    char *current_write=output_buffer;
    char current_char;
    int avail_chars=maxchars-2; /* safety measure */

    while ((current_char=*current_read) && avail_chars) {
        char *filter_point=original_chars;
        int filter_loop=0;
        while ((*filter_point) && (current_char!=*filter_point)) {
            filter_loop++;
            filter_point++;
        }
        avail_chars--;
        if (*filter_point) { /* this one must be filtered */
            *(current_write++)=*(asciionly_chars+(filter_loop*2));
            *(current_write++)=*(asciionly_chars+(filter_loop*2)+1);
            avail_chars--;
        } else {
            *(current_write++)=*current_read;
        }
        current_read++;
    }
    *current_write='\0';
    return (output_buffer);
}

/* in this function it's ok if given_string==output_buffer */
const char *filter_from_ascii(const char *given_string, char *output_buffer, int maxchars)
{
    const char *current_read=given_string;
    char *current_write=output_buffer;
    char current_char;
    int avail_chars=maxchars-1; /* safety measure */

    while ((current_char=*current_read) && avail_chars) {
        char next_char=*(current_read+1);
        char *filter_point=asciionly_chars;
        int filter_loop=0;
        while ((*filter_point) && \
               !((current_char==*filter_point) \
                 && (next_char==*(filter_point+1)))) {
            filter_loop++;
            filter_point+=2;
        }
        avail_chars--;
        if (*filter_point) { /* this one must be filtered */
            current_read++;
            *(current_write++)=*(original_chars+filter_loop);
        } else {
            *(current_write++)=*current_read;
        }
        current_read++;
    }
    *current_write='\0';
    return (output_buffer);
}

#ifdef OS_AMIGAOS
/* in this function it's ok if given_string==output_buffer */
/* if amigapl text provided, converts to iso. if iso, converts to amigapl */
const char *filter_fromto_amigapl_iso(const char *given_string, char *output_buffer)
{
    const char *read_from=given_string;
    char *write_to=output_buffer;
    char current_char;

    while(current_char=*read_from++){
        int table_counter=0;
        char table_char;
        int znalazlem=0;

        while((table_char=*(original_chars+table_counter))&&!znalazlem){
            if(current_char==table_char){
                current_char=*(amigapl_chars+table_counter);
                znalazlem=1;
            }
            table_counter++;
        }
        table_counter=0;
        while((table_char=*(amigapl_chars+table_counter))&&!znalazlem){
            if(current_char==table_char){
                current_char=*(original_chars+table_counter);
                znalazlem=1;
            }
            table_counter++;
        }
        *write_to++=current_char;
    }
    *write_to='\0';
    return(output_buffer);
}
#endif

#ifdef MSB_FIRST_PROCESSOR
long lsb2msb(long *lsb_long)
{
	*lsb_long=(*lsb_long>>24 & 0x000000ff) | \
				(*lsb_long>>8 & 0x0000ff00) | \
				(*lsb_long<<8 & 0x00ff0000) | \
				(*lsb_long<<24 & 0xff000000);
	return(*lsb_long);
}

short short_lsb2msb(short *lsb_short)
{
	*lsb_short=(*lsb_short>>8 & 0x00ff) |
				(*lsb_short<<8 & 0xff00);
	return(*lsb_short);
}
#endif
