Beispiel #1
0
void NcursesConsole(Connection *conn, const char *file)
{
	char console_buf[MAX_DATA];
	char *arg1, *arg2, *arg3, *arg4; 

	int *free_index = &(conn->core->cnf->free_index);
	int *delete_index = &(conn->core->cnf->delete_index);
	int *size = &(conn->core->cnf->size);

	wmove(console, 1, 1);
	touchwin(console);
	wrefresh(console);
	napms(100);
	wbkgd(border_console, COLOR_PAIR(5));
	touchwin(border_console);
	wrefresh(border_console);
	touchwin(console);
	wrefresh(console);
	echo();

	do {
		DisplayMode("Mode: Console");
		wmove(body, 0, 0);
		werase(body);
		DatabaseList(conn, body);
		PREFRESH;

		wgetnstr(console, console_buf, MAX_DATA);
		arg1 = strtok(console_buf, " ");
		arg2 = strtok(NULL, " ");
		arg3 = strtok(NULL, " ");
		arg4 = strtok(NULL, " ");

		ClearLine(console);

		if (arg1 != NULL) {

			if (strncmp(arg1, "add", 1) == 0) {
				if (arg4 == NULL) {
					if ((arg2 == NULL) || (arg3 == NULL)) {
						DisplayError("name or phone is empty\n");
					} else
						AddRecord(conn, NULL, arg2, arg3);
				} else {
					int i = atoi(arg2);
					if (i > *size || i < 0) {
						DisplayError("index out of bounds\n");
					} else {
						if ((arg3 == NULL) || (arg4 == NULL)) {
							DisplayError("name or phone is empty\n");
						} else {
							AddRecord(conn, &i, arg3, arg4);
						}
					}
				}
				DatabaseWrite(conn, file);
			}
			if (strncmp(arg1, "Insert", 1) == 0) {
				if (arg4 == NULL) {
					AddInsert(conn, NULL, arg2, arg3);
				} else {
					int i = atoi(arg2);
					if (i > *size || i < 0) {
						DisplayError("index out of bounds\n");
					} else {
						AddInsert(conn, &i, arg3, arg4);
					}
				}
				DatabaseWrite(conn, file);
			}
			if (strncmp(arg1, "delete", 1) == 0) {
				if (*free_index == 0 && *delete_index == 0) {
					DisplayError("database is already empty\n");
				} else if (arg2 == NULL) {
					DeleteRecord(conn, NULL);
				} else {
					int i = atoi(arg2);
					DeleteRecord(conn, &i);
				}
				DatabaseWrite(conn, file);
			}
			if (strncmp(arg1, "Delete", 1) == 0) {
				if (*free_index == 0 && *delete_index == 0) {
					DisplayError("database is already empty\n");
				} else if (arg2 == NULL) {
					DeleteInsert(conn, NULL);
				} else {
					int i = atoi(arg2);
					DeleteInsert(conn, &i);
				}
				DatabaseWrite(conn, file);
			}
			if (strncmp(arg1, "resize", 1) == 0) {
				int i = atoi(arg2);
				if (i == *size) {
					DisplayError("database size is already that size\n");
				} else if (i <= *free_index || i <= (*delete_index) + 1) {
					DisplayError("cannot resize below valid records\n");
				} else {
					DatabaseResize(conn, &i);
					DatabaseWrite(conn, file);
				}
			}
			if (strncmp(arg1, "Arrange", 1) == 0) {
				DatabaseArrange(conn);
				DatabaseWrite(conn, file);
			}
			if (strncmp(arg1, "sort", 1) == 0) {
				DatabaseSort(conn);
				DatabaseWrite(conn, file);
			}
			if (strncmp(arg1, "find", 1) == 0) {
				wmove(body, 0, 0);
				werase(body);
				DatabaseFind(conn, arg2, body);
				PREFRESH;
				getch();
			}
		} else
			break;
	} while (strncmp(arg1, "quit", 1) != 0);

	wbkgd(border_console, COLOR_PAIR(2));
	wrefresh(border_console);
	touchwin(console);
	wrefresh(console);
	noecho();
}
Beispiel #2
0
void NcursesOtherdb(Connection *conn, const char *file)
{
	Connection *olddb, *newdb, *createdb;
	int y = 0, down = 0, selection = 0;
	int dbsize = 10;
	int signature, input;
	char action;
	char create_buf[MAX_DATA];
	char *arg1;
	FILE *checksig;

	olddb = conn;

	getyx(body, gety, getx);

	do {
		DisplayMode("Mode: Otherdb");
		/* debug
		   getyx(body, gety, getx);

		   mvprintw(maxy - 10, maxx - 30, 
		   "\ny = %d\nx = %d\ndown = %d\n", y, x, down);
		   mvprintw(maxy - 7, maxx - 30, 
		   "\ngety = %d\ngetx = %d\n", gety, getx);
		   mvprintw(maxy - 10, maxx - 30, 
		   "\ny = %d\nx = %d\ndown = %d\n", y, x, down);
		   refresh();
		 */

		prefresh(body, down, 0, 6, 2, (maxy  / 2) + 8, maxx - 3);
		input = getchar();

		switch(input) {

			case 'c':
				touchwin(create);
				wrefresh(create);
				echo();
				wmove(create, 1, 1);
				wrefresh(create);
				wgetnstr(create, create_buf, MAX_DATA);
				arg1 = strtok(create_buf, " ");

				action = 'c';

				createdb = DatabaseLoad(arg1, &action);
				DatabaseCreate(createdb, &dbsize);
				DatabaseWrite(createdb, arg1);
				DatabaseClose(createdb);

				conn = olddb;
				DisplayError("Database has been successfully created");

				noecho();
				ClearLine(create);
				ReloadListing();
				break;

			case 'e':

				checksig = fopen(lsbuf[selection], "r+");
				if (checksig == NULL) {
					DisplayError("ERROR 622: could not open file");
					break;
				}

				// check signature
				fread(&signature, sizeof(int), 1, checksig);
				if (signature != 53281) {
					DisplayError("ERROR 127: db file not weno");
					break;
				}

				DatabaseClose(conn);
				action = 'C';
				newdb = DatabaseLoad(lsbuf[selection], &action);
				DisplayError("Database has been successfully loaded, exit this mode");
				ReloadListing();
				break;

				//case KEY_DOWN:
			case 'j':
				if (y != (maxy / 2) + 3) y++;
				wmove(body, y, 0);
				selection++;
				ReloadListing();
				if (y == (maxy / 2) + 3) down++;
				if (selection > (lscount) - 1) {
					selection = 0;
					y = 0;
					down = 0;
				}
				OtherdbSelection(&selection);
				break;

				//case KEY_UP:
			case 'k':
				if (y != 0) y--;
				selection--;
				ReloadListing();
				if ((y == 0) && (down != 0)) down--;
				if (selection < 0) {
					selection = (lscount) - 1;
					y = (lscount + 1) / 2;
					down = (lscount) / 2;
				}
				OtherdbSelection(&selection);
				break;

			default:
				break;
		}
	} while (input != 'q');
	RefreshdbList(conn);
	touchwin(body);
	wrefresh(body);
	PREFRESH;
}
Beispiel #3
0
void DatabaseCreate(struct Connection *conn){
  for (int i=0; i < MAX_ROWS; i++){
    struct Address addr = {.id = i, .set = 0};
    conn->db->rows[i] = addr;
  }
}

