C & ncurses problem *warning - long source

  • Thread starter dimitris.papastamos
  • Start date
D

dimitris.papastamos

Hello everyone, I've been working on a simple editor myself and I seem
to be having some problems with ncurses or so. I have been debugging
this program quite a lot trying to detect where the bug is hiding. If
you try to compile it gcc -o rosy rosy.cpp -lncurses and run it with
an input file, the delete and backspace characters function great
however when you start pressing keys like, right left or so to
navigate through the file, it prints ']'. I've been watching the
variables changing too closely but nothing really happens so I suspect
it has maybe something to do with ncurses. At the end of the source
file there is a function called 'print' which sums up the buffer to be
printed on the screen, if you take a look at it before it is returned
you can see that it includes the normal characters and not ']' however
when it is printed the ']''s appear. The source is long and maybe I am
not doing right to paste it here but I will try. Thanks a lot and help
is highly appreciated

p.s sorry for the lack of commenting

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>

#define MAX_WIDTH 300
#define MAX_HEIGHT 3500
#define CTRL(key) ((key) & 0x1f)
#define TABS_DEF 5

class rosyStream {
private:
char buffer[MAX_WIDTH];
public:
bool allocated;
int previous, next;
int cursor;

rosyStream() {
createObj();
}

void createObj() {
strcpy(buffer, "");
allocated = false;
cursor = 0;
previous = -1;
next = -1;
}

int returnCurPos() {
return cursor;
}

bool doBackspace() {
if(cursor > 0) {
for(int i = cursor; i < MAX_WIDTH; i++) {
buffer[i - 1] = buffer;
if(buffer == '\0') break;
}
cursor--;
return true;
}
return false;
}

void doEnd() {
cursor = strlen(buffer);
}

bool doDelete() {
if(cursor < strlen(buffer)) {
for(int i = cursor; i < MAX_WIDTH; i++) {
buffer = buffer[i + 1];
if(buffer == '\0') break;
}
return true;
}
return false;
}

bool left() {
if(cursor > 0) {
cursor--;
return true;
}
else return false;
}

bool right() {
if(cursor < strlen(buffer)) {
cursor++;
return true;
}
else return false;
}

void setCursor(int position) {
cursor = position;
}

int retLength() {
return strlen(buffer);
}

void cat(char *line) {
strcat(buffer, line);
}

char *returnBuf() {
return buffer;
}

void insert(char c) {
int s;
if(cursor == strlen(buffer)) {
buffer[cursor] = c;
cursor++;
buffer[cursor] = '\0';
} else {
for(s = strlen(buffer) + 1; s >= cursor; s--)
buffer = buffer[s - 1];
buffer[cursor] = c;
cursor++;
s = strlen(buffer);
buffer = '\0';
}
}

char *subStr(int init, int w) {
char retBuf[MAX_WIDTH] = "";
strcpy(retBuf, "");
int i;
for(i = 0; i <= w; i++) {
retBuf = buffer[init + i];
if(buffer[init + i] == '\0') break;
}
retBuf = '\0';
return retBuf;
}

char getChar(int pos) {
return buffer[pos];
}

void clear(int index) {
buffer[index] = '\0';
}

void doHome() {
cursor = 0;
}
};

rosyStream table[MAX_HEIGHT];

typedef struct rosy {
FILE *source;
int BACKGROUND_COLOR;
int FOREGROUND_COLOR;
int TABS;
int WIDTH;
int HEIGHT;
char filename[128];
char bBuffer[MAX_WIDTH];
} ROSY_DATA;

ROSY_DATA *initialize_rosy();
void parseIntoBuffer(ROSY_DATA *);
void edit(ROSY_DATA *);
char *print(ROSY_DATA *,char *, int);

void setfontcolor(int color) {
printf("%c[3%dm", 27, color);
}

void setbgcolor(int color) {
printf("%c[4%dm]", 27, color);
}

void __quit() {
endwin();
exit(0);
}

int yy = 0;
int c = 0;
int TOP_LINE = 0;
int counter = 0;

