c++ ncurses 질문이요.

jamesc1236의 이미지

지금 ncurses 로 기본적인 문서 편집기 (text editor)를 만들고 잇는데요 도저히 방법을 몰라서 여쭤봅니다.
일단 void input(int row, int col)에서 엔터랑 백스페이스 메소드를 만들었는데 이게 엔터나 백스페이스를 누르면 window 오른쪽 선이 같이 움직이는데 왜그러는건가여?
그리고 void open2(char * name) 으로 텍스트파일을 열고 수정할수 있게 만들려고 하는데 도저히 갈피를 못잡겠습니다.
int main(int argc, char*argv[])를 쓰면 어떻게 하는지 알고있는데 void open2(char *name)으로 할려고 하니 어떻게 시작해야할지도 모르겟습니다.
일단 새로운 window 창을 만들어서 유저에게 파일이름을 입력하시오 물어보고 입력하는 파일이름으로 getch()를 이용해서 열라고 하는게 맞는거 같은데...
좀 도와주시면 감사하겠습니다.

#include <ncurses.h>
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <curses.h>
#include <menu.h>
#include <cstring>
#include <fcntl.h>
#include <cerrno>
#include <unistd.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
using std::fstream;
using std::string; 
#define ENTER 10
 
 
const char *choices[] = {
  "Open",
  "Save",
  "Save As",
  "Exit",
};
WINDOW *create_newwin(int height, int width, int starty, int startx);
void print_in_middle(WINDOW *win, int starty, int startx, int width, const char *string, chtype color);
void opener(const char *name);
void open2(char * name);
void save(char * name);
void saveAs(char * name);
void exitProgram(char * name);
void menu();
void initFormat(int &cRow, int &cCol, const char* argv[]);
void userInput(int rows, int cols);
 
fstream r, r1;
 
