Exemplo n.º 1
0
struct Connection *DatabaseOpen (const char *filename, char mode){
  struct Connection *conn = malloc(sizeof(struct Connection));
  if(!conn) die("Memory Error");

  conn->db = malloc(sizeof(struct Database));
  if (!conn->db) die("Memory error");

  if(mode == 'c') {
      conn->file = fopen(filename, "w");
  } else {
    conn->file = fopen(filename, "r+");

    if (conn->file){
      DatabaseLoad(conn);
    }
  }

  if(!conn->file) die("Failed to open the file");
  return conn;
}; 
Exemplo n.º 2
0
Arquivo: ex17.c Projeto: Dekuben/cEx
struct Connection* DatabaseOpen(const char* filename,
char mode)
{
	struct Connection *conn = malloc(sizeof(struct Connection));
	if (!conn) Die("Memory Error!", conn);

	conn->db = malloc(sizeof(struct Database));
	if(!conn->db) Die("Memory Error!", conn);

	if(mode == 'c')
	{
		conn->file = fopen(filename, "w");
	}
	else
	{
		conn->file = fopen(filename, "r+");
		if(conn->file)	DatabaseLoad(conn);
	}

	if(!conn->file) Die("Failed to open the file!", conn);

	return conn;
}//end of function databaseopen
Exemplo n.º 3
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;
}