Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "dstring.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6.  
  7.  
  8. DString dstring_initialize(const char* str)
  9. {
  10. assert(str != NULL);
  11. char* newString;
  12.  
  13. newString = malloc(sizeof(char) * strlen(str));
  14. if (!newString)
  15. {
  16. printf("Can't allocate memory, exiting");
  17. return NULL;
  18. }
  19. strcpy(newString, str);
  20.  
  21.  
  22. return newString;
  23. // Precondition: str ar ej NULL - testas med en assert
  24. // Postcondition: returvardet innehaller samma strang som 'str' - behšver inte testas med assert
  25. }
  26.  
  27. int dstring_concatenate(DString* destination, DString source)
  28. {
  29. assert(destination != NULL);
  30. assert(*destination != NULL);
  31. assert(source != NULL);
  32.  
  33. int combinedLength = strlen(*destination) + strlen(source);
  34.  
  35. DString temp;
  36.  
  37. **destination = (DString*)realloc(*destination, sizeof(char) * combinedLength);
  38.  
  39. if (!*destination)
  40. {
  41. printf("Can't allocate memory, exiting");
  42. return 0;
  43. }
  44. strcat(*destination, source);
  45.  
  46. //printf("after concat length: %d\n", strlen(*destination));
  47.  
  48. /*for (int i = 0; i < strlen(*destination); i++)
  49. {
  50. printf("index %d\n", i + 1);
  51. }*/
  52.  
  53.  
  54. return 1;
  55. }
  56.  
  57. void dstring_truncate(DString* destination, unsigned int truncatedLength)
  58. {
  59. // Precondition: destination Šr ej NULL, *destination ar ej NULL
  60. // lŠngden (truncateLength) fŒr inte vara negativ
  61. // Preconditions testas med asserts
  62.  
  63. /* Tips:
  64. - Anvand realloc for att frigora det overflodiga minnet
  65. (realloc frigor forst minnet och allokerar sedan nytt for den angivna storleken)
  66. - glom inte nolltermineringen
  67.  
  68. Vad hŠnder om truncateLength Šr lŠngre Šn vad strŠngen Šr?*/
  69.  
  70. // Postcondition: *destination ar inte langre an 'truncatedLength' tecken - behšver inte testas med assert
  71. }
  72.  
  73. void dstring_print(DString str, FILE* textfile)
  74. {
  75. // Precondition: textfile ar inte NULL - maste testas innan du forsoker skriva till filen
  76. /*Tank pΠatt filpekaren kopplas till filen innan funktionen anropas*/
  77.  
  78. /* filpekaren far inte stangas, filen ska fungera som vanligt efter anropet */
  79. }
  80.  
  81. void dstring_delete(DString* stringToDelete)
  82. {
  83. // Precondition: stringToDelete ar inte NULL
  84.  
  85. // Postcondition: *stringToDelete ar NULL och minnet ar frigjort - behšver inte testas med assert
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement