Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / mcu / libfitkit /

globfun.c

   1  /*******************************************************************************
   2     globfunc.c:
   3     Copyright (C) 2009 Brno University of Technology,
   4                        Faculty of Information Technology
   5  
   6     LICENSE TERMS
   7  
   8     Redistribution and use in source and binary forms, with or without
   9     modification, are permitted provided that the following conditions
  10     are met:
  11     1. Redistributions of source code must retain the above copyright
  12        notice, this list of conditions and the following disclaimer.
  13     2. Redistributions in binary form must reproduce the above copyright
  14        notice, this list of conditions and the following disclaimer in
  15        the documentation and/or other materials provided with the
  16        distribution.
  17     3. All advertising materials mentioning features or use of this software
  18        or firmware must display the following acknowledgement:
  19  
  20          This product includes software developed by the University of
  21          Technology, Faculty of Information Technology, Brno and its
  22          contributors.
  23  
  24     4. Neither the name of the Company nor the names of its contributors
  25        may be used to endorse or promote products derived from this
  26        software without specific prior written permission.
  27  
  28     This software or firmware is provided ``as is'', and any express or implied
  29     warranties, including, but not limited to, the implied warranties of
  30     merchantability and fitness for a particular purpose are disclaimed.
  31     In no event shall the company or contributors be liable for any
  32     direct, indirect, incidental, special, exemplary, or consequential
  33     damages (including, but not limited to, procurement of substitute
  34     goods or services; loss of use, data, or profits; or business
  35     interruption) however caused and on any theory of liability, whether
  36     in contract, strict liability, or tort (including negligence or
  37     otherwise) arising in any way out of the use of this software, even
  38     if advised of the possibility of such damage.
  39  
  40     $Id$
  41  
  42     Popis:
  43       Definice globalnich funkci.
  44  *******************************************************************************/
  45  
  46  
  47  #include <stdio.h>
  48  #include <ctype.h>
  49  #include <string.h>
  50  #include "arch_specific.h"
  51  #include "define.h"
  52  #include "globfun.h"
  53  
  54  /**
  55  
  56  
  57  
  58  
  59   **/
  60  void chr2bin(unsigned char *buf, unsigned char chr) {
  61    int ii;
  62    for (ii=7; ii >= 0; ii--) {
  63      buf[ii] = ((0x01 & chr) == 0x01)? '1':'0';
  64      chr = (chr >> 1);
  65    }
  66    buf[8] = 0; // ukonceni retezce
  67  }
  68  
  69  /**
  70  
  71  
  72   \return znak
  73   **/
  74  char digit2chr(char chr) {
  75    if ((chr >= 0) && (chr <= 9))
  76      return (chr + '0');
  77  
  78    chr -= 10;
  79    return ('A' + chr);
  80  }
  81  
  82  
  83  /**
  84  
  85  
  86  
  87  
  88   **/
  89  void chr2hex(unsigned char *buf, unsigned char chr) {
  90    buf[0] = digit2chr((chr >> 4) & 0x0F);        // horni 4.bity
  91    buf[1] = digit2chr(chr & 0x0F);               // spodni 4.bity
  92    buf[2] = 0;                                   // ukoncovaci byte
  93  }
  94  
  95  /**
  96  
  97  
  98  
  99   **/
 100  unsigned char chr2digit(unsigned char chr) {
 101    if ((chr >= '0') && (chr <= '9'))
 102      return (chr - '0');
 103    if ((chr >= 'A') && (chr <= 'F'))
 104      return (chr - 'A' + 10);
 105    if ((chr >= 'a') && (chr <= 'f'))
 106      return (chr - 'a' + 10);
 107    return 0;
 108  }
 109  
 110  /**
 111  
 112  
 113  
 114   **/
 115  unsigned char hex2chr(unsigned char *buf) {
 116    unsigned char res = chr2digit(buf[0]);
 117    if (buf[1] != 0) {
 118  	  res = (res << 4) + chr2digit(buf[1]);
 119    }
 120    return res;
 121  }
 122  
 123  /**
 124  
 125  
 126  
 127   **/
 128  unsigned char rchr(unsigned char chr) {
 129    unsigned char ii, reschr = 0x00;
 130    for (ii=0; ii<8; ii++) {
 131      if ((chr & 0x01) == 0x01)
 132        reschr |= (0x80>>ii);
 133      chr = chr>>1;
 134    }
 135    return reschr;
 136  }
 137  
 138  
 139  /**
 140  
 141  
 142  
 143   **/
 144  long str2long(char *buf) {
 145    int ii, len;
 146    long res = 0;
 147  
 148    // odstraneni mezer na zacatku
 149    for (ii=0; buf[ii] == ' '; ii++) {
 150    }
 151    buf += ii;
 152  
 153    // delka cislic
 154    for (len=0; isdigit(buf[len]); len++) {
 155    }
 156  
 157    // prevod stringu na cislo
 158    for (ii=0; ii<len; ii++) {
 159      res += chr2digit(buf[ii]);
 160      if (ii != len-1)
 161        res*=10;
 162    }
 163  
 164    return res;
 165  }
 166  
 167  
 168  /**
 169  
 170  
 171  
 172  
 173  
 174   **/
 175  long str2long_2x(char *buf, long *n1, long *n2) {
 176    int ii, len, c1, c2;
 177    c1 = 0;
 178    c2 = 0;
 179    // odstraneni mezer na zacatku
 180    for (ii=0; buf[ii] == ' '; ii++) {
 181    }
 182    buf += ii;
 183  
 184    // delka cislic
 185    for (len=0; isdigit(buf[len]); len++) {
 186    }
 187  
 188    // prevod stringu na cislo1
 189    for (ii=0; ii<len; ii++) {
 190      c1 += chr2digit(buf[ii]);
 191      if (ii != len-1) c1 *=10;
 192    }
 193    // preskoceni 1. cisla
 194    buf += len;
 195  
 196    // odstraneni mezer mezi cisly
 197    for (ii=0; buf[ii] == ' '; ii++) {
 198    }
 199    buf += ii;
 200  
 201    // delka cislic
 202    for (len=0; isdigit(buf[len]); len++){
 203    }
 204  
 205    // prevod stringu na cislo1
 206    for (ii=0; ii<len; ii++)
 207    {
 208       c2 += chr2digit(buf[ii]);
 209       if (ii != len-1) c2 *=10;
 210    }
 211  
 212    *n1 = c1;
 213    *n2 = c2;
 214  
 215    return c1;
 216  }
 217  
 218  
 219  /**
 220  
 221  
 222  
 223   **/
 224  char *strrev(char *buf) {
 225    int ii, jj = strlen(buf);
 226    char chr;
 227    for (ii=0; ii < (strlen(buf)>>1); ii++) {
 228      jj--;
 229      chr = buf[ii];
 230      buf[ii] = buf[jj];
 231      buf[jj] = chr;
 232    }
 233    return buf;
 234  }
 235  
 236  /**
 237  
 238   \param dest - vystupni buffer
 239   \param buf - vstupni string
 240   **/
 241  void strupper(unsigned char *dest, unsigned char *buf)
 242  {
 243   int i;
 244   dest[0] = 0;
 245   for (i=0;;i++) {
 246      dest[i] = ((buf[i] > 96) && (buf[i] < 123)) ? buf[i] - 32 : buf[i];
 247      if (buf[i] == 0) return;
 248   }
 249  }
 250  
 251  /**
 252  
 253  
 254  
 255  
 256   **/
 257  void long2str(long input, char *buf, long radix) {
 258    int      ii  = 0;
 259    unsigned char rest;
 260    char     isMinus = 0;
 261  
 262    if (radix == 16)
 263      buf[ii++] = 'h';
 264  
 265    if (input == 0) {
 266       buf[ii++] = '0';
 267    }
 268    else if (input < 0) {
 269      input *= -1;
 270      isMinus = 1;
 271    }
 272  
 273    // prevod cisla na string
 274    for (; input!=0; ii++) {
 275      rest = input % radix;
 276      buf[ii] = digit2chr(rest);
 277      input -= rest;
 278      input /= radix;
 279    }
 280  
 281    // pokud zaporne cislo, tak vlozim minus
 282    if (isMinus)
 283      buf[ii++] = '-';
 284  
 285    // ukoncim retezec
 286    buf[ii] = 0;
 287    buf = strrev(buf);
 288  }
 289  
Zobrazeno: 731748x Naposledy: 29.11.2022 05:24:47