From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/hed/edit.cc | 262 ++++++++++++++++++++++++++++++++ src/hed/edit.h | 61 ++++++++ src/hed/hed.cc | 56 +++++++ src/hed/input.cc | 107 +++++++++++++ src/hed/input.h | 52 +++++++ src/hed/tables.cc | 442 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hed/tables.h | 61 ++++++++ src/hed/test.cc | 33 ++++ 8 files changed, 1074 insertions(+) create mode 100644 src/hed/edit.cc create mode 100644 src/hed/edit.h create mode 100644 src/hed/hed.cc create mode 100644 src/hed/input.cc create mode 100644 src/hed/input.h create mode 100644 src/hed/tables.cc create mode 100644 src/hed/tables.h create mode 100644 src/hed/test.cc (limited to 'src/hed') diff --git a/src/hed/edit.cc b/src/hed/edit.cc new file mode 100644 index 0000000..b38b1e9 --- /dev/null +++ b/src/hed/edit.cc @@ -0,0 +1,262 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include + +const mstring end_tag="~"; + +Editor::Line::Line() +{ + line=(char*)malloc(MAXCOLS); + line[0]=0; + dim=1; +} +Editor::Line::Line(char* p,int s) +{ + line=(char*)malloc(MAXCOLS); + memcpy(line,p,s); + dim=s; +} +Editor::Line::~Line() +{ + delete line; +} +int Editor::Line::Insert(int pos,char c) +{ + if(pos>=dim) pos=dim-1; + if(dimdim<=MAXCOLS) { + memcpy(line+dim-1,p->line,p->dim); + dim+=p->dim-1; + return 1; + } + else return 0; +} +Editor::Line* Editor::Line::Break(int pos) +{ + if(pos>=dim) pos=dim-1; + Line* p=new Line(line+pos,dim-pos); + line[pos]=0; + dim=pos+1; + return p; +} +void Editor::Line::Delete(int pos) +{ + if(dim-1>pos) { + memmove(line+pos,line+pos+1,dim-pos-1); + dim--; + } +} + +///////////////////////////////////////////////////////////////////////// + +Editor::Editor(const mstring& s,int ex) : + path(s), + from(0),lines(Lines()),atline(0),size(0), + tab(0),cols(Columns()),atcol(0),exclusive(ex) +{ + memset(map,0,sizeof(Line*)*MAXLINES); + map[0]=new Line(); + size=1; + caption=path; + caption+=sref(" ok (ESC to go back)"); + if(!exclusive) caption+=sref(" (readonly)"); +} +Editor::~Editor() +{ + for(int i=0;idim) { + if(i==size-1) { + if(tabdim-1) + OutputField(i-from,0,cols,sref(map[i]->line+tab,map[i]->dim-tab-1)); + OutputField(i-from,map[i]->dim-tab-1,1,end_tag); + } + else { + OutputField(i-from,0,cols,sref(map[i]->line+tab,map[i]->dim-tab)); + } + } + } + Reverse(1); + OutputField(lines,0,cols,caption); + Reverse(0); + MoveCursor(atline-from,atcol-tab); + Refresh(); +} + +void Editor::Move(int i,int j) +{ + atline+=i; + atcol+=j; + if(atline<0) atline=0; else if(atline>=size) atline=size-1; + if(atline=from+lines) from=atline+1-lines; + if(atcol<0) atcol=0; else if(atcol>=map[atline]->dim) atcol=map[atline]->dim-1; + if(atcol=tab+cols) tab=atcol+1-cols; +} + +void Editor::Home() +{ + atline=from=atcol=tab=0; +} + +void Editor::Navigate() +{ + int c; + while(1) { + Show(); + switch(c=Input()) { + case KK_UP: + case 16: + Move(-1,0); + break; + case KK_DOWN: + case 14: + Move(1,0); + break; + case KK_LEFT: + case 2: + Move(0,-1); break; + case KK_RIGHT: + case 6: + Move(0,1); + break; + case KK_NPAGE: + Move(lines/2,0); + break; + case KK_PPAGE: + Move(-lines/2,0); + break; + case KK_HOME: + case 1: + Move(0,-map[atline]->dim); + break; + case KK_END: + case 5: + Move(0,map[atline]->dim); + break; + case 127: + case 4: + if(exclusive) Delete(); + break; + case 8: + if(exclusive) Backspace(); + break; + case 13: + if(exclusive) BreakLine(); + break; + case KK_F(1): + if(exclusive) Insert('å'); + break; + case KK_F(2): + if(exclusive) Insert('ä'); + break; + case KK_F(3): + if(exclusive) Insert('ö'); + break; + case 9: + if(exclusive) Insert('\t'); + break; + default: + if(exclusive) if(c>=32&&c<127) Insert(c); + break; + case 27: + return; + } + } +} + +void Editor::Insert(int c) +{ + if(map[atline]->Insert(atcol,c)) Move(0,1); +} + +void Editor::BreakLine() +{ + if(sizeBreak(atcol); + memmove(map+atline+2,map+atline+1,sizeof(Line*)*(size-atline-1)); + map[atline+1]=p; + size++; + Move(1,-atcol); + } +} + +void Editor::Delete() +{ + if(map[atline]->dim-1>atcol) { + map[atline]->Delete(atcol); + } + else if(size-1>atline) { + delete map[atline]; + memmove(map+atline,map+atline+1,sizeof(Line*)*(size-atline-1)); + size--; + } +} + +void Editor::Backspace() +{ + if(atcol>0) { + Move(0,-1); + map[atline]->Delete(atcol); + } + else if(atline>0) { + int pos=map[atline-1]->dim-1; + if(map[atline-1]->Append(map[atline])) { + delete map[atline]; + memmove(map+atline,map+atline+1,sizeof(Line*)*(size-atline-1)); + size--; + Move(-1,pos-atcol); + } + } +} + +void Editor::Paste(const mstring& s) +{ + for(int i=0;iline,map[i]->dim-1); + if(i + +struct Editor { + static const int MAXLINES=1024; + static const int MAXCOLS=256; + struct Line { + Line(); + Line(char* p,int s); + ~Line(); + int Insert(int pos,char c); + int Append(const Line* p); + Line* Break(int pos); + void Delete(int pos); + char* line; + int dim; + }; + + Editor(const mstring& path,int ex); + ~Editor(); + + void Copy(mstring& s); + void Paste(const mstring& s); + + void Show(); + void Home(); + void Move(int i,int j); + void Navigate(); + void Insert(int c); + void BreakLine(); + void Delete(); + void Backspace(); + + mstring path,caption; + Line* map[MAXLINES]; + int from,lines,atline,size; + int tab,cols,atcol,exclusive; +}; + +#endif diff --git a/src/hed/hed.cc b/src/hed/hed.cc new file mode 100644 index 0000000..dc469cc --- /dev/null +++ b/src/hed/hed.cc @@ -0,0 +1,56 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "tables.h" +#include "input.h" +#include + +static fp_stream merr(stderr); + +main(int argc,char* argv[]) +{ + if(argc<2) { + merr<<"Usage: "< [exclusive]\n"; + return -1; + } + char* path=argv[1]; + int excl=argc>2?atoi(argv[2]):0; + mstring err; + sleep(1); + InitCurses(); + Tables tab; + try { + tab.Open(path,excl); + tab.Setup(20); + tab.Navigate(); + } + catch(const merror_t& e) { + err=e.desc; + } + catch(...) { + err="hdb: Unexpected error"; + } + tab.Close(); + EndCurses(); + if(err.nempty()) { + merr< +#include +#include +#include +#include + +int Lines() { return LINES-1; } +int Columns() { return COLS-1; } + +void ClearScreen() { erase(); } +void MoveCursor(int i,int j) { move(i,j); } + +void InitCurses() +{ + initscr(); + nodelay(stdscr,1); + raw(); + noecho(); + nonl(); + keypad(stdscr,1); + intrflush(stdscr,FALSE); +} + +void EndCurses() +{ + endwin(); +} + +void OutputField(int row,int col,int field,const sref& in) +{ + mstring tmp=in.left_first_of(cset_newl); + int cut=tmp.size()!=in.size(); + if(tmp.size()field) { tmp.resize(field-1); tmp.append(1,'\\'); } + mvaddstr(row,col,(char*)tmp.c_str()); +} + +void Reverse(int n) +{ + if(n) attron(A_REVERSE); else attroff(A_REVERSE); +} +void Bold(int n) +{ + if(n) attron(A_BOLD); else attroff(A_BOLD); +} + +void Refresh() +{ + refresh(); +} + +int RawInput() { + int c=getch(); + if(c>0) return c; + struct pollfd ps={fileno(stdin),POLLIN,0}; + poll(&ps,1,1000); + return getch(); +} + +int Input() { + int c=RawInput(); +#if 0 + if(c==27) { + napms(1); + int c2=getch(); + if(c2!=-1) { + switch(c2) { + default: return 0; + case 91: + switch(RawInput()) { + default: return 0; + case '2': + RawInput(); + switch(RawInput()) { + default: return 0; + case 52: return RawInput()==122?KEY_HOME:0; + case 54: return RawInput()==122?KEY_PPAGE:0; + case 48: return RawInput()==122?KEY_END:0; + case 50: return RawInput()==122?KEY_NPAGE:0; + } + } + } + } + } +#endif + return c; +} + diff --git a/src/hed/input.h b/src/hed/input.h new file mode 100644 index 0000000..76071ce --- /dev/null +++ b/src/hed/input.h @@ -0,0 +1,52 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#ifndef INPUTH +#define INPUTH + +#define KK_DOWN 0402 +#define KK_UP 0403 +#define KK_LEFT 0404 +#define KK_RIGHT 0405 +#define KK_HOME 0406 +#define KK_F0 0410 +#define KK_F(n) (KK_F0+(n)) +#define KK_NPAGE 0522 +#define KK_PPAGE 0523 +#define KK_END 0550 + +int Input(); +int RawInput(); + +int Lines(); +int Columns(); + +void ClearScreen(); +void MoveCursor(int i,int j); +void OutputField(int row,int col,int field,const sref& p); +void Reverse(int n); +void Bold(int n); +void Refresh(); + +void InitCurses(); +void EndCurses(); + +#endif diff --git a/src/hed/tables.cc b/src/hed/tables.cc new file mode 100644 index 0000000..c489e6f --- /dev/null +++ b/src/hed/tables.cc @@ -0,0 +1,442 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "tables.h" +#include "input.h" +#include +#include +#include + +const mstring star_tag="*"; + +const int PAD=2; +const mstring HELP= +"Commands from record page:\n" +"\n" +"DEL - Delete record\n" +"RET - Edit record\n" +"n - New record\n" +"s - Sort by table\n" +"o - Order by table\n" +"c - Check for duplicates\n" +"f - Filename for map\n" +"r - Remove table\n" +"a - Add table\n"; + +Tables::Tables() : + from(0),lines(Lines()-1),atline(0),writable(0), + tab(0),space(10),cols(Columns()),atcol(0), + lastfrom(-1),lasttab(-1),lastline(0),lastcol(0) +{ + dispcols=cols/space; +} + +Tables::~Tables() +{ +} + +void Tables::Setup(int sp) +{ + space=sp; + dispcols=cols/space; + Home(); +} + +void Tables::Show() +{ + if(db.remap()) { + lastfrom=-1; + caption.convert("%d, %d, %d, %d", + db.head()->filesize.get(), + db.head()->fatal.get(), + db.head()->major.get(), + db.head()->minor.get()); + } + if(from!=lastfrom||tab!=lasttab) { + ClearScreen(); + Bold(1); + for(int j=tab;j=0&&i<=db.size()&&j>=0&&j<=db.Tables()) { + sref tmp; if(i=0&&i<=db.size()&&j>=0&&j<=db.Tables()) { + sref tmp; if(i=db.size()) atline=db.size(); + if(atline<0) atline=0; + if(atline>=from+lines) from=atline+1-lines; + if(atline=db.Tables()) atcol=db.Tables(); + if(atcol<0) atcol=0; + if(atcol>=tab+dispcols) tab=atcol+1-dispcols; + if(atcol=0) { + caption.convert("%s - added record",path.c_str()); + Move(row-atline,0); + } + else { + caption.convert("%s - record nonunique - not added",path.c_str()); + Move(0,0); + } + } + catch(const merror_t& e) { + caption=e.desc; + Move(0,0); + } + lastfrom=-1; +} + +void Tables::Delete() +{ + if(db.remap()) lastfrom=-1; + if(atline +#include + +struct Tables { + Tables(); + ~Tables(); + + void Open(const mstring& path,int ex=0); + void Save(); + void Close(); + + void Setup(int sp); + + void Show(); + void Home(); + void Move(int i,int j); + void Navigate(); + int Command(); + + void Sort(); + void Order(); + void Check(); + + void Edit(); + void Insert(); + void Delete(); + void FileName(); + void AddTable(); + void RemTable(); + void Help(); + + dbmap db; + mstring path,caption; + int from,lines,atline,writable; + int tab,space,cols,dispcols,dim,atcol; + int lastfrom,lasttab,lastline,lastcol; +}; + +#endif diff --git a/src/hed/test.cc b/src/hed/test.cc new file mode 100644 index 0000000..318334f --- /dev/null +++ b/src/hed/test.cc @@ -0,0 +1,33 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "input.h" +#include + +main(int argc,char* argv[]) +{ + InitCurses(); + while(1) { + int c=RawInput(); + fprintf(stderr,"%d\r\n",c); + if(c==27) break; + } + EndCurses(); + return 0; +} -- cgit v1.2.3