upse_string.c 937 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * UPSE: the unix playstation sound emulator.
  3. *
  4. * Filename: upse_string.c
  5. * Purpose: libupse: string utility functions
  6. *
  7. * Copyright (c) 2007 William Pitcock <nenolod@sacredspiral.co.uk>
  8. *
  9. * UPSE is free software, released under the GNU General Public License,
  10. * version 2.
  11. *
  12. * A copy of the GNU General Public License, version 2, is included in
  13. * the UPSE source kit as COPYING.
  14. *
  15. * UPSE is offered without any warranty of any kind, explicit or implicit.
  16. */
  17. #include "upse.h"
  18. #include "upse-string.h"
  19. /*
  20. * fgets() wrapper - inefficient, but it will do.
  21. */
  22. char *upse_io_fgets(char *s, int n, void *stream, upse_iofuncs_t * iofuncs)
  23. {
  24. int c;
  25. register char *p;
  26. if (n <= 0)
  27. return NULL;
  28. p = s;
  29. while (--n)
  30. {
  31. if (iofuncs->read_impl(&c, 1, 1, stream) == 0)
  32. {
  33. break;
  34. }
  35. if ((*p++ = c) == '\n')
  36. {
  37. break;
  38. }
  39. }
  40. if (p > s)
  41. {
  42. *p = 0;
  43. return s;
  44. }
  45. return NULL;
  46. }