void DatabaseSet(struct Connection *conn, int id, const char *name, const char *email){
  struct Address *addr = &conn->db->rows[id];
  if(addr->set) die("Allready set, delete it first");

  addr->set = 1;
  char *res = strncpy(addr->name, name, MAX_DATA);
  if (!res) die("Name copy fialed");

  res = strncpy(addr->email, email, MAX_DATA);
  if (!res) die("Email copy failed");
}

void DatabaseGet(struct Connection *conn, int id){
  struct Address *addr = &conn->db->rows[id];

  if(addr->set){
    AddressPrint(addr);
  } else {
    die("ID is not set");
  }
}

void DatabaseDelete(struct Connection *conn, int id){
  struct Address addr = {.id = id, .set = 0};
  conn->db->rows[id] = addr;
}

void DatabaseList(struct Connection *conn){
  struct Database *db = conn->db;

  for (int i=0; i<MAX_ROWS; i++){
    struct Address *cur = &db->rows[i];

    if(cur->set){
      AddressPrint(cur);
    }
  }
}

int main(int argc, char *argv[]){
  if(argc < 3) die("USAGE: ext17 <dbfile> <action> [action params]");

  char *filename = argv[2];
  char action = argv[1][0];
  struct Connection *conn = DatabaseOpen(filename, action);
  int id = 0;

  if(argc > 3) id = atoi(argv[3]);
  if(id >= MAX_ROWS) die("There is not that many records.");

  switch(action){
    case 'c':
      DatabaseCreate(conn);
      DatabaseWrite(conn);
      break;
    case 'g':
      if (argc != 4) die("Need an id to get");
      DatabaseGet(conn, id);
      break;
    case 's':
      if(argc != 6) die("Need id, name, email to set.");
      DatabaseSet(conn, id, argv[4], argv[5]);
      DatabaseWrite(conn);
      break;
    case 'd':
      if(argc != 4) die("Need id to delete");
      DatabaseDelete(conn, id);
      DatabaseWrite(conn);
      break;
    case 'l':
      DatabaseList(conn);
      break;
    default: 
      die("Invalid action:\nc=create\ng=get\ns=set\nd=del\nl=list");
  }

  DatabaseClose(conn);

  return 0;
}
Beispiel #4
0
void NcursesExamine(Connection *conn, const char *file)
{
	int y = 0, down = 0, selection = 0; 
	int input;
	char examine_buf[MAX_DATA];
	char *arg1, *arg2;

	struct Information *rows = conn->core->db->rows;

	int *free_index = &(conn->core->cnf->free_index);
	int *delete_index = &(conn->core->cnf->delete_index);
	int *size = &(conn->core->cnf->size);

	mvwprintw(body, 0, 0, "%-2d %-10s %-10s", rows[0].index,
			rows[0].name, rows[0].phone);

	do {
		DisplayMode("Mode: Examine");
		/* debug
		   getyx(body, gety, getx);

		   mvprintw(maxy - 10, maxx - 30, 
		   "\ny = %d\nx = %d\ndown = %d\n", y, x, down);
		   mvprintw(maxy - 7, maxx - 30, 
		   "\ngety = %d\ngetx = %d\n", gety, getx);
		   mvprintw(maxy - 10, maxx - 30, 
		   "\ny = %d\nx = %d\ndown = %d\n", y, x, down);
		   refresh();
		 */

		PREFRESH_SCROLL;

		input = getchar();

		switch(input) {

			case 'a':
				touchwin(add);
				wrefresh(add);
				echo();
				wmove(add, 1, 1);
				wrefresh(add);
				wgetnstr(add, examine_buf, MAX_DATA);
				arg1 = strtok(examine_buf, " ");
				arg2 = strtok(NULL, " ");
				if ((arg1 == NULL) || (arg2 == NULL)) {
					DisplayError("name or phone is empty\n");
				} else {
					AddRecord(conn, &selection, arg1, arg2);
					DatabaseWrite(conn, file);
				}
				noecho();
				ClearLine(add);
				RefreshdbList(conn);
				break;

			case 'I':
				touchwin(add);
				wrefresh(add);
				echo();
				wmove(add, 1, 1);
				wrefresh(add);
				wgetnstr(add, examine_buf, MAX_DATA);
				arg1 = strtok(examine_buf, " ");
				arg2 = strtok(NULL, " ");
				if (arg1) {
					AddInsert(conn, &selection, arg1, arg2);
					DatabaseWrite(conn, file);
				}
				noecho();
				ClearLine(add);
				RefreshdbList(conn);
				break;

			case 'd':
				DeleteRecord(conn, &selection);
				DatabaseWrite(conn, file);
				RefreshdbList(conn);
				break;

			case 'D':
				DeleteInsert(conn, &selection);
				DatabaseWrite(conn, file);
				RefreshdbList(conn);
				break;

			case 'f':
				touchwin(find);
				wrefresh(find);
				echo();
				wmove(find, 1, 1);
				wrefresh(find);
				wgetnstr(find, examine_buf, MAX_DATA);
				arg1 = strtok(examine_buf, " ");
				if (arg1) {
					wmove(body, 0, 0);
					werase(body);
					DatabaseFind(conn, arg1, body);
					PREFRESH_SCROLL;
					getch();
				}
				noecho();
				ClearLine(find);
				RefreshdbList(conn);
				break;


				//case KEY_DOWN:
			case 'j':
				if (y != (maxy / 2) + 3) y++;
				wmove(body, y, 0);
				selection++;
				RefreshdbList(conn);
				if (y == (maxy / 2) + 3) down++;
				if (selection > (*size) - 1) {
					selection = 0;
					y = 0;
					down = 0;
				}
				ExamineSelection(&selection, conn);
				break;

				//case KEY_UP:
			case 'k':
				if (y != 0) y--;
				selection--;
				RefreshdbList(conn);
				if ((y == 0) && (down != 0)) down--;
				if (selection < 0) {
					selection = (*size) - 1;
					y = (*size) / 2;
					down = (*size) / 2;
				}
				ExamineSelection(&selection, conn);
				break;

			case 'r':
				touchwin(resize);
				wrefresh(resize);
				echo();
				wmove(resize, 1, 1);
				wrefresh(resize);
				wgetnstr(resize, examine_buf, MAX_DATA);
				arg1 = strtok(examine_buf, " ");
				if (arg1) {
					int i = atoi(arg1);
					if (i == *size) {
						DisplayError("database size is already that size\n");
					} else if (i <= *free_index || i <= (*delete_index) + 1) {
						DisplayError("cannot resize below valid records\n");
					} else {
						DatabaseResize(conn, &i);
						DatabaseWrite(conn, file);
					}
				}
				noecho();
				ClearLine(resize);
				RefreshdbList(conn);
				break;

			case 'A':
				DatabaseArrange(conn);
				DatabaseWrite(conn, file);
				RefreshdbList(conn);
				break;

			case 's':
				DatabaseSort(conn);
				DatabaseWrite(conn, file);
				RefreshdbList(conn);
				break;

			default:
				break;
		}

	} while (input != 'q');

	RefreshdbList(conn);
	PREFRESH_SCROLL;
}
Beispiel #5
0
void DatabaseCreate(struct Connection* conn )
{
	int i = 0;

	for(i = 0; i < MAX_ROWS; i++)
	{
		//make a prototype to initialise it
		struct Address addr = {.id = i, .set =0};
		//then just assign it
		conn->db->rows[i] = addr;
	}
}

