Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 7184 → Rev 7183

/programs/develop/ktcc/trunk/libc/include/kos32sys0.h
File deleted
/programs/develop/ktcc/trunk/libc/include/stdio.h
23,12 → 23,9
char* buffer;
dword buffersize;
dword filesize; // too small
int filepos; // too small, may be -1
dword filepos; // too small
char* filename;
int mode;
int ungetc_buf;
dword buffer_start; // 1st byte position
dword buffer_end; // points after last buffered data
} FILE;
 
#define stderr ((FILE*)3) /* works only for fprintf!!! */
40,7 → 37,7
#define FILE_OPEN_TEXT 4
#define FILE_OPEN_PLUS 8
#define EOF (-1)
#define BUFSIZ (4096)
#define BUFSIZ (256)
#define FILENAME_MAX (0x400)
 
extern FILE* fopen(const char* filename, const char *mode);
/programs/develop/ktcc/trunk/libc/include/stdlib.h
10,10 → 10,6
#define abs(i) (((i)<0)?(-(i)):(i))
#define labs(li) abs(li)
 
#define min(a, b) ((a)<(b) ? (a) : (b))
#define max(a, b) ((a)>(b) ? (a) : (b))
 
 
extern int atoib(char *s,int b);
extern int atoi(char *s);
extern char *itoab(unsigned int n,char* s,int b);
/programs/develop/ktcc/trunk/libc/include/kolibrisys.h
49,7 → 49,7
//------------------------KolibriOS system acces to files----------------------------
//-----------------------------------------------------------------------------------
extern dword stdcall _ksys_get_filesize(char *filename);
extern dword stdcall _ksys_readfile(char *filename,dword pos,dword blocksize,void *data, int *preadbytes);
extern dword stdcall _ksys_readfile(char *filename,dword pos,dword blocksize,void *data);
extern dword stdcall _ksys_rewritefile(char *filename,dword blocksize,void *data);
extern dword stdcall _ksys_appendtofile(char *filename,dword pos,dword blocksize,void *data);
//-----------------------------------------------------------------------------------
/programs/develop/ktcc/trunk/libc/kolibrisys/_ksys_files_acces.asm
39,7 → 39,7
 
 
align 4
proc _ksys_readfile stdcall,filename:dword,position:dword,sizeblock:dword,buffer:dword, preadbytes:dword
proc _ksys_readfile stdcall,filename:dword,position:dword,sizeblock:dword,buffer:dword
 
xor eax,eax
mov ebx,[position]
58,9 → 58,6
mov ebx,fileinfo
int 0x40
 
mov esi, [preadbytes]
mov [esi], ebx
 
ret
endp
 
/programs/develop/ktcc/trunk/libc/stdio/fgetc.c
1,11 → 1,31
#include <stdio.h>
int fgetc(FILE* file)
{
int c = 0, rc;
dword res;
if(!file)
{
errno = E_INVALIDPTR;
return EOF;
}
rc = fread(&c, 1, 1, file);
if ((file->mode & 3)!=FILE_OPEN_READ && (file->mode & FILE_OPEN_PLUS)==0) return EOF;
 
if (rc < 1) return EOF;
 
return c;
if (file->filepos>=file->filesize)
{
return EOF;
}
else
{
res=_ksys_readfile(file->filename,file->filepos,1,file->buffer);
if (res==0)
{
file->filepos++;
return (int)file->buffer[0];
}
else
{
errno = -res;
return EOF; // errors are < 0
}
}
}
/programs/develop/ktcc/trunk/libc/stdio/fopen.c
113,9 → 113,6
res->filepos=0;
res->mode=imode;
res->filename=fullname;
res->ungetc_buf = EOF;
res->buffer_start = -1;
res->buffer_end = -1;
}
if(!res || !res->buffer || !res->filename)
{
123,7 → 120,7
return NULL;
}
 
