Esempio n. 1
0
/* Safe way of reading the contents of 'input'
 * into a string 'str'. Your job is to read and
 * understand how this works and why is it safe,
 * and then fill out the body of the while() loop.
 * Feel free to use the resize_string function
 * provided above.
 *
 * Parameters:
 *   str: pointer to string that will be filled
 *        up; if the string is NULL, it will be
 *        created, else it'll be appended to.
 *   input: input FILE pointer
 * Returns:
 *   length of new string str
 */
size_t getstr( char **str, FILE *input ) {
  size_t chars_to_read = BLOCK_SIZE;
  size_t length = 0;

  // If str already exists, we'll append to the end
  if( *str != NULL ) {
    length = strlen( *str );
    while( chars_to_read < length ) {
      chars_to_read <<= 1;
    }
  }

  resize_string( str, chars_to_read << 1 );

  // Try to read in the number of 'chars_to_read'; store # of 
  // chars actually read from input in 'chars'
  size_t chars = 0;
  while( ( chars = fread( *str + length, 1, chars_to_read, input ) ) ) {

    size_t used_bytes = chars + length;
    length = strlen( *str );

    if (chars < chars_to_read){
      resize_string(str, used_bytes); //Resize exactly to the amount of bytes used.
      break;
    }


    
    chars_to_read <<= 1;
    resize_string(str, chars_to_read << 1);

    //length = strlen ( *str );
    //resize_string(str, length);
			 
    /* What you need to do:
     * We've just read # 'chars' into str. If we're at the end of
     * the file, we should exit the loop. Otherwise, we should
     * resize the string to read in more chars. Certain methods
     * will be faster than others, so try out a couple of different
     * ways of doing this and pick the fastest one. DO NOT LEAK MEMORY!
     */
  }

    length = strlen( *str );
    //    printf("string is:%s, length right now is:%zu\n", *str,  length);

  // Add a terminating '\0' (removing the final newline)
  // and resize to save space
  if( length > 0 ) {
    length = length - 1;
    (*str)[length] = '\0';
    char *tmp = realloc( *str, length + 1 );
    if( tmp != NULL ) {
      *str = tmp;
    }
  }
 return length;
}
Esempio n. 2
0
char *my_chrcat(MyString *mstr, const char c) {
	resize_string(mstr, mstr->length + 2);
	mstr->str[mstr->length++] = c;
	mstr->str[mstr->length] = '\0';

	return mstr->str;
}
Esempio n. 3
0
char *my_strcat(MyString *mstr, const char *str) {
	size_t len = mstr->length + strlen(str);
	resize_string(mstr, len + 1);
	strcpy(mstr->str + mstr->length, str);
	mstr->length = len;

	return mstr->str;
}
Esempio n. 4
0
char *my_strncat(MyString *mstr, const char *str, size_t n) {
	size_t len = mstr->length + n;
	resize_string(mstr, len + 1);
	strncpy(mstr->str + mstr->length, str, n);
	mstr->length = len;

	return mstr->str;
}
Esempio n. 5
0
        /**
            @brief appends a string (inserts it at the end)
        */
        void append_string(const string_type & str) {
            if ( m_reserve_append < str.size()) {
                std::size_t new_reserve_append = str.size() + m_grow_size ;
                resize_string( m_reserve_prepend, new_reserve_append);
            }

            BOOST_ASSERT(m_reserve_append >= str.size());

            typename string_type::difference_type start_idx = static_cast<typename string_type::difference_type>(m_str.size() - m_reserve_append);

            std::copy(str.begin(), str.end(), m_str.begin() + start_idx);
            m_reserve_append -= str.size();
            m_full_msg_computed = false;
        }
Esempio n. 6
0
        /** 
            @brief appends a string (inserts it at the end)
        */
        void append_string(const string_type & str) {
            if ( m_reserve_append < (int)str.size()) {
                int new_reserve_append = (int)str.size() + m_grow_size ;
                resize_string( m_reserve_prepend, new_reserve_append);
            }

            BOOST_ASSERT(m_reserve_append >= (int)str.size() );

            int start_idx = (int)m_str.size() - m_reserve_append;

            std::copy(str.begin(), str.end(), m_str.begin() + start_idx);
            m_reserve_append -= (int)str.size();
            m_full_msg_computed = false;
        }
Esempio n. 7
0
        void append_string(const char_type* str) {
            std::size_t len = str_len(str);
            if ( m_reserve_append < len) {
                std::size_t new_reserve_append = len + m_grow_size ;
                resize_string( m_reserve_prepend, new_reserve_append);
            }

            BOOST_ASSERT(m_reserve_append >= len );

            typename string_type::difference_type start_idx = static_cast<typename string_type::difference_type>(m_str.size() - m_reserve_append);

            std::copy(str, str + len, m_str.begin() + start_idx);
            m_reserve_append -= len;
            m_full_msg_computed = false;
        }
Esempio n. 8
0
        void append_string(const char_type* str) {
            int len = str_len(str);
            if ( m_reserve_append < len) {
                int new_reserve_append = len + m_grow_size ;
                resize_string( m_reserve_prepend, new_reserve_append);
            }

            BOOST_ASSERT(m_reserve_append >= len );

            int start_idx = (int)m_str.size() - m_reserve_append;

            std::copy(str, str + len, m_str.begin() + start_idx);
            m_reserve_append -= len;
            m_full_msg_computed = false;
        }
Esempio n. 9
0
char * enc (char *string, size_t length){
  if (length <= 2){
    //printf("string=%s, length = %zu\n", string,length);
        return string;
  }
  size_t N = length;
  size_t k = N/2;

  char *left_side = malloc(k * sizeof(char)); //split left
  char *right_side = malloc( (N-k) * sizeof(char)); //split right
  
  // Assign left_side = [s_k, s_k-1, ... , s1]
  for(size_t i = 0; i < k; i++){
    left_side[i] = string[k-1-i];
  }
  
  // Assign right_side = [s_N, s_N-1, ... , s_k+1]
  for(size_t i = 0; i < (N-k); i++){  
    right_side[i] = string[N-1-i]; 
  }  
  //printf("string:%s, left side is: %s, right side is: %s, its lenght is:%zu\n",string, left_side, right_side, length);  
  
  left_side = enc(left_side, strlen(left_side));
  right_side = enc(right_side, strlen(right_side));
  resize_string(&left_side, N);
  strcat(left_side, right_side);
  
for(size_t i = 0; i < N; i++){  
  string[i] = left_side[i];
 }

  free(left_side);
  free(right_side);

  //  printf("string:%s\n", string);
  return string;
    //left_side;
    //split left
    //split right
    // apply backwards assigning using while loop
    // add them recusrively calling the function.
    // call the function again
    //strcat(lh

}
Esempio n. 10
0
 void reserve_append(std::size_t new_size) {
     resize_string(m_reserve_prepend, new_size);
 }
Esempio n. 11
0
 void reserve_append(int new_size) {
     resize_string(m_reserve_prepend, new_size);
 }
Esempio n. 12
0
 void reserve_prepend(int new_size) {
     resize_string(new_size, m_reserve_append);
 }