utils.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* sd2iec - SD/MMC to Commodore serial bus interface/controller
  2. Copyright (C) 2007-2009 Ingo Korb <ingo@akana.de>
  3. Inspiration and low-level SD/MMC access based on code from MMC2IEC
  4. by Lars Pontoppidan et al., see sdcard.c|h and config.h.
  5. FAT filesystem access based on code from ChaN and Jim Brain, see ff.c|h.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; version 2 of the License only.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. utils.c: Misc. utility functions that didn't fit elsewhere
  17. */
  18. #include <stdint.h>
  19. #include "ustring.h"
  20. uint8_t *appendnumber(uint8_t *msg, uint8_t value) {
  21. if (value >= 100) {
  22. *msg++ = '0' + value/100;
  23. value %= 100;
  24. }
  25. *msg++ = '0' + value/10;
  26. *msg++ = '0' + value%10;
  27. return msg;
  28. }
  29. /* Convert a one-byte BCD value to a normal integer */
  30. uint8_t bcd2int(uint8_t value) {
  31. return (value & 0x0f) + 10*(value >> 4);
  32. }
  33. /* Convert a uint8_t into a BCD value */
  34. uint8_t int2bcd(uint8_t value) {
  35. return (value % 10) + 16*(value/10);
  36. }
  37. /* Similiar to strtok_r, but only a single delimiting character */
  38. uint8_t *ustr1tok(uint8_t *str, const uint8_t delim, uint8_t **saveptr) {
  39. uint8_t *tmp;
  40. if (str == NULL)
  41. str = *saveptr;
  42. /* Skip leading delimiters */
  43. while (*str == delim) str++;
  44. /* If there is anything left... */
  45. if (*str) {
  46. /* Search for the next delimiter */
  47. tmp = str;
  48. while (*tmp && *tmp != delim) tmp++;
  49. /* Terminate the string there */
  50. if (*tmp != 0)
  51. *tmp++ = 0;
  52. *saveptr = tmp;
  53. return str;
  54. } else
  55. return NULL;
  56. }