Subversion Repositories Kolibri OS

Rev

Rev 7503 | 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 convert_error = 0;
  6. int SysColor = 0;
  7. char debuf[50] = "";
  8.  
  9.  
  10. // ïî÷åìó-òî íå áûëî â ñòàíäàðòíîé áèáëèîòåêå
  11. void kos_DrawLine( Word x1, Word y1, Word x2, Word y2, Dword colour, Dword invert )
  12. {
  13.         Dword arg1, arg2, arg3;
  14.  
  15.         //
  16.         arg1 = ( x1 << 16 ) | x2;
  17.         arg2 = ( y1 << 16 ) | y2;
  18.         arg3 = (invert)?0x01000000:colour;
  19.         //
  20.         __asm{
  21.                 mov eax, 38
  22.                 mov ebx, arg1
  23.                 mov ecx, arg2
  24.                 mov edx, arg3
  25.                 int 0x40
  26.         }
  27. }
  28.  
  29. // ïîõèùåíî èç áèáëèîòåêè ê C--
  30. void DrawRegion(Dword x,Dword y,Dword width,Dword height,Dword color1)
  31. {
  32.         kos_DrawBar(x,y,width,1,color1); //ïîëîñà ãîð ñâåðõó
  33.         kos_DrawBar(x,y+height,width,1,color1); //ïîëîñà ãîð ñíèçó
  34.         kos_DrawBar(x,y,1,height,color1); //ïîëîñà âåðò ñëåâà
  35.         kos_DrawBar(x+width,y,1,height+1,color1); //ïîëîñà âåðò ñïðàâà
  36. }
  37.  
  38.  
  39. // äà, ýòî áàÿí
  40. int atoi(const char* string)
  41. {
  42.         int res=0;
  43.         int sign=0;
  44.         const char* ptr;
  45.         for (ptr=string; *ptr && *ptr<=' ';ptr++);
  46.         if (*ptr=='-') {sign=1;++ptr;}
  47.         while (*ptr >= '0' && *ptr <= '9')
  48.         {
  49.                 res = res*10 + *ptr++ - '0';
  50.         }
  51.         if (sign) res = -res;
  52.         return res;
  53. }
  54.  
  55. /*int abs(int n)
  56. {
  57.         return (n<0)?-n:n;
  58. }*/
  59.  
  60.  
  61.  
  62.  
  63.  
  64. double fabs(double x)
  65. {
  66.         __asm   fld     x
  67.         __asm   fabs
  68. }
  69. #define M_PI       3.14159265358979323846
  70. double cos(double x)
  71. {
  72.         __asm   fld     x
  73.         __asm   fcos
  74. }
  75. double sin(double x)
  76. {
  77.         __asm   fld     x
  78.         __asm   fsin
  79. }
  80.  
  81. bool isalpha(char c)
  82. {
  83.         return (c==' ' || c=='\n' || c=='\t' || c=='\r');
  84. }
  85.  
  86. // ýòà ôóíêöèÿ - âåëîñèïåä. íî ïðîùå áûëî íàïèñàòü ÷åì íàéòè.
  87. double convert(char *s, int *len)
  88. {
  89.  
  90.         int i;
  91.  
  92.  
  93.         double sign,res, tail, div;
  94.  
  95.         convert_error = 0;
  96.  
  97.         res = 0.0;
  98.  
  99.         i=0;
  100.         while (s[i] && isalpha(s[i])) i++;
  101.         if (len) *len=i;
  102.         if (s[i] == '\0')
  103.         {
  104.                 convert_error = ERROR_END;
  105.                 return 0.0;
  106.         }
  107.  
  108.         sign=1.0;
  109.         if (s[i] == '-')
  110.         {
  111.                 sign=-1.0;
  112.                 i++;
  113.         }
  114.         while (s[i] && s[i] >= '0' && s[i] <= '9')
  115.         {
  116.                 res *= 10.0;
  117.                 res += id(s[i] - '0');
  118.                 i++;
  119.         }
  120.         if (len) *len=i;
  121.         if (!s[i] || isalpha(s[i]))
  122.                 return sign*res;
  123.         if (s[i] != '.' && s[i] != ',')
  124.         {
  125.                 convert_error = ERROR;
  126.                 return 0;
  127.         }
  128.         i++;
  129.         if (len) *len=i;
  130.         if (!s[i])
  131.                 return sign*res;
  132.        
  133.         div = 1.0;
  134.         tail = 0.0;
  135.         while (s[i] && s[i] >= '0' && s[i] <= '9')
  136.         {
  137.                 tail *= 10.0;
  138.                 tail += id(s[i] - '0');
  139.                 div *= 10.0;
  140.                 i++;           
  141.         }
  142.         res += tail/div;
  143.         if (len) *len=i;
  144.         return sign*res;
  145. }
  146.  
  147. /*
  148. #define PREC 2
  149.  
  150. double double_tab[]={1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15};
  151.  
  152. // ýòî sprintf, óìåþùèé ôîðìàòèðîâàòü _òîëüêî_ âåùåñòâåííûå ÷èñëà (double) %f
  153. void format( char *Str, int len, char* Format, ... )
  154. {
  155.         int i, fmtlinesize, j, k, flag;
  156.         char c;
  157.         va_list arglist;
  158.         //
  159.         va_start(arglist, Format);
  160.  
  161.         //
  162.         fmtlinesize = strlen( Format );
  163.         //
  164.         if( fmtlinesize == 0 ) return;
  165.  
  166.         for (i = 0; i < len; i++)
  167.                 Str[i] = 0;
  168.  
  169.         //
  170.         for( i = 0, j = 0; i < fmtlinesize; i++ )
  171.         {
  172.                 //
  173.                 c = Format[i];
  174.                 //
  175.                 if( c != '%' )
  176.                 {
  177.                         Str[j++] = c;
  178.                         continue;
  179.                 }
  180.                 //
  181.                 i++;
  182.                 //
  183.                 if( i >= fmtlinesize ) break;
  184.  
  185.                 //
  186.                 flag = 0;
  187.                 //
  188.                 c = Format[i];
  189.                 //
  190.                 switch( c )
  191.                 {
  192.                 //
  193.                 case '%':
  194.                         Str[j++] = c;
  195.                         break;
  196.                 // auaia aauanoaaiiiai ?enea
  197.                 case 'f':
  198.                         // ii?aaaeeou ?enei oeo? ai oi?ee
  199.                         double val, w;
  200.                         int p;
  201.                         val = va_arg(arglist, double);
  202.                         if (val < 0.0)
  203.                         {
  204.                                 Str[j++] = '-';
  205.                                 val = -val;
  206.                         }
  207.                         for (k = 0; k < 15; k++)
  208.                                 if (val < double_tab[k])
  209.                                         break;
  210.  
  211.                         if (val < 1.0)
  212.                         {
  213.                                 Str[j++] = '0';
  214.                         }
  215.                        
  216.                         for (p = 1; p < k + 1; p++)
  217.                         {
  218.                                 Str[j++] = '0' + di(val / double_tab[k - p] - 0.499) % 10;
  219.                         }
  220.                         Str[j++] = '.';
  221.                         w = 0.1;
  222.                         for (p = 0; p < 2; p++)
  223.                         {
  224.                                 val-=floor(val);
  225.                                 Str[j++] = '0' + di(val / w - 0.499) % 10;
  226.                                 w /= 10.0;
  227.                         }
  228.  
  229.                 //
  230.                 default:
  231.                         break;
  232.                 }
  233.         }
  234.         //
  235.         Str[j] = 0;
  236. }
  237.  
  238. void *memcpy(void *dst, const void *src, unsigned size)
  239. {
  240.         while (size--)
  241.                 *((char*)dst+size) = *((char*)src+size);
  242.         return dst;
  243. }
  244. */
  245. int strcmp(const char *s1, const char *s2)
  246. {
  247.         int i;
  248.  
  249.         if (s1 == NULL)
  250.                 if (s2 == NULL)
  251.                         return 0;
  252.                 else
  253.                         return 1;
  254.         else
  255.                 if (s2 == NULL)
  256.                         return 1;
  257.  
  258.         for (i = 0;;i++)
  259.         {
  260.                 if (s1[i] == '\0')
  261.                         if (s2[i] == '\0')
  262.                                 return 0;
  263.                         else
  264.                                 return 1;
  265.                 else
  266.                         if (s2[i] == '\0')
  267.                                 return 1;
  268.                         else
  269.                         {
  270.                                 if (s1[i] != s2[i])
  271.                                         return 1;
  272.                         }
  273.         }
  274.         return 0;
  275. }
  276.  
  277. kol_struct_import* kol_cofflib_load(char *name)
  278. {
  279. //asm ("int $0x40"::"a"(68), "b"(19), "c"(name));
  280.         __asm
  281.         {
  282.                 mov eax, 68
  283.                 mov ebx, 19
  284.                 mov ecx, name
  285.                 int 0x40
  286.         }
  287. }
  288.  
  289.  
  290. void* kol_cofflib_procload (kol_struct_import *imp, char *name)
  291. {
  292.        
  293. int i;
  294. for (i=0;;i++)
  295.         if ( NULL == ((imp+i) -> name))
  296.                 break;
  297.         else
  298.                 if ( 0 == strcmp(name, (imp+i)->name) )
  299.                         return (imp+i)->data;
  300. return NULL;
  301.  
  302. }
  303.  
  304.  
  305. unsigned kol_cofflib_procnum (kol_struct_import *imp)
  306. {
  307.        
  308. unsigned i, n;
  309.  
  310. for (i=n=0;;i++)
  311.         if ( NULL == ((imp+i) -> name))
  312.                 break;
  313.         else
  314.                 n++;
  315.  
  316. return n;
  317.  
  318. }
  319.  
  320.  
  321. void kol_cofflib_procname (kol_struct_import *imp, char *name, unsigned n)
  322. {
  323.        
  324. unsigned i;
  325. *name = 0;
  326.  
  327. for (i=0;;i++)
  328.         if ( NULL == ((imp+i) -> name))
  329.                 break;
  330.         else
  331.                 if ( i == n )
  332.                         {
  333.                         strcpy(name, ((imp+i)->name));
  334.                         break;
  335.                         }
  336.  
  337. }
  338.  
  339.  
  340.  
  341. /*
  342. end of system part
  343. */
  344.  
  345.  
  346. // ïîñêîëüêó ÿ ïîðòèðîâàë ñ äðåâíåãî äîñà...
  347. void line( int x1, int y1, int x2, int y2)
  348. {
  349.         kos_DrawLine(x1,y1,x2,y2,SysColor,0);
  350. }
  351.  
  352. void outtextxy( int x, int y, char *s, int len)
  353. {
  354.         kos_WriteTextToWindow(x,y,0,SysColor,s,len);
  355. }
  356.  
  357. double textwidth( char *s, int len)
  358. {
  359.         int i;
  360.         for (i = 0; i < len; i++)
  361.                 if (s[i] == 0)
  362.                         break;
  363.         return id(i * 6);
  364. }
  365.  
  366. double textheight( char *s, int len)
  367. {
  368.         return 8.0;
  369. }
  370.  
  371. void setcolor( DWORD color)
  372. {
  373.         SysColor = color;
  374. }
  375.  
  376. void rectangle( int x1, int y1, int x2, int y2)
  377. {
  378.         kos_DrawBar(x1,y1,x2-x1,y2-y1,SysColor);
  379. }
  380.  
  381.  
  382.  
  383. Dword kos_GetSkinHeight()
  384. {
  385.         __asm{
  386.                 mov             eax, 48
  387.                 mov             ebx, 4
  388.                 int             0x40
  389.         }
  390. }
  391.  
  392. Dword kos_GetSpecialKeyState()
  393. {
  394.         __asm{
  395.                 mov             eax, 66
  396.                 mov             ebx, 3
  397.                 int             0x40
  398.         }
  399. }
  400.  
  401.  
  402.  
  403. Dword kos_GetSlotByPID(Dword PID)
  404. {
  405.         __asm
  406.         {
  407.                 push ebx
  408.                 push ecx
  409.                 mov eax, 18
  410.                 mov ebx, 21
  411.                 mov ecx, PID
  412.                 int     0x40
  413.                 pop ecx
  414.                 pop ebx
  415.         }
  416. }
  417.  
  418.  
  419. Dword kos_GetActiveSlot()
  420. {
  421.         __asm
  422.         {
  423.                 push ebx
  424.                 mov eax, 18
  425.                 mov ebx, 7
  426.                 int     0x40
  427.                 pop ebx
  428.         }
  429. }
  430.  
  431.  
  432.  
  433. void kos_GetScrollInfo(int &vert, int &hor)
  434. {
  435.         short v, h;
  436.         __asm
  437.         {
  438.                 mov eax, 37
  439.                 mov ebx, 7
  440.                 int     0x40
  441.                 mov ebx, eax
  442.                 and eax, 0xffff
  443.                 mov v, ax
  444.                 shr ebx, 16
  445.                 mov h, bx
  446.         }
  447.         vert = v;
  448.         hor = h;
  449. }
  450.  
  451.  
  452. // ïîëó÷åíèå èíôîðìàöèè î ñîñòîÿíèè "ìûøè" ôóíêöèÿ 37/1
  453. void kos_GetMouseStateWnd( Dword & buttons, int & cursorX, int & cursorY )
  454. {
  455.         Dword mB;
  456.         Word curX;
  457.         Word curY;
  458.         sProcessInfo sPI;
  459.  
  460.         //
  461.         __asm{
  462.                 mov             eax, 37
  463.                 mov             ebx, 1
  464.                 int             0x40
  465.                 mov             curY, ax
  466.                 shr             eax, 16
  467.                 mov             curX, ax
  468.                 mov             eax, 37
  469.                 mov             ebx, 2
  470.                 int             0x40
  471.                 mov             mB, eax
  472.         }
  473.         //
  474.         kos_ProcessInfo( &sPI );
  475.         //
  476.         buttons = mB;
  477.         cursorX = curX - sPI.processInfo.x_start;
  478.         cursorY = curY - sPI.processInfo.y_start;
  479. }
  480.  
  481. char *ftoa(double d)
  482. {
  483.         char buffer[256], *p;
  484.         sprintf(buffer, "%f", d);
  485.         p = (char*)allocmem(strlen(buffer)+1);
  486.         strcpy(p, buffer);
  487.         return p;
  488. }
  489.  
  490. double atof(char *s)
  491. {
  492.         return convert(s, NULL);
  493. }
  494.  
  495.  
  496. int di(double x)
  497. {
  498.         int a;
  499.         __asm fld x
  500.         __asm fistp a
  501.         return a;
  502. }
  503.  
  504. double id(int x)
  505. {
  506.         double a;
  507.         __asm fild x
  508.         __asm fstp a
  509.         return a;
  510. }
  511.