Skip to content

Instantly share code, notes, and snippets.

@Todd-Davies
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Todd-Davies/0ec9e459f3f44ae673cc to your computer and use it in GitHub Desktop.
Save Todd-Davies/0ec9e459f3f44ae673cc to your computer and use it in GitHub Desktop.
C character buffer
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "buffer.h"
/*
* Writes a character to the buffer
*/
void writeToBuffer(Buffer *b, char c, int position)
{
// Resize the array if needed
if(position == (b->length - 1))
{
b->length *= 2;
b->buffer = realloc(b->buffer, b->length*sizeof(char));
if(b->buffer == 0){
fprintf(stderr, "Failed to allocate memory to buffer\n");
exit(0);
}
}
b->buffer[position] = c;
return;
}
/*
* Finds the number of characters in the buffer
*/
int getLength(Buffer *b)
{
int i = 0;
for(i = 0; i < b->length; i++) if(b->buffer[i] == '\0') break;
return i;
}
/*
* Converts the case of a single character
*/
void convertCharCaseWithCount(char *c, int *toLower, int *toUpper)
{
if(isupper(*c))
{
*c = tolower(*c);
++*toLower;
}
else if (islower(*c))
{
*c = toupper(*c);
++*toUpper;
}
}
/*
* Converts the case of a single character
*/
void convertCharCase(char *c)
{
int i = 0;
convertCharCaseWithCount(c, &i, &i);
}
/*
* Converts the case of a buffer
*/
void convertCaseWithCount(Buffer *b, int *toLower, int *toUpper)
{
int i;
for(i = 0; i < b->length; i++)
{
convertCharCaseWithCount(&(b->buffer[i]), toLower, toUpper);
}
}
/*
* Converts the case of a buffer
*/
void convertCase(Buffer *b)
{
int i = 0;
convertCaseWithCount(b,&i,&i);
}
/*
* Sets up a buffer
*/
Buffer* constructBuffer(Buffer *buffer)
{
// Set the default buffer length
buffer->length = 20;
// Allocate the buffer
buffer->buffer = (char*)malloc(buffer->length);
if(buffer->buffer == 0)
{
fprintf(stderr, "Failed to allocate memory to buffer\n");
exit(0);
}
return buffer;
}
/*
* Reads a line into a Buffer
*/
void readLine(Buffer *buffer)
{
// Allocate temporary variables for the while loop
char c;
int input_length = 0;
// While we get characters from stdin
while((c = getchar()) != '\n') writeToBuffer(buffer, c, input_length++);
// Add a /0 at the end
writeToBuffer(buffer, '\0', input_length++);
}
/*
* Defines a simple String-like type
*/
typedef struct
{
int length;
char* buffer;
} Buffer;
/*
* Writes a character to the buffer
*/
void writeToBuffer(Buffer *b, char c, int position);
/*
* Finds the number of characters in the buffer
*/
int getLength(Buffer *b);
/*
* Converts the case of a single character
*/
void convertCharCase(char *c);
/*
* Converts the case of a single character
*/
void convertCharCaseWithCount(char *c, int *toLower, int *toUpper);
/*
* Converts the case of a buffer
*/
void convertCaseWithCount(Buffer* b, int* toLower, int* toUpper);
/*
* Converts the case of a buffer
*/
void convertCase(Buffer *b);
/*
* Sets up a buffer
*/
Buffer* constructBuffer(Buffer *buffer);
/*
* Reads a line from stdin to the buffer
*/
void readLine(Buffer *buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment