fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5.  
  6. #define DEFAULT_BREAKPOINT -1
  7. #define DEFAULT_STARTPOINT -1
  8.  
  9. class stringclass
  10. {
  11. protected :
  12.  
  13. inline bool success() { failbit = false; return true; }
  14. inline bool fail() { failbit = true; return false; }
  15.  
  16. public :
  17.  
  18. bool failbit;
  19.  
  20. long memsize;
  21. long length;
  22. char * mystring;
  23.  
  24. bool ins(const char * str, long startpoint, long breakpoint);
  25.  
  26.  
  27. inline long get_length() { if(mystring) length = strlen(mystring); return length;}
  28. inline long get_memsize() const { return memsize; }
  29.  
  30. void reset();
  31. void alloc(long newsize);
  32.  
  33. void copy(const stringclass & other);
  34. stringclass();
  35. stringclass(const char str[]);
  36. stringclass(const stringclass & other);
  37. ~stringclass();
  38.  
  39. friend ostream& operator << (ostream& out, stringclass & sc){out << sc.mystring; return out;}
  40.  
  41. };
  42.  
  43. void stringclass::copy(const stringclass & other)
  44. {
  45. if(other.mystring == NULL)
  46. {
  47. reset();
  48. return;
  49. }
  50.  
  51. alloc(other.memsize);
  52. strcpy(mystring, other.mystring);
  53. length = other.length;
  54. }
  55.  
  56. stringclass::stringclass()
  57. : mystring(NULL), memsize(0), length(0)
  58. {
  59. }
  60.  
  61. stringclass::stringclass(const char str[])
  62. : mystring(NULL), memsize(0), length(0)
  63. {
  64. if(str != NULL)
  65. {
  66. alloc(strlen(str) + 1);
  67. strcpy(mystring, str);
  68. length = strlen(mystring);
  69. }
  70. }
  71.  
  72. stringclass::stringclass(const stringclass & other)
  73. : mystring(NULL), memsize(0), length(0)
  74. {
  75. copy(other);
  76. }
  77.  
  78. stringclass::~stringclass()
  79. {
  80. delete [] mystring;
  81. }
  82.  
  83. void stringclass::reset()
  84. {
  85. if(mystring) delete [] mystring;
  86. mystring = NULL;
  87. length = 0;
  88. memsize = 0;
  89. }
  90.  
  91. void stringclass::alloc(long newsize)
  92. {
  93. if(memsize < newsize)
  94. {
  95. cout << "checkpoint 1\n";
  96. if(mystring) delete [] mystring;
  97. cout << "checkpoint 2\n";
  98. memsize = newsize;
  99. mystring = new char[memsize];
  100. mystring[0] = 0;
  101. length = 0;
  102. }
  103. }
  104.  
  105. bool stringclass::ins(const char * str, long startpoint = DEFAULT_STARTPOINT, long breakpoint = DEFAULT_BREAKPOINT)
  106. {
  107. if(startpoint == DEFAULT_STARTPOINT) startpoint = 0;
  108. if(breakpoint == DEFAULT_BREAKPOINT) breakpoint = startpoint;
  109.  
  110. if(breakpoint > length || breakpoint - startpoint < 0) return fail();
  111. if(str == NULL) return fail();
  112.  
  113. long str_length = strlen(str);
  114. long temp_size = 0;
  115. bool to_resize = false;
  116.  
  117. if(length + str_length + 1 - (breakpoint - startpoint) > memsize)
  118. {
  119. temp_size = (length + str_length + 1 - (breakpoint - startpoint));
  120. to_resize = true;
  121. }
  122. else temp_size = memsize;
  123.  
  124. char * temp = new char[temp_size];
  125.  
  126. long temp_i = 0;
  127.  
  128. if(mystring) for(; temp_i < startpoint; temp_i++) temp[temp_i] = mystring[temp_i];
  129. for(long str_i = 0; str_i < str_length; str_i++, temp_i++) temp[temp_i] = str[str_i];
  130. if(mystring) for(long buf_i = breakpoint; buf_i < memsize; buf_i++, temp_i++)temp[temp_i] = mystring[buf_i];
  131.  
  132. temp[temp_i] = 0;
  133.  
  134. if(to_resize) alloc(temp_size); //empty
  135.  
  136. for(long buf_i = 0; buf_i < memsize; buf_i++) mystring[buf_i] = temp[buf_i];
  137.  
  138. if(temp) delete [] temp;
  139. length = strlen(mystring);
  140.  
  141. return success();
  142. }
  143.  
  144.  
  145. int main()
  146. {
  147.  
  148. stringclass str = "Hello";
  149.  
  150. str.ins("rld", str.get_length());
  151. cout << str << endl;
  152.  
  153. str.ins(" the wo", 5);
  154. cout << str << endl;
  155.  
  156. return 0;
  157. }
Success #stdin #stdout 0.02s 2816KB
stdin
Standard input is empty
stdout
checkpoint 1
checkpoint 2
checkpoint 1
checkpoint 2
Hellorld
checkpoint 1
checkpoint 2
Hello the world