#ifndef ASE_H
#define ASE_H ASE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <math.h>
#include <unistd.h>
#include <sys/stat.h>
#include <curses.h>

#define MAX_FILES 20000
#define ON 1
#define OFF 0
#define MAX_WINDOW 1

#define	iskanjich(c)	((c >= 0x81 && c <= 0x9f || c >= 0xe0 && c<= 0xfc) ? (ON) : (OFF))	

class AseFilerTool {
private:
	void _splitpath(char *path,char *dir,char *file);
	void cutfspace(char *s);
	void getdirfile(char *pathbuff);
public:
	char field[32][256];
	char username[256][16];
	char userid[256][7];
	char groupname[256][16];
	char groupid[256][7];
	char openbuff[1024][33];

	char name[256];
	char valu[256];
	char bookmarks[256][1024];
	int bookline;

	char dirbuff[256];
	char filebuff[256];
	char depath[256];

	int errno;
	int dir_num;

	int bookmarksave(void);
	void bookmark(void);
	void readowner(void);
	void readgroup(void);
	void passread(char *path,int n);
	void groupread(char *path,int n);
	void pathfiled(char *path,int n);
};

class AseLocalFiler : public AseFilerTool {
private:
	char selcp[256][512];// /bin/cp 用
	int cpflag[256]; 	// /bin/cp 用
	int regline[MAX_FILES];
	int intCPnum;

	char gwpmenu[256][78];
	int yyy;
	int jj;

public:
	int fmode;
	int botom;
	int dirflag;
	int dmode;	// 'v'と 'h' .の付いたファイルの表示非表示を切替える
	int file_num;
	int max_lines;

	char fsortbuff[MAX_FILES][512];
	char fsortbuff2[MAX_FILES][512];
	char fil[MAX_FILES];
	int fileindex[MAX_FILES];

	char homedirname[256];	// 環境変数 HOME に定義されたディレクトリ
	char startdirname[256];	//ＵＦを起動したディレクトリ
	char docbuff[128];		//ブックマークのディレクトリ
	char dirstack[256][512]; //チェンジディレクトリの記録を保存する
	int dirlocatey[256];
	int dirlocatey_save[256];
	int dirlocateyy[256];
	int dirlocateyy_save[256];
	char dirlocate[256][512];
	int pathisto;
	int pathcnt;

	struct ViewBox {
		WINDOW *vs;	//端末ウインドウ
		int col;	//Ｘ軸の長さ（ウインドウの横の幅）
		int line;	//Ｙ軸の長さ（ウインドウの縦の長さ）
		int x;		//表示桁位置
		int y;		//表示行位置
	} pt[20];

	char buff0[MAX_FILES][64];	// for dtree

	struct FileView {
		char buff1[512];// 
		char buff2[32];// 
		char buff3[64];// 
		char buff4[32];// 
		char buff5[32];// 
		char buff6[32];// 

		char dtree1[128];// 
		char dtree2[128];// 

		char buff10[256];// 
	} fs[MAX_FILES];


	AseLocalFiler() {};
	int filer(int start,int end);

	void all_cls(WINDOW *evs);
//	int popmsg(int x,int y,char *buff);
	int popmsg2(int x,int y,char *buff);
	int findex(char *s,char *t);
	int resize(void);
	int filemode(uid_t ut,mode_t at, char *filebuff);
	char *popmenu(int x,int y,char *buff);
	char *cmpuid(uid_t uid);
	char *cmpgid(uid_t gid);
	char *cutspace(char *s);
	char *cutchars(char *s,int c);
	char *Mid1(char *ss, int a, int b);
	char *Mid2(char *ss, int a, int b);
	char *Mid3(char *ss, int a, int b);
	char *Mid4(char *ss, int a, int b);
	char *left2(char *s,size_t n);
	char *right2(char *s, size_t n);
	void cls_all(int no);
	void vs_cls(int no,int col);
	void line_clear(int no,int x,int y,int sy,int end,int len);
	void datadisp(int no,int x,int y,int sy,int end,int start,int end2);
	void vs_print(char *p,WINDOW *vs,int x,int y,int N);
	void set_screen(void);
	void set_screen_line1(void);
	void set_screen_line2(void);
	void initbuff(void);
	void displaymenu1(WINDOW *evs,int flag);
	void displaymenu2(WINDOW *evs);
	void dialogmsg(int x,int y,char *buff);
	void dialogmsg2(int x,int y,char *buff,char *header);
	void usermsg(int x,int y,char *buff);
	void sys_errmsg(int x,int y,char *buff);
	void fsort(int filenum,int point);
	void dirprint_all(char *dname,int type);
	void dirprint(char *dname,int type);
//	void *input_window(int x,int y,char *buff,char *msg);
	void *input_window2(int x,int y,char *buff,char *buff2,char *msg);
	void rectang(void);
	void attrview(int at,char *attrbuff);
	void checkhisto(void);
	void Mid(char *ds, char *ss, int a, int b);
	void dirlocateclear(void);
	void dirlocateofset(char mode);

	void helpmsg(int x,int y);
	void helpmsg2(int x,int y);	//for ase
	void right3(char *ds, char *ss, int a);
	~AseLocalFiler() {};
};

class AseEditor : public AseLocalFiler {
private:
	struct cell {
		int no;
		int x;
		int y;
		int c;
		int len;
		int attr;
		int mode;
		int action;
		char s[256];
		char p[256];
		WINDOW * cell;
	} cp[256][80];

	char copybuff[256][80][256];

	char tmpbuff1[256];
	char tmpbuff2[256];

	struct ViewBox {
		WINDOW *vs;	//端末ウインドウ
		int col;	//Ｘ軸の長さ（ウインドウの横の幅）
		int line;	//Ｙ軸の長さ（ウインドウの縦の長さ）
		int x;		//表示桁位置
		int y;		//表示行位置
	} pt2[256][80];

	struct ecpg {
		int cell_length;
		char cell_fieldtype[12];
		char cell_title[138];
	} pgc[512];

public:
	AseEditor() {};
	int convlabel(WINDOW *vs,int xcol, int xrow, char *f);
	int convtextbox(WINDOW *vs,int xcol, int xrow, char *f);
	int convgrid(WINDOW *vs,int xcol, int xrow, char *f);
	int putecpg(WINDOW *vs,int xcol, int xrow, char *f);
	int putecpgINSERT(WINDOW *vs,int xcol, int xrow, char *f);
	int putecpgUPDATE(WINDOW *vs,int xcol, int xrow, char *f);
	int putecpgDELETE(WINDOW *vs,int xcol, int xrow, char *f);
	int putecpgInterFace(char *f);
	int putCreateSQL(WINDOW *vs,int xcol, int xrow, char *f, int nline);
	int rightcell(int xcol, int xrow);
	int freads(int xcol, int xrow, char *f);
	int fwrites(int xcol, int xrow, char *f);
	int ase(int xcol,int xrow,char *f,int command);
//	void ehelpmsg(int x,int y);
//	void jhelpmsg(int x,int y);
//	void asehelp(void );
	~AseEditor() {};

	int convlabel2(WINDOW *vs,int xcol, int xrow, char *f);
	int convtextbox2(WINDOW *vs,int xcol, int xrow, char *f);
	int convgrid2(WINDOW *vs,int xcol, int xrow, char *f);
};

#endif

//typedef struct stat Dir;
//static Dir dptr;
//static Dir *fptr = &dptr;

extern "C" int jmvwgetnstr(WINDOW *win, int y, int x, unsigned char *str, int n, int wmode, int cmode);

extern "C" char *vs_ieee_c(WINDOW *vs,int x,int y,char *p,int N);

