Subversion Repositories Kolibri OS

Rev

Rev 1764 | 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 fabs(double x)
  63. {
  64.         __asm   fld     x
  65.         __asm   fabs
  66. }
  67. #define M_PI       3.14159265358979323846
  68. double cos(double x)
  69. {
  70.         __asm   fld     x
  71.         __asm   fcos
  72. }
  73. double 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. void *memcpy(void *dst, const void *src, unsigned size)
  246. {
  247.         while (size--)
  248.                 *((char*)dst+size) = *((char*)src+size);
  249.         return dst;
  250. }
  251.  
  252. int strcmp(const char *s1, const char *s2)
  253. {
  254.         int i;
  255.  
  256.         if (s1 == NULL)
  257.                 if (s2 == NULL)
  258.                         return 0;
  259.                 else
  260.                         return 1;
  261.         else
  262.                 if (s2 == NULL)
  263.                         return 1;
  264.  
  265.         for (i = 0;;i++)
  266.         {
  267.                 if (s1[i] == '\0')
  268.                         if (s2[i] == '\0')
  269.                                 return 0;
  270.                         else
  271.                                 return 1;
  272.                 else
  273.                         if (s2[i] == '\0')
  274.                                 return 1;
  275.                         else
  276.                         {
  277.                                 if (s1[i] != s2[i])
  278.                                         return 1;
  279.                         }
  280.         }
  281.         return 0;
  282. }
  283.  
  284. kol_struct_import* kol_cofflib_load(char *name)
  285. {
  286. //asm ("int $0x40"::"a"(68), "b"(19), "c"(name));
  287.         __asm
  288.         {
  289.                 mov eax, 68
  290.                 mov ebx, 19
  291.                 mov ecx, name
  292.                 int 0x40
  293.         }
  294. }
  295.  
  296.  
  297. void* kol_cofflib_procload (kol_struct_import *imp, char *name)
  298. {
  299.        
  300. int i;
  301. for (i=0;;i++)
  302.         if ( NULL == ((imp+i) -> name))
  303.                 break;
  304.         else
  305.                 if ( 0 == strcmp(name, (imp+i)->name) )
  306.                         return (imp+i)->data;
  307. return NULL;
  308.  
  309. }
  310.  
  311.  
  312. unsigned kol_cofflib_procnum (kol_struct_import *imp)
  313. {
  314.        
  315. unsigned i, n;
  316.  
  317. for (i=n=0;;i++)
  318.         if ( NULL == ((imp+i) -> name))
  319.                 break;
  320.         else
  321.                 n++;
  322.  
  323. return n;
  324.  
  325. }
  326.  
  327.  
  328. void kol_cofflib_procname (kol_struct_import *imp, char *name, unsigned n)
  329. {
  330.        
  331. unsigned i;
  332. *name = 0;
  333.  
  334. for (i=0;;i++)
  335.         if ( NULL == ((imp+i) -> name))
  336.                 break;
  337.         else
  338.                 if ( i == n )
  339.                         {
  340.                         strcpy(name, ((imp+i)->name));
  341.                         break;
  342.                         }
  343.  
  344. }
  345.  
  346.  
  347.  
  348. /*
  349. end of system part
  350. */
  351.  
  352.  
  353. // ïîñêîëüêó ÿ ïîðòèðîâàë ñ äðåâíåãî äîñà...
  354. void line( int x1, int y1, int x2, int y2)
  355. {
  356.         kos_DrawLine(x1,y1,x2,y2,SysColor,0);
  357. }
  358.  
  359. void outtextxy( int x, int y, char *s, int len)
  360. {
  361.         kos_WriteTextToWindow(x,y,0,SysColor,s,len);
  362. }
  363.  
  364. double textwidth( char *s, int len)
  365. {
  366.         int i;
  367.         for (i = 0; i < len; i++)
  368.                 if (s[i] == 0)
  369.                         break;
  370.         return id(i * 6);
  371. }
  372.  
  373. double textheight( char *s, int len)
  374. {
  375.         return 8.0;
  376. }
  377.  
  378. void setcolor( DWORD color)
  379. {
  380.         SysColor = color;
  381. }
  382.  
  383. void rectangle( int x1, int y1, int x2, int y2)
  384. {
  385.         kos_DrawBar(x1,y1,x2-x1,y2-y1,SysColor);
  386. }
  387.  
  388.  
  389.  
  390.