small polish, samples

git-svn-id: svn://kolibrios.org@6443 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
siemargl 2016-06-10 11:10:52 +00:00
parent b718363009
commit 3e571bd7cc
15 changed files with 487 additions and 24 deletions

View File

@ -90,6 +90,13 @@ int vprintf ( const char * format, va_list arg );
int vsprintf (char * s, const char * format, va_list arg );
int vfprintf ( FILE * stream, const char * format, va_list arg );
int tiny_sprintf (char * s, const char * format, ... );
int tiny_snprintf (char * s, size_t n, const char * format, ... );
int tiny_vsnprintf (char * s, size_t n, const char * format, va_list args );
// support %c, %s, %d, %x, %u, %% for 32-bit values only. no width specs, left align
// always zero-ended
extern int errno;
/* errors codes from KOS, but minus */
#ifndef E_SUCCESS

View File

@ -12,8 +12,8 @@
extern int atoib(char *s,int b);
extern int atoi(char *s);
extern char *itoab(int n,char* s,int b);
extern char *itoa(int n,char* s);
extern char *itoab(unsigned int n,char* s,int b);
extern char *__itoa(int n,char* s);
extern void* stdcall malloc(dword size);
extern void stdcall free(void *pointer);
@ -25,6 +25,8 @@ extern void srand (unsigned int seed);
double strtod (const char* str, char** endptr);
long double strtold (const char* str, char** endptr);
float strtof (const char* str, char** endptr);
long int strtol (const char* str, char** endptr, int base);
#define strtoul(s, ep, b) ((unsigned long int)strtol(s, ep, b))
void* calloc (size_t num, size_t size);
@ -41,5 +43,8 @@ typedef div_t ldiv_t;
div_t div (int numer, int denom);
#define ldiv(a, b) div(a, b)
#define atol(a) atoi(a)
#define atof(a) strtod(a, NULL)
#endif

View File

@ -28,9 +28,10 @@ start:
;DEBUGF ' path "%s"\n params "%s"\n', .path, .params
; check for overflow
mov al, [path+buf_len-1]
or al, [params+buf_len-1]
jnz .crash
;; that not work
; mov al, [path+buf_len-1]
; or al, [params+buf_len-1]
; jnz .crash
; check if path written by OS
mov eax, [hparams]
test eax, eax

View File