int main(int argc, char **argv) {
if(argc != 2 && argc != 1) {
fprintf(stderr, "usage : %s source\n", argv[0]);
exit(1);
}

ROSY_DATA *data = initialize_rosy();

data->TABS = TABS_DEF;
data->HEIGHT = 24;
data->WIDTH = 80;
strcpy(data->filename, "");
if(argc == 2) {
data->source = fopen(argv[1], "r");
if(data->source == NULL) {
fprintf(stderr, "+ rosy[error] - cannot find the
specified file [ %s ]\n", argv[1]);
exit(1);
}
strncpy(data->filename, argv[1], strlen(argv[1]));
parseIntoBuffer(data);
}

edit(data);

return 0;
}

ROSY_DATA *initialize_rosy() {
return ((ROSY_DATA *)malloc(sizeof(ROSY_DATA)));
}

void parseIntoBuffer(ROSY_DATA *mydata) {
char tmp[MAX_WIDTH] = "";
char secTmp[MAX_WIDTH];
int newPos = 0;
int ptr = 0;
table[0].createObj();

while(!feof(mydata->source)) {
fgets(tmp, MAX_WIDTH, mydata->source);
table[ptr].createObj();
newPos = 0;

secTmp[0] = '\0';

for(int i = 0; i < strlen(tmp); i++) {
if(tmp == 10 || tmp == 13) {
secTmp[newPos++] = '\0';
} else if(tmp == 9) {
for(int j = 0; j < mydata->TABS; j++) {
secTmp[newPos++] = ' ';
}
} else {
secTmp[newPos++] = tmp;
}
}
secTmp[newPos] = '\0';
table[ptr].cat(secTmp);
if(ptr > 0) {
table[ptr].previous = ptr - 1;
table[ptr - 1].next = ptr;
}

table[ptr].allocated = true;
ptr++;
if(ptr == MAX_HEIGHT) {
printf("File too big [ max_width = %d], [max_height =
%d ]\n", MAX_WIDTH, MAX_HEIGHT);
getch();
}
}
fclose(mydata->source);
}

void edit(ROSY_DATA *data) {
int input;
int v = 0;

(void) initscr();
keypad(stdscr, TRUE);
(void) def_prog_mode();
(void) nonl();
(void) cbreak();
(void) noecho();
start_color();

data->BACKGROUND_COLOR = 0;
data->FOREGROUND_COLOR = 8;

setbgcolor(data->BACKGROUND_COLOR);
setfontcolor(data->FOREGROUND_COLOR);

data->WIDTH = COLS - 1;
data->HEIGHT = LINES - 2;

for(int i = 0; i < MAX_WIDTH; i++)
data->bBuffer = ' ';
data->bBuffer[data->WIDTH] = '\0';

print(data, "", 1);
refresh();

c = 0;

do {
input = getch();
if(input == 127 || input == KEY_BACKSPACE) {
if(!table[c].doBackspace() && table[c].previous > -1) {

table[table[c].previous].setCursor(table[table[c].previous].retLength());

table[table[c].previous].cat(table[c].returnBuf());
table[c].allocated = false;
table[table[c].previous].next = table[c].next;
if(table[c].next > -1)
table[table[c].next].previous =
table[c].previous;
c = table[c].previous;
}
}
else if(input == KEY_RESIZE || input == -1) {
data->bBuffer[data->WIDTH] = ' ';
data->WIDTH = COLS - 1;
data->bBuffer[data->WIDTH]= '\0';
data->HEIGHT = LINES - 2;
for(int i = 0; i < MAX_WIDTH; i++)
data->bBuffer = ' ';
data->bBuffer[data->WIDTH] = '\0';
}
else if(input == 330) {
if(!table[c].doDelete() && table[c].next > -1) {
table[c].cat(table[table[c].next].returnBuf());
table[table[c].next].allocated = false;
table[c].next = table[table[c].next].next;
table[table[c].next].previous = c;
}
}
else if(input == KEY_NPAGE) {
for(int i = 0; i < data->HEIGHT; i++) {
if(table[c].next == -1) break;
c = table[c].next;
}
}
else if(input == KEY_PPAGE) {
for(int i = 0; i < data->HEIGHT; i++) {
if(table[c].previous == -1) break;
c = table[c].previous;
}
}
else if(input == 13) {
bool flag = false;
int i, j;

for(i = 1; i < MAX_HEIGHT; i++)
if(table.allocated == false) {
flag = true;
break;
}
if(flag == true) {
table.createObj();
table.allocated = true;
table.next = table[c].next;
if(table.next > -1)
table[table.next].previous = i;
table.previous = c;
table[c].next = i;
table.setCursor(0);
for(j = table[c].returnCurPos(); j <
table[c].retLength(); j++)
table.insert(table[c].getChar(j));
table[c].clear(table[c].returnCurPos());
table.setCursor(0);
c = i;
}
}
else if(input == KEY_LEFT) {
if(!table[c].left()) {
if(table[c].previous > -1) {
c = table[c].previous;
table[c].setCursor(table[c].retLength());
}
}
}
else if(input == KEY_DOWN) {
if(table[c].next > -1) {
if(table[table[c].next].retLength() <
table[c].returnCurPos())

table[table[c].next].setCursor(table[table[c].next].retLength());
else

table[table[c].next].setCursor(table[c].returnCurPos());
c = table[c].next;
}
}
else if(input == KEY_UP) {
if(table[c].previous > -1) {
if(table[table[c].previous].retLength() <
table[c].returnCurPos())

table[table[c].previous].setCursor(table[table[c].previous].retLength());
else

table[table[c].previous].setCursor(table[c].returnCurPos());
c = table[c].previous;
}
}
else if(input == 9 || input == 11) {
for(int i = 0; i < TABS_DEF; i++)
table[c].insert(' ');
}
else if(input == KEY_RIGHT) {
if(!table[c].right()) {
if(table[c].next > -1) {
c = table[c].next;
table[c].setCursor(0);
}
}
}
else if(input == KEY_F(4) || (v == 2 && input == 'S'))
__quit();
else if(input == KEY_F(3) || (v == 2 && input == 'R')) {
if(strcmp(data->filename, "") == 0) {
strcpy(data->filename, print(data,"Filename: ",
2));
} else {
FILE *save;
save = fopen(data->filename, "w");
if(!save) {
char local[128];
snprintf(local, 128, "+ rosy[error] - I can't
save the file %s\n", data->filename);
print(data, local, 1);
}
else {
char buf[MAX_WIDTH];
int i = 0;
do {
strcpy(buf, table.returnBuf());
strcat(buf, "\n");
fputs(buf, save);
i = table.next;
} while(i > -1);
fclose(save);
print(data, "File saved.", 1);
}
}
}
else if(input == 27 || input == 33) {
v = 1;
}
else if(input < 30);
else {
if(v == 1 && (input == 'O' || input == '['))
v = 2;
else if(v == 2 && (input == 'F' || input == '8'))
{
if(input == '8')
v = 4;
else {
table[c].doEnd();
v = 0;
}
}
else if(v == 2 && (input == 'H' || input == '7')) {
if(input == '7') {
v = 3;
} else {
table[c].doHome();
v = 0;
}
}
else if(v == 4 && input == '~') {
table[c].doEnd();
v = 0;
}
else if(v = 3 && input == '~') {
table[c].doHome();
v = 0;
}
else if(v == 2 && input == 97) {
if(table[c].previous > -1) {
if(table[table[c].previous].retLength() <
table[c].returnCurPos())

table[table[c].previous].setCursor(table[table[c].previous].retLength());
else

table[table[c].previous].setCursor(table[c].returnCurPos());
c = table[c].previous;
}
v = 0;
}
else if(v == 2 && input == 99) {
if(!table[c].right()) {
if(table[c].next > -1) {
c = table[c].next;
table[c].setCursor(0);
}
}
v = 0;
}
else if(v == 2 && input == 98) {
if(table[c].next > -1) {
if(table[table[c].next].retLength() <
table[c].returnCurPos())

table[table[c].next].setCursor(table[table[c].next].retLength());
else

table[table[c].next].setCursor(table[c].returnCurPos());
c = table[c].next;
}
v = 0;
}
else if(v == 2 && input == 100) {
if(!table[c].left()) {
if(table[c].previous > -1) {
c = table[c].previous;

table[c].setCursor(table[c].retLength());
}
}
v = 0;
}
else {
v = 0;
table[c].insert(input);
}
}

if(table[c].returnCurPos() > yy + data->WIDTH - 1)
yy = table[c].returnCurPos() - data->WIDTH;
else if(table[c].returnCurPos() < yy) {
yy = table[c].returnCurPos();
}

print(data, "", 1);
refresh();
++counter;
} while(input != CTRL('q') && input != CTRL('Q'));
__quit();
}

char *print(ROSY_DATA *data, char *buf, int t) {
int a = -1, i, z;
char outBuffer[500][500];
char local[MAX_WIDTH];

for(int tmp = 0; tmp <= data->HEIGHT; tmp++)
strcpy(outBuffer[tmp], data->bBuffer);

local[0] = '\0';
z = 0;
i = TOP_LINE;
do {
z++;
if(i == c) a = z;
} while(i > -1 && z < data->HEIGHT);
if(a == -1) {
z = 0;
i = TOP_LINE;
while(table.next != -1 && i != c) {
i = table.next;
z++;
if(i == c) a = z;
if(z > data->HEIGHT) {
TOP_LINE = table[TOP_LINE].next;
}
}
if(a == -1) {
i = TOP_LINE;
while(table.previous != -1 && i != c) {
i = table.previous;
TOP_LINE = i;
}
}
}
a = -1, z = 0, i = TOP_LINE;
do {
strcpy(outBuffer[z], table.subStr(yy, data->WIDTH));
outBuffer[z][strlen(outBuffer[z])] = ' ';
outBuffer[z][data->WIDTH + 1] = '\0';
z++;
if(i == c) a = z;
i = table.next;
} while(i > -1 && z <= data->HEIGHT);

for(int tmp = 0; tmp <= data->HEIGHT; tmp++) {
move(tmp, 0);
printw("%s ", outBuffer[tmp]);
}

move(data->HEIGHT + 1, 0);
wattron(stdscr, A_REVERSE);
printw("%s ", data->bBuffer);
move(data->HEIGHT + 1, 0);
if(strlen(buf) > 0)
printw("Char code: %i | Column: %i | %s",
table[c].getChar(table[c].returnCurPos()),
table[c].returnCurPos() + 1, buf);
else
printw("Char code: %d | Column: %i | F3 - Save F4 -
Exit", table[c].getChar(table[c].returnCurPos()),
table[c].returnCurPos() + 1);
if(t == 2) {
refresh();
echo();
getstr(local);
noecho();
printw("%s", local);
getch();
}
setbgcolor(data->BACKGROUND_COLOR);
setfontcolor(data->FOREGROUND_COLOR);
wattroff(stdscr, A_REVERSE);

move(a - 1, table[c].returnCurPos() - yy);

if(!strcmp(buf, "") == 0 && t == 1)
getch();

return local;
}
 
K

Kenny McCormack

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>

#define MAX_WIDTH 300
#define MAX_HEIGHT 3500
#define CTRL(key) ((key) & 0x1f)
#define TABS_DEF 5

class rosyStream {
private:
char buffer[MAX_WIDTH];
public:
bool allocated;
int previous, next;
int cursor;

I take it, google/gmail doesn't support cross-posting...
 
D

dimitris.papastamos

sorry I mean g++ -o rosy rosy.cpp -lncurses not gcc. Such a bad habit
really :p
 
M

Mark McIntyre

On Wed, 06 Jun 2007 09:46:34 -0700, in comp.lang.c ,
Hello everyone, I've been working on a simple editor myself and I seem
to be having some problems with ncurses or so.

curses / ncurses isn't actually part of the C language. This also
looks more like an environmental issue, eg your terminal isn't
configured for the right keyboard escapes.

You will probably have more luck in comp.unix.programmer, where this
is probably more topical.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

dimitris.papastamos

Thanks for the reply, I will probably ask for help in
comp.unix.programmer, and by the way I truly like the quote you have
there :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top