Tag Archives: strings

This last week I have been treated to working on a code base written entirely in C code, no C++, all C!

While this has been an interesting challenge there are definitely times when I really missed some of the features provided in C++ and realised how much the standard template library is taken for granted! Anyway, as usual I only really post things on here which are of use to myself as reference at a later date, or items I found it particularly hard to track down elsewhere on the web. This post falls into both categories, and being such a fundamental issue I’m surprised that it took so long to find a solution.

I was working on adding a small lightweight XML library to the existing C code base. I was continuously faced with the problem of requiring some form of expandable buffer, in most cases to hold c strings (null terminated character arrays). The root of the problem is that there is no string type in C, no vectors and no dynamic arrays of any kind. This means that whenever you want to use one of the string functions such as strcopy, strcat or sprintf, you are always required to know the size of the buffer upfront and allocate memory appropriately (freeing the memory when the buffer is disposed).

The main function I was concerned with was sprintf which I required in order to build up strings for the xml documents. The length of the strings produced by sprintf could be very varied and picking a fixed buffer size would in most cases be over the top, and in some cases dangerous as buffer overflows could potentially always occur, corrupting the heap. Luckily I discovered the following technique which allows you to first print the formatted string to a dummy file (no data is actually written to a file and no I/O occurs) using fprintf. Once the fprintf call has completed (assuming the format string / parameters were valid) you can then check the return value to find out exactly how many characters were written to the dummy buffer, providing you with the exact buffer size required for allocation!

Once a buffer of the correct size is allocated sprintf can then be used with the same paramaters to safely fill in the buffer. This method does have its drawbacks. I guess that there is extra overhead incurred in formatting the string twice and gaining a handle to the dummy file. However, as far as I can find this is the only way to do this properly without using fixed size buffers. One work of warning, this technique should work across platforms but “:NULL” should be replaced with “nul” on unix systems. A simple #ifdef UNIX should sort that out!

Note: – In this example the character previous to the asterisk is a . meaning we will accept any number of wildcards. Backslashes are used simply to escape the . symbol which we need to represent as a literal string in our url.

Plus

The plus symbol + is very similar to the asterisk * symbol, however it will only match one or more instances of the previous character. Using the previous example..

In my project today I was taking the name of a user and then usig that to generate a data file e.g. “fred.dat”. I decided that I needed to keep the same data file for each user, regardless of how they entered there name .This name was not a username and could be writtin in any mixture of upper / lower case.

To resolve this I decided to make the filenames for each player uppercase when saved and loaded, e.g. FRED.dat but I couldn’t find any easy way to do so. The toupper() function I am familiar with only works on characters so I designed the following simple function to convert a string to uppercase.

NB:- Here I use the string data type, not c string.The resultant string can ofcourse be converted to c string by adding the post function .c_str()

Post navigation

I have recently graduated with a 1st class degree in MComp Games Software Development at Sheffield Hallam University, focusing primarily on application development in C++, with experience in graphics programming, scripting languages, DVCS/VCS and web technologyRead More