Guest User

Untitled

a guest
Jan 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. typedef struct BufferDescriptor {
  2. char * ca_head ;
  3. int capacity ;
  4. char inc_factor;
  5. int addc_offset ;
  6. int mark_offset ;
  7. char r_flag;
  8. char mode;
  9. } Buffer ;
  10.  
  11. int b_reset ( Buffer *pB )
  12. {
  13. Buffer *temp = NULL;
  14. int i = 0;
  15. int j = 1;
  16.  
  17. if (pB == NULL)
  18. {
  19. return R_FAIL_1;
  20. }
  21. else
  22. {
  23. temp = (Buffer*)malloc(sizeof(Buffer*));
  24. if (temp == NULL)
  25. {
  26. return R_FAIL_1;
  27. }
  28. temp->ca_head = (char*)malloc(pB->capacity);
  29. if (!temp->ca_head)
  30. {
  31. temp = NULL;
  32. return R_FAIL_1;
  33. }
  34.  
  35. for(i = 0;i < ca_getsize(pB);++i)
  36. {
  37. temp->ca_head[j] = pB->ca_head[i];
  38. j++;
  39. }
  40.  
  41. pB->ca_head = temp->ca_head;
  42.  
  43. //free(temp->ca_head);
  44. //free(temp);
  45.  
  46. return 0;
  47. }
  48. }
  49.  
  50. temp->ca_head[0] = 'a';
  51. temp->ca_head[1] = 'b';
  52. temp->ca_head[2] = 'c';
  53. temp->ca_head[3] = 'd';
  54. temp->ca_head[4] = 'e';
  55.  
  56. b_reset(temp); //this will return the size as 0, when it's actually 5
  57.  
  58. //temp->ca_head[0] = 'i'; //if this is executed, it returns the size as 6
  59. //and prints out the right values, but if it's not,
  60. //it will not print out anything
  61.  
  62. printf("%d", ca_getsize(temp));
  63. for(i = 0;i < ca_getsize(temp);++i)
  64. {
  65. printf("%c", temp->ca_head[i]);
  66. }
  67.  
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #include <assert.h>
  72.  
  73. #define R_FAIL_1 1
  74.  
  75. #define BUFFER_SIZE 10
  76.  
  77. typedef struct BufferDescriptor {
  78. char * ca_head ;
  79. int capacity ;
  80. char inc_factor;
  81. int addc_offset ;
  82. int mark_offset ;
  83. char r_flag;
  84. char mode;
  85. } Buffer ;
  86.  
  87. void allocate_buffer(Buffer *pB, int size)
  88. {
  89. pB->ca_head = malloc(size);
  90. assert(pB->ca_head);
  91. pB->capacity = size;
  92. }
  93.  
  94. int ca_getsize( Buffer *pB)
  95. {
  96. return pB->capacity;
  97. }
  98.  
  99.  
  100. int b_reset ( Buffer *pB )
  101. {
  102. int i = 0;
  103.  
  104. if (pB == NULL)
  105. {
  106. return R_FAIL_1;
  107. }
  108. else
  109. {
  110. if ( ca_getsize(pB) <= 0 || pB->ca_head == NULL )
  111. return R_FAIL_1;
  112. }
  113. // shift data up by 1 byte
  114. for( i = ca_getsize(pB) - 1 ; i > 0;i-- )
  115. {
  116. pB->ca_head[i] = pB->ca_head[i-1];
  117. }
  118. pB->ca_head[0] = '';
  119. return 0;
  120. }
  121.  
  122. void print_buffer(Buffer *pB)
  123. {
  124. printf("capacity: %d n", ca_getsize(pB));
  125. for (int i = 0;i < ca_getsize(pB);++i)
  126. {
  127. printf("buffer(%d): [%d] ",i, pB->ca_head[i]);
  128. }
  129. printf("n");
  130. }
  131.  
  132.  
  133. int main(void)
  134. {
  135. Buffer a_buffer;
  136. allocate_buffer(&a_buffer,BUFFER_SIZE);
  137. strcpy(a_buffer.ca_head,"abcdefgh");
  138. print_buffer(&a_buffer);
  139. int ret = b_reset(&a_buffer);
  140. assert(ret == 0);
  141. print_buffer(&a_buffer);
  142. }
  143.  
  144. temp = (Buffer*)malloc(sizeof(Buffer*));
  145.  
  146. temp = (Buffer*)malloc(sizeof(Buffer));
Add Comment
Please, Sign In to add comment