WINDOW *my_menu_win;
int startx, starty, width, height;
int ch;
int enterStart = -1;
WINDOW * innerWindow;
int main(const int argc, const char * argv[])
{
  int row = 0; 
  int col = 0;
  int numLines = 0;
  cbreak();
  initFormat(row, col, argv);
  const char * filename = argv[1];
   opener(argv[1]);  
   refresh();
   userInput(3, 3);
 
 
//  getch();
  endwin();
 
  // menu();
}
 
 
void initFormat(int &cRow, int &cCol, const char* argv[]) {
 
  char topLeftMenu[] = "F1: Menu";
  char topMidMenu[] = "CSCI 1730 Editor!";
  int row, col;
 
  initscr();
  keypad(stdscr, TRUE);
  scrollok(stdscr, TRUE);
  noecho();
  getmaxyx(stdscr, row, col);
 
  innerWindow = subwin(stdscr, row-2,col-2,1,1);
 
  keypad(innerWindow, TRUE);
  scrollok(innerWindow, TRUE);
 
  int innerRow, innerCol;
  getmaxyx(innerWindow, innerRow, innerCol);
 
  cRow = row - innerRow;
  cCol = col - innerCol +1;
 
  mvprintw(0,(col-17)/2,"%s",topMidMenu);
  mvprintw(0,0,"%s",topLeftMenu);
  mvprintw(row-1,0,"%s","No File Yet Open--change later");//mvprintw(row-1,0,"%s",arg[1]);                                                                                           
  //mvaddch(10,10,'h');                                                                                                                                                              
  move(cRow,cCol);
  box(innerWindow,0,0);
  wrefresh(innerWindow);
  innerWindow = subwin(stdscr, row-4,col-6,2,3);
  move(3, 3);
  //  WINDOW *newpad(row-3, col-3);                                                                                                                                                  
}
 
 
void userInput(int row, int col)
{
  int  x = row;
  int  y = col;
  bool takeIn = true;
  while(takeIn){
    int ch = getch();
    if(ch < 32 || ch > 126)
      {
	switch(ch)
	  {
	  case KEY_UP:
	    {
	      if(y > 3)
		{
		  --y;
		  move(y, x);
		}
	      break;
	    }
	  case KEY_DOWN:
	    {
	      if(y < (LINES -3))
		{
		  ++y;
		  move(y, x);
		}
	      break;
	    }
	  case KEY_LEFT:
	    {
	      if(x > 2)
		{
		  --x;
		  move(y, x);
		}
	      break;
	    }
	  case KEY_RIGHT:
	    {
	      if(x < (COLS - 4))
		{
		  ++x;
		  move(y, x);
		}
	      break;
	    }
 
	  case KEY_F(1):
	    {
	      menu();
	      break;
	    }
 
	  case KEY_F(2):
	    {
	      takeIn = false;
	      break;
	    }
	case ENTER:
	    {		
		printf("\n");
		x = 4;
		move (y, x);
 	    }
	  case KEY_BACKSPACE: /*delete*/
	    {
	      printw("\b");
	      delch();
	    }
 
	  }//switch
      }//if
    else
      {
	++x;
       	if(x == (COLS - 3))
	  {
 
	    mvaddch(y +1, 3, ch);
	    ++y;
	    x = 3;
	  }
     	else
	  {
	    addch(ch);
	  }
      }//else
  }//while
}//user input
 
 
void menu(){
 
  initscr();
  start_color();
  cbreak();
  noecho();
  keypad(stdscr, TRUE);
  init_pair(1, COLOR_RED, COLOR_BLACK);
 
  height = LINES/4;
  width = COLS/4;
  starty = (LINES - height) / 2;/* Calculating for a center placement */
  startx = (COLS - width) / 2;/* of the window*/
 
  ITEM **my_items;
  int c;
  MENU *my_menu;
  int n_choices, i;
  ITEM *cur_item;
 
 
  n_choices = ARRAY_SIZE(choices);
  my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
  for(i = 0; i < n_choices; ++i)
    {
      my_items[i] = new_item(choices[i], NULL);
      if(i == 0)
	{
      	  set_item_userptr(my_items[0], (void *)open2);
	}
      if(i == 1)
	{
	  set_item_userptr(my_items[1], (void *)save);
	}
      if(i == 2)
	{
	  set_item_userptr(my_items[2], (void *)saveAs);
	}
      if(i == 3)
	{
	  set_item_userptr(my_items[3], (void *)exitProgram);
	}
    }
  my_items[n_choices] = (ITEM *)NULL;
 
  my_menu = new_menu((ITEM **)my_items);
 
  my_menu_win = newwin(10, 40, 4, 4); 
  keypad(my_menu_win, TRUE);
 
  set_menu_win(my_menu, my_menu_win);
  set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
 
 
  box(my_menu_win, 0, 0);
  print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
  mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
  mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
  mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
  mvprintw(LINES - 2, 0, "F1 to exit");
  refresh();
 
  post_menu(my_menu);
  wrefresh(my_menu_win);
 
  while((c = wgetch(my_menu_win)) != 10)
    {   
      switch(c)
	{
	case KEY_DOWN:
	  menu_driver(my_menu, REQ_DOWN_ITEM);
	  break;
	case KEY_UP:
	  menu_driver(my_menu, REQ_UP_ITEM);
	  break;
	case 10:
	  //  menu_driver(my_menu, REQ_TOGGLE_ITEM);
 
	  ITEM * cur;
	  void (*p)(char *);
 
	  cur = current_item(my_menu);
	  p = reinterpret_cast<void (*)(char*)>(item_userptr(cur));
	  p((char *)item_name(cur));
	  pos_menu_cursor(my_menu);
	  /*  move(20, 0);
	        clrtoeol();
		  mvprintw(20, 0, "Item selected is : % s", 
		         item_name(current_item(my_menu)));
			 pos_menu_cursor(my_menu);*/
 
	  break;
	}
    }
 
  unpost_menu(my_menu);
  free_menu(my_menu);
  for(i = 0; i < n_choices; ++i)
    free_item(my_items[i]);
  endwin();
 
 
}//main                                                                                                                                                                                                            
WINDOW *create_newwin(int height, int width, int starty, int startx)
{WINDOW *local_win;
 
  local_win = newwin(height, width, starty, startx);
  box(local_win, 0 , 0);/* 0, 0 gives default characters                                                                                                                                                           
                         * for the vertical and horizontal                                                                                                                                                         
                         * lines*/
  wrefresh(local_win);/* Show that box */
 
  return local_win;
}
 
 
void print_in_middle(WINDOW *win, int starty, int startx, int width, const char *string, chtype color)
{
 
  int length, x, y;
  float temp;
 
  if(win == NULL)
    win = stdscr;
  getyx(win, y, x);
  if(startx != 0)
    x = startx;
  if(starty != 0)
    y = starty;
  if(width == 0)
    width = 80;
 
  length = strlen(string);
  temp = (width - length)/ 2;
  x = startx + (int)temp;
  wattron(win, color);
  mvwprintw(win, y, x, "%s", string);
  wattroff(win, color);
  refresh();
 
}//print middle
 
 
void opener(const char * name)
{
 
  r.open(name);
  r1.open(name);
  int numlines = 0;
  int height, width;
  getmaxyx(innerWindow, height, width);
  string line; 
  while(getline(r, line))
    {
      numlines++;
    }
  string * storage = nullptr;
  storage = new string[numlines];
 
  for(int j = 0; j < numlines; j++)
    {
      getline(r1, line);
      storage[j] = line;
    }
 
  for(int i = 0; i < numlines; i++)
    {
      wprintw(innerWindow, storage[i].c_str());
      wprintw(innerWindow, "\n");
    }
  refresh();
 
  int start = 0;
 
 
  int KEY = wgetch(innerWindow);
  int breakout=0;
  while(breakout==0){
    switch(KEY){
    case KEY_UP: 
      if(start >= 0)
	{start--;
	  werase(innerWindow);
	  for(int i = start; i < numlines; i++){
	    wprintw(innerWindow, storage[i].c_str());
	    wprintw(innerWindow, "\n");
	  }
 
	  refresh();
	}
      KEY = wgetch(innerWindow);
      break;
 
    case KEY_DOWN:
      if(start < numlines){
	werase(innerWindow);
	start++;
	for(int i = start; i < numlines; i++){
	  wprintw(innerWindow, storage[i].c_str());
	  wprintw(innerWindow, "\n");
	}
 
	refresh();
 
      }
      KEY = wgetch(innerWindow);
      break;
 
    default:
 
      breakout=1;
      break;
    }
  }
/*
  string entered;
  const int BUFFSIZE = 1024;
  char buffer [BUFFSIZE];
  int n = 0;
 
  const char * filename = name;
  int fd = open(filename, O_RDWR);
//  NEED TO ADD OPEN ERROR
  if(fd != 1)
    {
 
      while ((n = read(fd, buffer, BUFFSIZE)) > 0)
	{
	  if(write(STDOUT_FILENO, buffer, n) != n)
	    {
	      perror("write error");
	    }
	}
      close(fd);
    }
  refresh();*/
}
void open2(char * name)
{
WINDOW *fileOpen;
int key;
fileOpen = newwin (3, 40, 5, 10);
wmove(fileOpen, 1, 2);
wprintw(fileOpen, "Enter file name");
 
}
void save(char * name)
{
  create_newwin(10, 10, 20, 20);
}
 
void saveAs(char * name)
{
  create_newwin(20, 20, 15, 15);
}
 
void exitProgram(char * name)
{
  endwin();
}
<

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.