473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C & ncurses problem *warning - long source

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[i];
if(buffer[i] == '\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[i] = buffer[i + 1];
if(buffer[i] == '\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[s] = buffer[s - 1];
buffer[cursor] = c;
cursor++;
s = strlen(buffer);
buffer[s] = '\0';
}
}

char *subStr(int init, int w) {
char retBuf[MAX_WIDTH] = "";
strcpy(retBuf, "");
int i;
for(i = 0; i <= w; i++) {
retBuf[i] = buffer[init + i];
if(buffer[init + i] == '\0') break;
}
retBuf[i] = '\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[i] == 10 || tmp[i] == 13) {
secTmp[newPos++] = '\0';
} else if(tmp[i] == 9) {
for(int j = 0; j < mydata->TABS; j++) {
secTmp[newPos++] = ' ';
}
} else {
secTmp[newPos++] = tmp[i];
}
}
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[i] = ' ';
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[i] = ' ';
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[i].allocated == false) {
flag = true;
break;
}
if(flag == true) {
table[i].createObj();
table[i].allocated = true;
table[i].next = table[c].next;
if(table[i].next -1)
table[table[i].next].previous = i;
table[i].previous = c;
table[c].next = i;
table[i].setCursor(0);
for(j = table[c].returnCurPos(); j <
table[c].retLength(); j++)
table[i].insert(table[c].getChar(j));
table[c].clear(table[c].returnCurPos());
table[i].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[i].returnBuf());
strcat(buf, "\n");
fputs(buf, save);
i = table[i].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[i].next != -1 && i != c) {
i = table[i].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[i].previous != -1 && i != c) {
i = table[i].previous;
TOP_LINE = i;
}
}
}
a = -1, z = 0, i = TOP_LINE;
do {
strcpy(outBuffer[z], table[i].subStr(yy, data->WIDTH));
outBuffer[z][strlen(outBuffer[z])] = ' ';
outBuffer[z][data->WIDTH + 1] = '\0';
z++;
if(i == c) a = z;
i = table[i].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;
}

Jun 6 '07 #1
6 1829
In article <11*********************@p77g2000hsh.googlegroups. com>,
<di*****************@gmail.comwrote:
....
>#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...

Jun 6 '07 #2
sorry I mean g++ -o rosy rosy.cpp -lncurses not gcc. Such a bad habit
really :p

Jun 6 '07 #3
On Wed, 06 Jun 2007 09:46:34 -0700, in comp.lang.c ,
di*****************@gmail.com wrote:
>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
Jun 6 '07 #4
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

Jun 6 '07 #5
di*****************@gmail.com wrote:
>
sorry I mean g++ -o rosy rosy.cpp -lncurses not gcc. Such a bad
habit really :p
Ignore McCormack, he is a troll. Provide adequate quotations to
make your article stand alone.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 7 '07 #6
di*****************@gmail.com wrote:
>
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 ....
Wrong group. ncurses is not in the standard library, thus
off-topic. Try a group that deals with your system.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 7 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Volker Kamp | last post by:
Hi, I'm using the CLI SAPI of PHP to create ncurses programs. All works fine instead of debugging, since most PHP debuggers are not compatible to CLI. The worst is, that after calling...
9
by: Sinan Nalkaya | last post by:
hi, i want to use ncurses library in python i`ve found proper library for that, PyNcurses. then i searched for some documentation about ncurses programming, i only found that web site ;...
2
by: Martín Marconcini | last post by:
Hello there, I'm writting (or trying to) a Console Application in C#. I has to be console. I remember back in the old days of Cobol (Unisys), Clipper and even Basic, I used to use a program...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
17
by: Terry | last post by:
I have some old VB5 functions that specify types of Long and Double that make calls to an unmanaged 3rd party DLL probably just as old. The source for the DLL is not available. I'm getting some...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
17
by: Andrew C. | last post by:
I'm trying to complete excercise 1-23 in the K&R book, which calls for a program to wrap input lines at a given column, making sure to handle lines that wrap twice or don't contain whitespace. This...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.