@ -47,7 +47,7 @@ int vscanf ( const char * format, va_list arg )
return format_scan(NULL, format, arg, &virtual_getc_con, &virtual_ungetc_con);
};
int kos_scanf ( const char * format, ...)
int scanf ( const char * format, ...)
{
va_list arg;
int n;

View File

@ -0,0 +1,97 @@
/*
function for format output to the string. much lighter than standard sprintf
because of lesser formats supported
*/
#include <string.h>
//#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
char* __itoa(int n,char* s);
char* itoab(unsigned int n, char* s, int b);
int tiny_vsnprintf (char * s, size_t n, const char * format, va_list args )
// support %c, %s, %d, %x, %u, %% for 32-bit values only. no width specs, left align
// always zero-ended
{
char *fmt, *dest, buf[32];
fmt = (char*)format;
dest = s; dest[n - 1] = '\0';
int arg, len;
while (*fmt && (dest - s < n - 1))
{
if (*fmt != '%')
{
*dest++ = *fmt++;
continue;
}
if (fmt[1] == '%') // %%
{
*dest++ = '%';
fmt += 2;
continue;
}
arg = va_arg(args, int);
len = n - 1 - (dest - s);
switch (*++fmt)
{
case 'c':
*dest++ = (char)arg;
break;
case 's':
strncpy(dest, (char*)arg, len);
dest = strchr(dest, 0);
break;
case 'd':
__itoa(arg, buf);
strncpy(dest, buf, len);
dest = strchr(dest, 0);
break;
case 'x':
itoab((unsigned)arg, buf, 16);
strncpy(dest, buf, len);
dest = strchr(dest, 0);
break;
case 'u':
itoab((unsigned)arg, buf, 10);
strncpy(dest, buf, len);
dest = strchr(dest, 0);
break;
default:
*dest++ = *fmt;
}
fmt++;
}
*dest = '\0';
return dest - s;
}
int tiny_snprintf (char * s, size_t n, const char * format, ... )
{
va_list arg;
int rc;
va_start(arg, format);
rc = tiny_vsnprintf(s, n, format, arg);
va_end(arg);
return rc;
}
int tiny_sprintf (char * s, const char * format, ... )
{
va_list arg;
int rc;
va_start(arg, format);
rc = tiny_vsnprintf(s, 4096, format, arg);
va_end(arg);
return rc;
}

View File

@ -6,11 +6,15 @@
/*
** itoa(n,s) - Convert n to characters in s
*/
char* itoa(int n,char* s)
char* __itoa(int n,char* s)
{
int sign;
char *ptr;
ptr = s;
if(n == (int)0x80000000)
return strcpy(s, "-2147483648"); // overflowed -n
if ((sign = n) < 0) n = -n;
do {
*ptr++ = n % 10 + '0';

View File

@ -7,7 +7,7 @@
** itoab(n,s,b) - Convert "unsigned" n to characters in s using base b.
** NOTE: This is a non-standard function.
*/
char* itoab(int n,char* s,int b)
char* itoab(unsigned int n, char* s, int b)
{
char *ptr;
int lowbit;
@ -15,7 +15,7 @@ char* itoab(int n,char* s,int b)
b >>= 1;
do {
lowbit = n & 1;
n = (n >> 1) & 32767;
n = (n >> 1) & 0x7FFFFFFF;
*ptr = ((n % b) << 1) + lowbit;
if(*ptr < 10) *ptr += '0'; else *ptr += 55;
++ptr;

View File

@ -0,0 +1,78 @@
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define LONG_MIN (-2147483647L-1)
#define LONG_MAX (2147483647L)
#define ULONG_MAX (4294967295UL)
int getdigit(char ch, int base)
{
if (isdigit(ch)) ch-= '0';
else
if (isalpha(ch) && ch <= 'Z') ch = 10 + ch - 'A';
else
if (isalpha(ch)) ch = 10 + ch - 'a';
else
return -1;
if (ch / base != 0) return -1;
return ch;
}
long int strtol (const char* str, char** endptr, int base)
{
long int res = 0;
int sign = 1;
if (base > 36)
{
errno = EINVAL;
goto bye;
}
while (isspace(*str)) str++;
if (*str == '-') { sign = -1; str++; }
else
if (*str == '+') str++;
if (base == 0 || base == 16)
{
if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
{
base = 16;
str += 2;
}
}
if (base == 0 && *str == '0') base = 8;
if (base == 0) base = 10;
int digit;
while ((digit = getdigit(*str, base)) >= 0)
{
res = base * res + digit;
str++;
if (res < 0)
{
errno = ERANGE;
if (sign > 0)
res = LONG_MAX;
else
res = LONG_MIN;
}
}
bye:
if (endptr)
*endptr = (char*)str;
return res * sign;
}

View File

@ -10,6 +10,8 @@ int memcmp(const void* buf1,const void* buf2,size_t count)
return -1;
if (*(uc*)buf1>*(uc*)buf2)
return 1;
(uc*)buf1++;
(uc*)buf2++;
}
return 0;
}

View File

@ -0,0 +1,23 @@
// demonstration conio use, color text
// more info in conio.h
#include <conio.h>
int main()
{
int i;
if (con_init_console_dll()) return 1; // init fail
// con_write_asciiz("\033[0;31;42m test \n"); // red on green bk
for(i = 30; i < 48; i++)
{
con_printf("\033[%dmColor 0x%02X: ", i, i);
con_write_asciiz("Text sample.");
con_printf(" printf %s test %d\n", "small", i);
}
con_exit(0);
}

View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int i;
char c;
FILE *f;
FILE *fin;
FILE *fout;
//write to file
f=fopen("testfile.txt","w");
for(i=0;i<50;i++)
{
fputc('1',f);
}
fclose(f);
//append to file
f=fopen("testfile.txt","a");
for(i=0;i<50;i++)
{
fputc('2',f);
}
fclose(f);
//copy from testfile.txt to copyfile.txt
fin=fopen("testfile.txt","r");
fout=fopen("copyfile.txt","w");
while((c=fgetc(fin))!=EOF)
{
fputc(c,fout);
}
fclose(fin);
fclose(fout);
}

View File

@ -0,0 +1,61 @@
// simple sample by Ghost
#include <stdio.h>
#include <string.h>
#include <kolibrisys.h>
#define FONT0 0
#define FONT1 0x10000000
#define BT_NORMAL 0
#define BT_DEL 0x80000000
#define BT_HIDE 0x40000000
#define BT_NOFRAME 0x20000000
char header[]={" -= C demo programm. Compiled whith KTCC halyavin and andrew_programmer port =- "};
void rotate_str(char *str){
char tmp;
int i;
tmp = str[0];
for(i = 1; str[i]; i++)str[i - 1] = str[i];
str[i - 1] = tmp;
}
void draw_window(){
static int offs = 0;
static int fcolor = 0;
static int col = 0;
_ksys_window_redraw(1);
_ksys_draw_window(100, 100, 300, 120, 0xaabbcc, 2, 0x5080d0, 0, 0x5080d0);
_ksys_write_text(6 - offs, 8, fcolor | FONT0, header, strlen(header));
_ksys_draw_bar(1, 6, 5, 13, 0x05080d0);
_ksys_draw_bar(274, 6, 26, 13, 0x05080d0);
_ksys_make_button(300 - 19, 5, 12, 12, 1 | BT_NORMAL, 0x6688dd);
_ksys_window_redraw(2);
offs = (offs + 1) % 6;
if(!offs)rotate_str(header);
fcolor += (col)?-0x80808:0x80808;
if(fcolor > 0xf80000 || fcolor == 0)col = !col;
}
int main(int argc, char **argv){
while(!0){
switch(_ksys_wait_for_event(10)){
case 2:return 0;
case 3:
if(_ksys_get_button_id() == 1)return 0;
break;
default:
draw_window();
break;
}
}
}

View File

@ -0,0 +1,148 @@
/*
newlib-style window example
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "kos32sys1.h"
struct kolibri_system_colors sys_color_table;
char statusbar[255];
char proc_info[1024];
char text_line[255];
enum BUTTONS
{
BTN_QUIT = 1,
BTN_POP = 10,
BTN_UNLOCK = 11
};
#define FONT_W 8
#define FONT_H 14
#define LINES 10
void draw_window()
{
int win_hight, win_width, i, pos_y = get_skin_height() + 36; // 60 == 24+36
// start redraw
begin_draw();
// define&draw window
sys_create_window(10, 40, 600, 400, "My window", /*sys_color_table.work_area*/0xFFFFFF, 0x13);
get_proc_info(proc_info);
win_width = *(int*)(proc_info + 0x3E); // client, 2A windows
win_hight = *(int*)(proc_info + 0x42); // client, 2E windows
define_button((10 << 16) + 80, (30 << 16) + 20, BTN_POP, sys_color_table.work_button);
draw_text_sys("BUTTON1", 15, 34, 0, 0x90000000 | sys_color_table.work_button_text); //0x80000000 asciiz
define_button((100 << 16) + 100, (30 << 16) + 20, BTN_UNLOCK, sys_color_table.work_button);
draw_text_sys("BUTTTON2", 110, 34, 0, 0x90000000 | sys_color_table.work_button_text);
// display statusbar
draw_bar(6, win_hight - 17, win_width - 11, 12, 0x80000000 | sys_color_table.work_area); //0x80000000 gradient
draw_text_sys(statusbar, 10, win_hight - 15, 0, 0x80000000 | sys_color_table.work_text);
// display strings
for (i = LINES; i > 0; i--)
{
tiny_snprintf (text_line, sizeof text_line, "Line[%d]<<Just a text>>", i);
text_line[(win_width - 10 - 5) / FONT_W + 1] = '\0'; // clip text size, seems to big lines crashing OS, and form len by window size
// draw_number_sys(nbytes, 5, pos_y, 6, 0x10000000); 8x12 font
draw_text_sys(text_line, 5, pos_y, 0, 0x90000000 /*| sys_color_table.work_text*/);
pos_y += FONT_H;
if(pos_y + 29 > win_hight) break; // 12 font + 12 statusbar + 5 border
}
// end redraw
end_draw();
}
int main()
{
int gui_event;
uint32_t pressed_button = 0, mouse_button;
pos_t mouse_pos;
strcpy(statusbar, "Program running...Double click on TEXT for details");
get_system_colors(&sys_color_table);
set_event_mask(0xC0000027); // mouse events only when focused window and mouse inside
do /* Start of main activity loop */
{
// gui_event = wait_for_event(10); // 100 = 1 sec, case you have background work
gui_event = get_os_event();
switch(gui_event)
{
case KOLIBRI_EVENT_NONE:
// background work
break;
case KOLIBRI_EVENT_REDRAW:
draw_window();
break;
case KOLIBRI_EVENT_KEY:
// scroll
break;
case KOLIBRI_EVENT_BUTTON:
pressed_button = get_os_button();
switch (pressed_button)
{
case BTN_POP:
strcpy(statusbar, "POP pressed....");
draw_window();
break;
case BTN_UNLOCK:
strcpy(statusbar, "UNLOCK pressed....");
draw_window();
break;
case BTN_QUIT:
return 0;
break;
}
break;
case KOLIBRI_EVENT_MOUSE:
mouse_pos = get_mouse_pos(POS_WINDOW); // window relative
mouse_button = get_mouse_eventstate();
debug_board_printf("mouse ev (%d,%d)%x\n", mouse_pos.x, mouse_pos.y, mouse_button);
if (mouse_button & (1<<24)) // double click
{
int n = (mouse_pos.y - 60) / FONT_H;
if (n < 0 || n >= LINES) break;
debug_board_printf("click on str(%d), clip slot(%d)\n", n, LINES - n - 1);
tiny_sprintf(statusbar, "click on str(%d), clip slot(%d)\n", n, LINES - n - 1);
draw_window();
}
// ignore
break;
}
} while(1) ; /* End of main activity loop */
return 0;
}
void __attribute__ ((noinline)) debug_board_write_str(const char* str){
while(*str)
debug_board_write_byte(*str++);
}
void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
{
va_list ap;
char log_board[300];
va_start (ap, format);
tiny_vsnprintf(log_board, sizeof log_board, format, ap);
va_end(ap);
debug_board_write_str(log_board);
}

