Subversion Repositories Kolibri OS

Rev

Rev 1005 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2.  
  3. #include "func.h"
  4.  
  5. int SysColor = 0;
  6. char debuf[50] = "";
  7.  
  8. // ïî÷åìó-òî íå áûëî â ñòàíäàðòíîé áèáëèîòåêå
  9. void kos_DrawLine( Word x1, Word y1, Word x2, Word y2, Dword colour, Dword invert )
  10. {
  11.         Dword arg1, arg2, arg3;
  12.  
  13.         //
  14.         arg1 = ( x1 << 16 ) | x2;
  15.         arg2 = ( y1 << 16 ) | y2;
  16.         arg3 = (invert)?0x01000000:colour;
  17.         //
  18.         __asm{
  19.                 mov eax, 38
  20.                 mov ebx, arg1
  21.                 mov ecx, arg2
  22.                 mov edx, arg3
  23.                 int 0x40
  24.         }
  25. }
  26.  
  27. // ïîõèùåíî èç áèáëèîòåêè ê C--
  28. void DrawRegion(Dword x,Dword y,Dword width,Dword height,Dword color1)
  29. {
  30.         kos_DrawBar(x,y,width,1,color1); //ïîëîñà ãîð ñâåðõó
  31.         kos_DrawBar(x,y+height,width,1,color1); //ïîëîñà ãîð ñíèçó
  32.         kos_DrawBar(x,y,1,height,color1); //ïîëîñà âåðò ñëåâà
  33.         kos_DrawBar(x+width,y,1,height+1,color1); //ïîëîñà âåðò ñïðàâà
  34. }
  35.  
  36.  
  37. // äà, ýòî áàÿí
  38. int atoi(const char* string)
  39. {
  40.         int res=0;
  41.         int sign=0;
  42.         const char* ptr;
  43.         for (ptr=string; *ptr && *ptr<=' ';ptr++);
  44.         if (*ptr=='-') {sign=1;++ptr;}
  45.         while (*ptr >= '0' && *ptr <= '9')
  46.         {
  47.                 res = res*10 + *ptr++ - '0';
  48.         }
  49.         if (sign) res = -res;
  50.         return res;
  51. }
  52.  
  53. /*int abs(int n)
  54. {
  55.         return (n<0)?-n:n;
  56. }*/
  57.  
  58.  
  59.  
  60.  
  61.  
  62. double __cdecl fabs(double x)
  63. {
  64.         __asm   fld     x
  65.         __asm   fabs
  66. }
  67. #define M_PI       3.14159265358979323846
  68. double __cdecl cos(double x)
  69. {
  70.         __asm   fld     x
  71.         __asm   fcos
  72. }
  73. double __cdecl sin(double x)
  74. {
  75.         __asm   fld     x
  76.         __asm   fsin
  77. }
  78.  
  79. int di(double x)
  80. {
  81.         int a;
  82.         __asm fld x
  83.         __asm fistp a
  84.         return a;
  85. }
  86.  
  87. double id(int x)
  88. {
  89.         double a;
  90.         __asm fild x
  91.         __asm fstp a
  92.         return a;
  93. }
  94.  
  95. bool isalpha(char c)
  96. {
  97.         return (c==' ' || c=='\n' || c=='\t' || c=='\r');
  98. }
  99.  
  100. // ýòà ôóíêöèÿ - âåëîñèïåä. íî ïðîùå áûëî íàïèñàòü ÷åì íàéòè.
  101. double convert(char *s, int *len)
  102. {
  103.  
  104.         int i;
  105.  
  106.  
  107.         double sign,res, tail, div;
  108.  
  109.         res = 0.0;
  110.  
  111.         i=0;
  112.         while (s[i] && isalpha(s[i])) i++;
  113.         if (len) *len=i;
  114.         if (s[i] == '\0')
  115.                 return ERROR_END;
  116.  
  117.         sign=1.0;
  118.         if (s[i] == '-')
  119.         {
  120.                 sign=-1.0;
  121.                 i++;
  122.         }
  123.         while (s[i] && s[i] >= '0' && s[i] <= '9')
  124.         {
  125.                 res *= 10.0;
  126.                 res += id(s[i] - '0');
  127.                 i++;
  128.         }
  129.         if (len) *len=i;
  130.         if (!s[i] || isalpha(s[i]))
  131.                 return sign*res;
  132.         if (s[i] != '.' && s[i] != ',')
  133.                 return ERROR;
  134.         i++;
  135.         if (len) *len=i;
  136.         if (!s[i])
  137.                 return sign*res;
  138.        
  139.         div = 1.0;
  140.         tail = 0.0;
  141.         while (s[i] && s[i] >= '0' && s[i] <= '9')
  142.         {
  143.                 tail *= 10.0;
  144.                 tail += id(s[i] - '0');
  145.                 div *= 10.0;
  146.                 i++;           
  147.         }
  148.         res += tail/div;
  149.         if (len) *len=i;
  150.         return sign*res;
  151. }
  152.  
  153.  
  154.  
  155. #define PREC 2
  156.  
  157. double double_tab[]={1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15};
  158.  
  159. // ýòî sprintf, óìåþùèé ôîðìàòèðîâàòü _òîëüêî_ âåùåñòâåííûå ÷èñëà (double) %f
  160. void format( char *Str, int len, char* Format, ... )
  161. {
  162.         int i, fmtlinesize, j, k, flag;
  163.         char c;
  164.         va_list arglist;
  165.         //
  166.         va_start(arglist, Format);
  167.  
  168.         //
  169.         fmtlinesize = strlen( Format );
  170.         //
  171.         if( fmtlinesize == 0 ) return;
  172.  
  173.         for (i = 0; i < len; i++)
  174.                 Str[i] = 0;
  175.  
  176.         //
  177.         for( i = 0, j = 0; i < fmtlinesize; i++ )
  178.         {
  179.                 //
  180.                 c = Format[i];
  181.                 //
  182.                 if( c != '%' )
  183.                 {
  184.                         Str[j++] = c;
  185.                         continue;
  186.                 }
  187.                 //
  188.                 i++;
  189.                 //
  190.                 if( i >= fmtlinesize ) break;
  191.  
  192.                 //
  193.                 flag = 0;
  194.                 //
  195.                 c = Format[i];
  196.                 //
  197.                 switch( c )
  198.                 {
  199.                 //
  200.                 case '%':
  201.                         Str[j++] = c;
  202.                         break;
  203.                 // auaia aauanoaaiiiai ?enea
  204.                 case 'f':
  205.                         // ii?aaaeeou ?enei oeo? ai oi?ee
  206.                         double val, w;
  207.                         int p;
  208.                         val = va_arg(arglist, double);
  209.                         if (val < 0.0)
  210.                         {
  211.                                 Str[j++] = '-';
  212.                                 val = -val;
  213.                         }
  214.                         for (k = 0; k < 15; k++)
  215.                                 if (val < double_tab[k])
  216.                                         break;
  217.  
  218.                         if (val < 1.0)
  219.                         {
  220.                                 Str[j++] = '0';
  221.                         }
  222.                        
  223.                         for (p = 1; p < k + 1; p++)
  224.                         {
  225.                                 Str[j++] = '0' + di(val / double_tab[k - p] - 0.499) % 10;
  226.                         }
  227.                         Str[j++] = '.';
  228.                         w = 0.1;
  229.                         for (p = 0; p < 2; p++)
  230.                         {
  231.                                 val-=floor(val);
  232.                                 Str[j++] = '0' + di(val / w - 0.499) % 10;
  233.                                 w /= 10.0;
  234.                         }
  235.  
  236.                 //
  237.                 default:
  238.                         break;
  239.                 }
  240.         }
  241.         //
  242.         Str[j] = 0;
  243. }
  244.  
  245. #ifndef AUTOBUILD
  246. void *memcpy(void *dst, const void *src, unsigned size)
  247. {
  248.         while (size--)
  249.                 *((char*)dst+size) = *((char*)src+size);
  250.         return dst;
  251. }
  252. #endif
  253.  
  254. int strcmp(const char *s1, const char *s2)
  255. {
  256.         int i;
  257.  
  258.         if (s1 == NULL)
  259.                 if (s2 == NULL)
  260.                         return 0;
  261.                 else
  262.                         return 1;
  263.         else
  264.                 if (s2 == NULL)
  265.                         return 1;
  266.  
  267.         for (i = 0;;i++)
  268.         {
  269.                 if (s1[i] == '\0')
  270.                         if (s2[i] == '\0')
  271.                                 return 0;
  272.                         else
  273.                                 return 1;
  274.                 else
  275.                         if (s2[i] == '\0')
  276.                                 return 1;
  277.                         else
  278.                         {
  279.                                 if (s1[i] != s2[i])
  280.                                         return 1;
  281.                         }
  282.         }
  283.         return 0;
  284. }
  285.  
  286. kol_struct_import* kol_cofflib_load(char *name)
  287. {
  288. //asm ("int $0x40"::"a"(68), "b"(19), "c"(name));
  289.         __asm
  290.         {
  291.                 mov eax, 68
  292.                 mov ebx, 19
  293.                 mov ecx, name
  294.                 int 0x40
  295.         }
  296. }
  297.  
  298.  
  299. void* kol_cofflib_procload (kol_struct_import *imp, char *name)
  300. {
  301.        
  302. int i;
  303. for (i=0;;i++)
  304.         if ( NULL == ((imp+i) -> name))
  305.                 break;
  306.         else
  307.                 if ( 0 == strcmp(name, (imp+i)->name) )
  308.                         return (imp+i)->data;
  309. return NULL;
  310.  
  311. }
  312.  
  313.  
  314. unsigned kol_cofflib_procnum (kol_struct_import *imp)
  315. {
  316.        
  317. unsigned i, n;
  318.  
  319. for (i=n=0;;i++)
  320.         if ( NULL == ((imp+i) -> name))
  321.                 break;
  322.         else
  323.                 n++;
  324.  
  325. return n;
  326.  
  327. }
  328.  
  329.  
  330. void kol_cofflib_procname (kol_struct_import *imp, char *name, unsigned n)
  331. {
  332.        
  333. unsigned i;
  334. *name = 0;
  335.  
  336. for (i=0;;i++)
  337.         if ( NULL == ((imp+i) -> name))
  338.                 break;
  339.         else
  340.                 if ( i == n )
  341.                         {
  342.                         strcpy(name, ((imp+i)->name));
  343.                         break;
  344.                         }
  345.  
  346. }
  347.  
  348.  
  349.  
  350. /*
  351. end of system part
  352. */
  353.  
  354.  
  355. // ïîñêîëüêó ÿ ïîðòèðîâàë ñ äðåâíåãî äîñà...
  356. void line( int x1, int y1, int x2, int y2)
  357. {
  358.         kos_DrawLine(x1,y1,x2,y2,SysColor,0);
  359. }
  360.  
  361. void outtextxy( int x, int y, char *s, int len)
  362. {
  363.         kos_WriteTextToWindow(x,y,0,SysColor,s,len);
  364. }
  365.  
  366. double textwidth( char *s, int len)
  367. {
  368.         int i;
  369.         for (i = 0; i < len; i++)
  370.                 if (s[i] == 0)
  371.                         break;
  372.         return id(i * 6);
  373. }
  374.  
  375. double textheight( char *s, int len)
  376. {
  377.         return 8.0;
  378. }
  379.  
  380. void setcolor( DWORD color)
  381. {
  382.         SysColor = color;
  383. }
  384.  
  385. void rectangle( int x1, int y1, int x2, int y2)
  386. {
  387.         kos_DrawBar(x1,y1,x2-x1,y2-y1,SysColor);
  388. }
  389.  
  390.  
  391.  
  392.