home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
bbs_ra
/
msgq160s.arj
/
TEXTFILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-28
|
6KB
|
321 lines
/*
* TEXTFILE.C - Write text files
*
* Msged/Q message editor for QuickBBS Copyright 1990 by P.J. Muller
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef __MSC__
#include <sys/types.h>
#endif
#include "msged.h"
#include "screen.h"
#include "qmsgbase.h"
#include <assert.h>
#define TEXTLEN 200
static FILE *repapp(char *path);
void import(LINE * l)
{
char line[TEXTLEN];
FILE *fp;
static char fn[PATHLEN];
LINE *n;
int key;
memset(line, 0, sizeof(line));
gotoxy(9, 1);
clreol();
set_color(co_info);
bputs("File to import? ");
key = bgets(fn, PATHLEN);
set_color(co_normal);
if (key == ABORT) return;
if ((fp = fopen(fn, "rt")) != NULL) {
while (fgets(line, min(maxx,TEXTLEN), fp) != NULL) {
if (l->text != NULL) {
if ((n = (LINE *) calloc(1, sizeof(LINE))) == NULL)
goto error;
n->next = l->next;
if (n->next == NULL)
msgbuf.last = n;
else
n->next->prev = n;
n->prev = l;
l->next = n;
l = n;
}
else
n = l;
if ((n->text = strdup(line)) == NULL)
goto error;
memset(line, 0, sizeof(line));
}
fclose(fp);
}
return;
error: /* out of memory error */
gotoxy(9, 1);
clreol();
bputs("Not enough memory, press any key");
video_update();
(void)getkey();
return;
} /* import */
void export(LINE * f)
{
FILE *fp;
static char fn[PATHLEN];
int key;
gotoxy(9, 1);
clreol();
set_color(co_info);
bputs("File name to write to? ");
if (fn[0] == EOS)
strcpy(fn, outfile);
key = bgets(fn, sizeof(fn));
set_color(co_normal);
if (key == ABORT) return;
if ((fp = repapp(fn)) != NULL) {
fputc('\n', fp);
for (; f != NULL; f = f->next)
if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
fputs(f->text, fp);
if (strchr(f->text, '\n') == NULL)
fputc('\n', fp);
}
fclose(fp);
}
else {
gotoxy(9, 1);
clreol();
set_color(co_warn);
bputs("Can't open file, press any key :");
video_update();
(void)getkey();
set_color(co_normal);
}
showheader(message);
}
void writetxt()
{
LINE *f = msgbuf.first;
FILE *fp;
static char fn[PATHLEN];
int key;
gotoxy(9, 1);
clreol();
set_color(co_info);
bputs("File name to write to? ");
if (fn[0] == EOS)
strcpy(fn, outfile);
key = bgets(fn, sizeof(fn));
set_color(co_normal);
if (key == ABORT) return;
if ((fp = repapp(fn)) == NULL) {
gotoxy(9, 1);
clreol();
set_color(co_warn);
bputs("Can't open file, press any key :");
video_update();
(void)getkey();
set_color(co_normal);
return;
}
fprintf(fp, "\n%03d/%03d %s\n",
boardmsg(CurBoard, message.header.msgnum), countmsg(CurBoard),
timedate(message.header.posttime, message.header.postdate, NO));
fprintf(fp, "From: %s", message.header.from);
if (arealist[area].netmail)
fprintf(fp, " of %s", formaddr(message.from,Z_O|N_A|P_O|D_O|A_INT));
/*fprintf(fp, " of %s", formaddr(thisnode[CurAKA],Z_O|N_A|P_O|D_O|A_INT));*/
fputc('\n', fp);
fprintf(fp, "To: %s", message.header.to);
if (arealist[area].netmail) {
fprintf(fp, " of %s", formaddr(message.to,Z_O|N_A|P_O|D_O|A_INT));
}
fputc('\n', fp);
if (message.header.bits.is_file)
fprintf(fp, "Files: %s", message.header.subj);
else
fprintf(fp, "Subj: %s", message.header.subj);
fputc('\n', fp);
fputs("Attr: ", fp);
fputs(bits2ascii(message.header.bits,TRUE), fp);
fputc('\n', fp);
{
char buf[80];
strcpy(buf,"------------------------------------------------\n");
memcpy(buf,arealist[area].description,
min(strlen(arealist[area].description),strlen(buf)-1));
fputs(buf, fp);
}
for (; f != NULL; f = f->next)
if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
fputs(f->text, fp);
if (strchr(f->text, '\n') == NULL)
fputc('\n', fp);
}
if (isatty(fileno(fp)))
fputc(12, fp);
fclose(fp);
} /* writetxt */
static FILE *repapp(char *path)
{
FILE *fp;
int ch;
if ((fp = fopen(path,"r")) == NULL)
return (fp = fopen(path,"wt"));
if (isatty(fileno(fp))) {
fclose(fp);
return(fp = fopen(path,"wt"));
}
fclose(fp);
gotoxy(9,1);
clreol();
set_color(co_hilite);
bputc('r');
set_color(co_info);
bputs("eplace or ");
set_color(co_hilite);
bputc('a');
set_color(co_info);
bputs("ppend? ");
video_update();
ch = getkey() & 0x7f;
ch = tolower(ch);
if (ch == 0x1b)
return(NULL);
while ((ch != 'a') && (ch != 'r')) {
ch = 0x7f & getkey();
ch = tolower(ch);
if (ch == 0x1b)
return(NULL);
}
if (ch == 'a')
fp = fopen(path,"at");
else
fp = fopen(path,"wt");
return(fp);
}
/*
* Write the whole text buffer to a file, overwrite the path if it exists
*/
#ifdef EXTERNEDIT
BOOLEAN writebuffile(char *path, BUFFER *buf)
{
LINE *p;
FILE *f;
BOOLEAN ok = FALSE;
if ((f = fopen(path,"wt")) == NULL)
return FALSE;
p = buf->first;
while (p != NULL) {
if ((p->text != NULL) && (shownotes || (*(p->text) != '\01'))) {
if (strcmp(p->text,"---") == 0) break; /* stop at tearline */
if (fputs(p->text, f) == EOF) break;
if (strchr(p->text, '\n') == NULL) /* hard CR */
fputc('\n', f);
}
p = p->next;
}
ok = (p == NULL);
if (fclose(f) != 0)
ok = FALSE;
return ok;
} /* writebuffile */
BOOLEAN readbuffile(char *path, BUFFER *buf)
{
struct stat st;
int fd = -1;
char *text = NULL;
BOOLEAN ok = FALSE;
if ((fd = open(path, O_RDONLY|O_BINARY)) == -1)
goto fail;
if (fstat(fd, &st) == -1)
goto fail;
clearbuffer(buf);
if (st.st_size != 0) {
if ((text = malloc(st.st_size)) == NULL)
goto fail;
if (read(fd, text, st.st_size) != st.st_size)
goto fail;
} else {
text = strdup("\r");
}
*buf = buffer(&text);
ok = TRUE;
fail:
if (fd != -1)
if (close(fd) != 0)
ok = FALSE;
if (text != NULL)
free(text);
return ok;
} /* readbuffile */
#endif