/*     Bed a menu-driven multi dataformat binary editor for Linux            */
/*     Copyright (C) 1998 Jaap Korthals Altes <jkaltes@hetnet.nl>       */
/*                                                                           */
/*     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., 675 Mass Ave, Cambridge, MA 02139, USA.             */
/* Fri Dec  8 22:14:27 2000                                                  */
#ifdef GLOBAL
#include "global.h"
BEDPLUGIN
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "screenpart.h"
#include "typelist.h"

#define FLOATDEFS \
	floatdata(Float,4,13,float,"%g","%013.7g")\
	floatdata(Double,8,23,double,"%lg","%023.16lg")\
	floatdata(LongDouble,12,36,long double,"%Lg","%036.28Lg")



	class Float: public General {
		char *tobin;
		char *frombin;
		public:
		declareparent 
		Float(Float &fl):General(fl) {
			tobin=fl.tobin;
			frombin=fl.frombin;
			}
		Float( Editor *ed,int x1=0, int y1=0): General( ed,13,10,4,x1, y1)
			{
//			name=Float::thename;
			chbytes(4);};
		int chbytes(int b) {
			switch(b) {
#undef floatdata
#define floatdata(Name,Bytes,Takes,Type,Tobin,Frombin) case Bytes: tobin=Tobin;frombin=Frombin;maxres=Takes;datbytes(Bytes);break ;
				FLOATDEFS
				case -1: return 1;
				default: return -1;
				}
			return datbytes();
			}
		ScreenPart *newone(void) {
			return new Float(*this);
			}
	int format2bin(char *formal, unsigned char *bin, int len) {
	       int start,end;
                for(start=0;start<len&&formal[start]&&isspace(formal[start]);start++)
                                ;
                for(end=start;end<len&&formal[end]&&!isspace(formal[end]);end++)
                                ;
                if(end==start)
                        return 0;
		sscanf(formal+start,tobin,bin);
		return end;
		}
		int bin2formal(unsigned char *bin, char *formal, int len) {
			switch(datbytes()) {
#undef floatdata
#define floatdata(Name,Bytes,Takes,Type,Tobin,Frombin)\
			case Bytes: {\
				if(len<Bytes) {\
					strncpy(formal,"Error",6);\
					return -1;\
					}\
				Type get =*((Type*)bin);\
				if(!(get<0)&&!(get>0)&&!(get==0)) {\
					memset(formal, ' ',Takes-3);\
					strncpy(formal+Takes-3,"nan",4);\
					return 0;\
					}\
				sprintf(	formal,frombin,get); };break;
				FLOATDEFS
				default: return -1;
				};
			return 0;
			}
		int isel(int ch) {return ((ch>='0'&&ch<='9')||(ch=='.')||(ch=='-')||(ch=='e')||(ch=='+')||(ch==' '));}
		};

parentd(ScreenPart,Float) 
datatype(Float)

#endif




