Subversion Repositories Kolibri OS

Rev

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