home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
CSSRC
/
TEDFILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-12-06
|
2KB
|
112 lines
/*
tedfile.c
% ted_ReadFile, ted_WriteFile
file read and write for text editting
C-scape 3.2
Copyright (c) 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
---------------------
2/07/89 jmd added cast to omalloc call
3/24/89 jmd added sed_ macros
2/21/90 jmd took credit for fixing ted_ReadFile return value
3/28/90 jmd ansi-fied
7/25/90 jdc fixed return for memory out situations
10/12/90 jdc added error checks
10/30/90 jdc added break in read loop on SetTB failure
12/06/90 jdc fixed read from 0 len file bug
*/
#include "ted.h"
#define _TED_BUFSIZE 1000
long ted_ReadFile(sed_type sed, FILE *fp)
/*
fills the sed's textbuffer from fp.
returns: number of characters read or -1L if error
*/
{
unsigned int size;
char *buf;
boolean ok = TRUE;
buf = (char *) omalloc(CSA_TEDREADF, _TED_BUFSIZE);
sed_ClearTB(sed);
rewind(fp);
while ((size = fread(buf, 1, _TED_BUFSIZE, fp)) != 0) {
if ((ok = sed_SetTB(sed, buf, size)) == FALSE) {
break;
}
}
/* get rid of the extra '\n' */
if (ted_GetSize(sed) > 1L && (sed_GetTextbuf(sed))->len == 1) {
menu_DelRowTB(sed_GetMenu(sed), tb_GetRow(sed_GetTextbuf(sed)), 0);
}
sed_RewindTB(sed);
ofree(CSA_TEDREADF, buf);
return((ferror(fp) != 0 || !ok) ? -1L : ted_GetSize(sed));
}
long ted_WriteFile(sed_type sed, FILE *fp, int mode)
/*
writes the sed's textbuffer to fp
returns: number of characters written or -1L if error
*/
{
unsigned int ref, llen;
long clen = 0L, cursor = 0L;
bbc_type bbc;
bbpeek_struct bp;
int row, col, dlen;
ted_GetPosition(sed, &row, &col);
ref = ted_GetRefresh(sed);
ted_SetRefresh(sed, TED_NOREFRESH);
rewind(fp);
sed_RewindTB(sed);
bbc = (sed_GetTextbuf(sed))->bbc;
do {
llen = ted_GetLineLen(sed);
bp.b = bbc->b;
bp.off = bp.b->off;
bp.len = (long)llen;
do {
dlen = bbpeek(&bp);
fwrite(bp.p, 1, dlen, fp);
bp.off += dlen;
bp.len -= (long)dlen;
} while (bp.len > 0L);
if (mode == TED_HARD && !ted_IsHardLine(sed)) {
fwrite("\n", 1, 1, fp);
clen++;
}
clen += llen;
cursor += (long)llen;
} while (ted_GotoCursor(sed, cursor));
ted_GotoPosition(sed, row, col);
ted_SetRefresh(sed, ref);
return((ferror(fp) != 0) ? -1L : clen);
}