if ((imode & 3) == FILE_OPEN_READ || (imode & 3) == FILE_OPEN_APPEND)
if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND))
{
if (sz > 0) /*already got*/
res->filesize = sz;
/programs/develop/ktcc/trunk/libc/stdio/fread.c
1,12 → 1,10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kolibrisys.h>
 
int fread(void *buffer,int size,int count,FILE* file)
{
dword res, readbytes;
dword fullsize, read4cache, toread, readcount;
dword res;
dword fullsize;
 
if(!file || !buffer)
{
21,74 → 19,22
}
 
fullsize=count*size;
if (fullsize + file->filepos >= file->filesize)
if ((fullsize+file->filepos)>=(file->filesize))
{
fullsize=file->filesize - file->filepos;
if (fullsize <= 0) return 0;
if (fullsize<=0) return(0);
}
 
/***** file buffering strategy, just read forward *****
if we read small part - read full buffer, but if buffer have this data - dont read again nothing (or partial read forward 4k pages)
any writes drops buffers as ungetc_buf */
read4cache = 0;
readcount = 0;
if (file->filepos >= file->buffer_start && file->filepos < file->buffer_end)
res=_ksys_readfile(file->filename,file->filepos,fullsize,buffer);
if (res==0)
{
read4cache = min(file->buffer_end - file->filepos, fullsize);
memcpy(buffer, file->buffer + file->filepos - file->buffer_start, read4cache);
file->filepos += read4cache;
if (file->ungetc_buf != EOF) // subst ungetc byte
{
*((char*)buffer) = (char)file->ungetc_buf;
file->ungetc_buf = EOF;
file->filepos=file->filepos+fullsize;
fullsize=fullsize/size;
return(fullsize);
}
buffer += read4cache; // ! advance
fullsize -= read4cache;
readcount = read4cache / size;
}
toread = max(fullsize, file->buffersize);
if (toread + file->filepos >= file->filesize)
{
toread = file->filesize - file->filepos;
}
if (fullsize <= 0 || toread <= 0)
res = 0; // already read or file end
else
{
file->buffer_start = file->filepos;
if (toread <= fullsize) // read to bigger buffer
{
res = _ksys_readfile(file->filename, file->filepos, toread, buffer, &readbytes);
read4cache = min(readbytes, file->buffersize);
memcpy(file->buffer, buffer, read4cache);
file->filepos += readbytes;
} else
{
res = _ksys_readfile(file->filename, file->filepos, toread, file->buffer, &readbytes);
read4cache = readbytes;
memcpy(buffer, file->buffer, min(fullsize, read4cache));
file->filepos += min(fullsize, read4cache);
}
file->buffer_end = file->buffer_start + read4cache;
if (readbytes >= fullsize)
readcount += fullsize / size;
else
readcount += readbytes / size;
}
 
if (file->ungetc_buf != EOF) // subst ungetc byte
{
*((char*)buffer) = (char)file->ungetc_buf;
file->ungetc_buf = EOF;
}
if (res != 0)
{
file->ungetc_buf = EOF;
errno = -res;
return 0;
}
return readcount; // really full readed plus cached items
}
/programs/develop/ktcc/trunk/libc/stdio/fgetpos.c
8,6 → 8,5
}
 
*pos=file->filepos;
return 0;
}
/programs/develop/ktcc/trunk/libc/stdio/fputc.c
2,9 → 2,55
int fputc(int c,FILE* file)
{
dword res;
if(!file)
{
errno = E_INVALIDPTR;
return EOF;
}
res = fwrite(&c, 1, 1, file);
if (res < 1) return EOF;
if ((file->mode & 3)==FILE_OPEN_READ)
{
errno = E_ACCESS;
return EOF;
}
file->buffer[0]=c;
if ((file->mode & 3)==FILE_OPEN_APPEND)
{
file->filepos=file->filesize;
file->filesize++;
res=_ksys_appendtofile(file->filename,file->filepos,1,file->buffer);
if (res!=0)
{
errno = -res;
return EOF;
}
file->filepos++;
return c;
}
if ((file->mode & 3)==FILE_OPEN_WRITE)
{
if (file->filepos==0)
{ //file not created
res=_ksys_rewritefile(file->filename,1,file->buffer);
if (res!=0)
{
errno = -res;
return EOF;
}
file->filepos++;
return c;
}
else
{ //file created and need append one byte
res=_ksys_appendtofile(file->filename,file->filepos,1,file->buffer);
if (res!=0)
{
errno = -res;
return EOF;
}
file->filepos++;
return c;
}
}
}
/programs/develop/ktcc/trunk/libc/stdio/fsetpos.c
10,7 → 10,6
if (*pos>=0)
{
file->filepos=*pos;
file->ungetc_buf = EOF;
return 0;
}
else
/programs/develop/ktcc/trunk/libc/stdio/fwrite.c
41,13 → 41,6
 
}
*/
file->ungetc_buf = EOF;
if (file->filepos >= file->buffer_start && file->filepos < file->buffer_end) // drop buffer, if change his data
{
file->buffer_start = -1;
file->buffer_end = -1;
}
if ((file->mode &3)==FILE_OPEN_WRITE || (file->mode&3)==FILE_OPEN_APPEND) // always true, as read checked previous
{
if (file->filepos==0)
/programs/develop/ktcc/trunk/libc/stdio/rewind.c
7,6 → 7,5
return;
}
 