void DatabaseSet(struct Connection* conn, int id, const char* name, const char* email)
{
	struct Address* addr = &conn->db->rows[id];
	if(addr->set) Die("Already set, deleted it first!", conn);

	addr->set =1;
	//WARNING: bug, read the "How to break it" section and fix this
	char* res = strncpy(addr->name,name,MAX_DATA);
	//demonstrate the strncpy bug
	if(!res) Die("Name copy failed", conn);

	res = strncpy(addr->email,email,MAX_DATA);
	if(!res) Die("Email copy failed", conn);
}

void DatabaseGet(struct Connection* conn, int id)
{
	struct Address* addr = &conn->db->rows[id];

	if(addr->set)
	{
		AddressPrint(addr);
	}
	else
	{
		Die("Id is not set.", conn);
	}
}

void DatabaseDelete(struct Connection* conn, int id)
{
	struct Address addr = {.id = id, .set = 0};
	conn->db->rows[id] = addr;
}

void DatabaseList(struct Connection* conn)
{
	int i = 0;
	struct Database* db = conn->db;

	for(i=0;i<MAX_ROWS;i++)
	{
		struct Address* cur = &db->rows[i];
		if(cur->set) AddressPrint(cur);
	}
}

int main(int argc, char* argv[])
{
	if(argc<3) Die("USAGE: ex17 <dbfile> <action> [action params]", conn);

	char* filename = argv[1];
	char action = argv[2][0];
	struct Connection* conn = DatabaseOpen(filename, action);
	int id =0;

	if(argc>3) id = atoi(argv[3]);
	if(id>=MAX_ROWS) Die("There's not that many records.", conn);

	switch(action)
	{
		case 'c':
			DatabaseCreate(conn);
			DatabaseWrite(conn);
			break;

		case 'g':
			if(argc != 4) Die("Need an id to get", conn);

			DatabaseGet(conn, id);
			break;

		case 's':
			if(argc != 6) Die("Need id, name, email to set.", conn);

			DatabaseSet(conn, id, argv[4], argv[5]);
			DatabaseWrite(conn);
			break;

		case 'd':
			if(argc != 4) Die("Need id to delete", conn);

			DatabaseDelete(conn, id);
			DatabaseWrite(conn);
			break;

		case 'l':
			DatabaseList(conn);
			break;
		default:
			Die("Invalid action, only: c=create, g=get, s=set, d=del, l=list", conn);
	}//end switch action

	DatabaseClose(conn);

	return 0;
}//end main