Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. typedef struct {
  2. char *historyCommand;
  3. int usedSize;
  4. int maximumSize;
  5. } HistoryArray;
  6.  
  7. void CreateHistoryArray(HistoryArray *HistoryArray) {
  8. HistoryArray->historyCommand = (char *) malloc(sizeof(char) * MAX_LEN);
  9. HistoryArray->usedSize = 0;
  10. HistoryArray->maximumSize = INITIAL_SIZE;
  11. }
  12.  
  13. void ExpandHistoryArray(HistoryArray *HistoryArray, int newSize) {
  14. int *newArray = (char *) malloc(sizeof(char) * newSize);
  15. memcpy(newArray, HistoryArray->historyCommand, sizeof(char) * HistoryArray->maximumSize);
  16. free(HistoryArray->historyCommand);
  17. HistoryArray->historyCommand = newArray;
  18. HistoryArray->maximumSize = newSize;
  19. }
  20.  
  21. void AddHistoryValue(HistoryArray *HistoryArray, char historyCommand[]) {
  22. strcpy(HistoryArray->historyCommand[HistoryArray->usedSize], historyCommand);
  23.  
  24. HistoryArray->usedSize++;
  25.  
  26. if (HistoryArray->usedSize == HistoryArray->maximumSize) {
  27. ExpandHistoryArray(HistoryArray, HistoryArray->maximumSize * 2);
  28. }
  29. }
  30.  
  31. void freeHistoryArray(HistoryArray *a) {
  32. free(a->historyCommand);
  33. a->historyCommand = NULL;
  34. a->usedSize = 0;
  35. a->maximumSize = 2;
  36. }
  37.  
  38.  
  39. HistoryArray historyArray;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement