string.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * stage0 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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include<stdlib.h>
  18. #define MAX_STRING 4096
  19. //CONSTANT MAX_STRING 4096
  20. // void* calloc(int count, int size);
  21. char* copy_string(char* target, char* source)
  22. {
  23. while(0 != source[0])
  24. {
  25. target[0] = source[0];
  26. target = target + 1;
  27. source = source + 1;
  28. }
  29. return target;
  30. }
  31. char* postpend_char(char* s, char a)
  32. {
  33. char* ret = calloc(MAX_STRING, sizeof(char));
  34. char* hold = copy_string(ret, s);
  35. hold[0] = a;
  36. return ret;
  37. }
  38. char* prepend_char(char a, char* s)
  39. {
  40. char* ret = calloc(MAX_STRING, sizeof(char));
  41. ret[0] = a;
  42. copy_string((ret+1), s);
  43. return ret;
  44. }
  45. char* prepend_string(char* add, char* base)
  46. {
  47. char* ret = calloc(MAX_STRING, sizeof(char));
  48. copy_string(copy_string(ret, add), base);
  49. return ret;
  50. }
  51. int string_length(char* a)
  52. {
  53. int i = 0;
  54. while(0 != a[i]) i = i + 1;
  55. return i;
  56. }