View File

@ -18,8 +18,6 @@ otherwise (error) underscoring all symbols, not only cdecl
-silent (kos) -> writes to debugboard
-impossible using with mingw-gcc compiled lib, incompatible library format:
.o is PE-format from gcc but ELF from tcc, may be linux-gcc does it ok
-no symbols (mapfile) for debug, see howtodebugtcc
-no debug info for -g (kos32 linker imperfection)
-__fastcall incompatible with other compilers. now stack freed by caller.
must fix i386-gen.c@490,572 (fixed in other branch https://github.com/mirror/tinycc)
@ -30,8 +28,6 @@ otherwise (error) underscoring all symbols, not only cdecl
-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
-if static var sized more than 14096+ -> crash compiled .exe (kos)
---^ stack size set in menuet header at compile time tccmeos.c:177 about 4k
-bench timing coarse (0s or 1s), no usec in newlib gettimeofday. OK
Tests status:
@ -93,9 +89,6 @@ setvbuf
stdlib.h:
atof
atol
strtol, strtoul
atexit
getenv
system
@ -114,13 +107,12 @@ strxfrm
Status or libc tests
---FAILED---
tstring - need to fix
strtoul incorrect work with big unsigned > MAX_LONG
---NOT TESTED---
no library fns realized
qsort
strtol
time
---HANG---
@ -129,9 +121,9 @@ sscanf
---STACK IS SMALL---
use new -stack=1280000 option to pass test
tstring
strtodlong
use new -stack=1280000 option
--other--
@ -141,6 +133,7 @@ fscanf
snprintf
-some format misturbances
-may incorrect prints unsigned > 2147483647L
ungetc
-ungetc fails if filepos == 0 - no tricks

View File

@ -358,7 +358,8 @@ int tcc_output_dbgme(const char *filename, me_info* me)
// return 1 on error
{
FILE *fdbg;
char fname[400], buf[200];
char fname[400],
buf[80]; // no more fits in mtdbg string
strcpy(fname, filename);
strcat(fname, ".dbg");