file->ungetc_buf = EOF;
file->filepos=0;
}
/programs/develop/ktcc/trunk/libc/stdio/ungetc.c
1,8 → 1,5
#include <stdio.h>
// non standard realization - support for virtually change ONLY ONE char
 
 
 
// non standard realization - no support for virtually change char
int ungetc(int c,FILE* file)
{
dword res;
13,19 → 10,17
return EOF;
}
 
if ((file->mode & 3) != FILE_OPEN_READ && (file->mode & FILE_OPEN_PLUS) == 0)
if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0))
{
errno = E_ACCESS;
return EOF;
}
 
if (file->filepos > file->filesize || file->filepos == 0 || c == EOF || file->ungetc_buf != EOF)
if (file->filepos>file->filesize || file->filepos==0)
{
errno = E_EOF;
return EOF;
}
file->ungetc_buf = c;
file->filepos--;
 
return c;
/programs/develop/ktcc/trunk/libc/stdlib/exit.c
File deleted
\ No newline at end of file
/programs/develop/ktcc/trunk/readme.txt
16,6 → 16,7
-add stdin, stderr, stdout emulation íå õâàòàåò stdin, stdout - ìîæíî ñäåëàòü êàê stderr!, íî íàäî âîçèòüñÿ çàîäíî ñ ferror & feof
-getchar, gets if returs errorcode (0, null) - you must exit program, because of closed console window
-ïðè íîðìàëüíîì âûõîäå çàêðûâàòü êîíñîëü
-sstrek âèñíåò íà ïîèñêå õåëïà ñ äèñêåòû - just very long reading by one symbol without buffering (need to change gets, getc, ungetc etc)
 
 
------ errors ------
22,8 → 23,8
-not working: default search path are ./include ./lib from executable (under KOS need to use -Bpath_to_ktcc)
--start.o not found using -B (kos) - put near your.c file
-åñëè ïðîåêò ìíîãîôàéëîâûé - .dbg ãåíåðèò äóáëèðóþùèåñÿ ìåòêè äàííûõ, òèïà L.78 ìîæåò óêàçûâàòü íà äðóãîé ñåãìåíò (
-.dbg sometimes generated improperly for source code labels
 
 
----- fixed errors ------
-if static var sized more than 14096+ -> crash compiled .exe (kos)
(^ default stack size set at compile time tccmeos:177 is below 4k)
119,7 → 120,7
-may incorrect prints unsigned > 2147483647L
 
ungetc
-ungetc fails if filepos == 0 - by design
-ungetc fails if filepos == 0 - no tricks
 
all file ops limited to 2Gb
 
/programs/develop/ktcc/trunk/libctest/myfile.txt
File deleted
\ No newline at end of file
/programs/develop/ktcc/trunk/libctest/ungetc2.c
File deleted
\ No newline at end of file
/programs/develop/ktcc/trunk/libctest/ungetc.c
14,60 → 14,6
!strcmp((s),(x)) || \
(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kolibrisys.h>
 
#define fgetc fgetc_dbg
#define ungetc ungetc_dbg
 
int fgetc_dbg(FILE* file)
{
int c = 0, rc;
rc = fread(&c, 1, 1, file);
 
if (rc < 1) return EOF;
 
return c;
}
 
int ungetc_dbg(int c,FILE* file)
{
dword res;
 
if(!file)
{
errno = E_INVALIDPTR;
return EOF;
}
 
if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0))
{
errno = E_ACCESS;
return EOF;
}
 
if (file->filepos>file->filesize || file->filepos==0 || c == EOF || file->ungetc_buf != EOF)
{
errno = E_EOF;
return EOF;
}
file->ungetc_buf = c;
file->filepos--;
 
return c;
}
 
void mark(int n)
{
n++;
}
 
int main(void)
{
int i;
88,14 → 34,11
TEST(i, ftell(f), 0, "%d != %d");
TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d");
TEST(i, ftell(f), 0, "%d != %d");
mark(0x11);
printf("debug file ungetbuf=%d\n", f->ungetc_buf);
TEST(i, fgetc(f), 'x', "'%c' != '%c'");
TEST(i, ftell(f), 1, "%d != %d");
 
TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
TEST(i, ungetc('x', f), 'x', "%d != %d");
mark(0x22);
TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d");
a[14] = 0;
TEST_S(a, "xhello, world\n", "mismatch reading ungot character");
104,8 → 47,6
TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d");
TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d");
TEST(i, fgetc(f), 'x', "'%c' != '%c'");
mark(0x33);
TEST(i, ftell(f), 1, "%d != %d");
TEST(i, fgetc(f), 'h', "'%c' != '%c'");
 
printf("%s finished\n", __FILE__);