Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "dstring.h"
- #include <string.h>
- #include <stdlib.h>
- #include <assert.h>
- DString dstring_initialize(const char* str)
- {
- assert(str != NULL);
- char* newString;
- newString = malloc(sizeof(char) * strlen(str));
- if (!newString)
- {
- printf("Can't allocate memory, exiting");
- return NULL;
- }
- strcpy(newString, str);
- return newString;
- // Precondition: str ar ej NULL - testas med en assert
- // Postcondition: returvardet innehaller samma strang som 'str' - behšver inte testas med assert
- }
- int dstring_concatenate(DString* destination, DString source)
- {
- assert(destination != NULL);
- assert(*destination != NULL);
- assert(source != NULL);
- int combinedLength = strlen(*destination) + strlen(source);
- DString temp;
- **destination = (DString*)realloc(*destination, sizeof(char) * combinedLength);
- if (!*destination)
- {
- printf("Can't allocate memory, exiting");
- return 0;
- }
- strcat(*destination, source);
- //printf("after concat length: %d\n", strlen(*destination));
- /*for (int i = 0; i < strlen(*destination); i++)
- {
- printf("index %d\n", i + 1);
- }*/
- return 1;
- }
- void dstring_truncate(DString* destination, unsigned int truncatedLength)
- {
- // Precondition: destination Šr ej NULL, *destination ar ej NULL
- // lŠngden (truncateLength) fŒr inte vara negativ
- // Preconditions testas med asserts
- /* Tips:
- - Anvand realloc for att frigora det overflodiga minnet
- (realloc frigor forst minnet och allokerar sedan nytt for den angivna storleken)
- - glom inte nolltermineringen
- Vad hŠnder om truncateLength Šr lŠngre Šn vad strŠngen Šr?*/
- // Postcondition: *destination ar inte langre an 'truncatedLength' tecken - behšver inte testas med assert
- }
- void dstring_print(DString str, FILE* textfile)
- {
- // Precondition: textfile ar inte NULL - maste testas innan du forsoker skriva till filen
- /*Tank pŒ att filpekaren kopplas till filen innan funktionen anropas*/
- /* filpekaren far inte stangas, filen ska fungera som vanligt efter anropet */
- }
- void dstring_delete(DString* stringToDelete)
- {
- // Precondition: stringToDelete ar inte NULL
- // Postcondition: *stringToDelete ar NULL och minnet ar frigjort - behšver inte testas med assert
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement