utils.c 2.3 KB

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