Roman number translator
8
Nifty way to swap two integers in C (C++) without using a temporary variable. Uses one line of code (3 assignments).
12
Using the Termios library, we can have the user enter a character on the keyboard, without it being displayed on the screen.
9
I was coding a project that used arrays when the lack of a standard foreach in C/C++ got to me. So, I coded this little function to help me. It applies a function that returns int to each element of the array.
9
This script lets you do any action the number of times specified.
-8
Databinding in asp.net 2.0 is takes very few lines of code.
-7
Databinding in asp.net 2.0 takes very few lines of code. This example shows how to bind a CheckBoxList using a SqlDataSource.
8
The best way I've found to keep a suite of CGI environment variables in my C CGI programs is actually just to read them as name-value pairs into a stack. It simplifies parsing and makes the code cleaner and less fragile than using a specialized structure or an ordered array of strings (as well, empty variables are simply not push()ed onto the stack, so memory doesn't have to be allocated for empty strings). Plus, since there are never a huge number of environment variables, and they are all unique (by definition), a search through the stack for a given name takes minimal time. In fact, retrieval of environment variables beats a PHP-like hash-table implementation by a good deal.
In the code below, all you have to keep in mind is that the NVStk is a name/value pair stack (implemented as a singly-linked list with each node containing two char*s). Variable retrieval times can be minimized by adjusting the order of variable names in the char**s passed to sgcgi_getenv(). In fact, the ones below are just about backwards from how they ought to be, since I forgot I was using a stack instead of a queue . . . *blush*
Of course, there are more environment variables you can get, but you have to draw the line between exhaustion and efficiency, and that depends on the project. The variables included here are pretty much overkill for any program you're likely to need.
A nice way to use these types of functions is to wrap them in an accessor function that gets the environment once and keeps it as a static variable, and then on subsequent calls just looks up values in its stack. (If you want to see the NVStk, I can put it up, but it's pretty much a basic linked list.)
In the code below, all you have to keep in mind is that the NVStk is a name/value pair stack (implemented as a singly-linked list with each node containing two char*s). Variable retrieval times can be minimized by adjusting the order of variable names in the char**s passed to sgcgi_getenv(). In fact, the ones below are just about backwards from how they ought to be, since I forgot I was using a stack instead of a queue . . . *blush*
Of course, there are more environment variables you can get, but you have to draw the line between exhaustion and efficiency, and that depends on the project. The variables included here are pretty much overkill for any program you're likely to need.
A nice way to use these types of functions is to wrap them in an accessor function that gets the environment once and keeps it as a static variable, and then on subsequent calls just looks up values in its stack. (If you want to see the NVStk, I can put it up, but it's pretty much a basic linked list.)
6
Another pull from my growing-towards-beta CGI library: sgcgi_url_unescape().
Note the use strcpy, which is faster than the equivalent memmove()ing. To ensure 64-bit safety, I plan to rename this function and then conditionally compile it to point to either strcpy or a 64-bit-safe memmove() implementation of strcpy.
However, even though copy order isn't guaranteed for strcpy, on 16-bit and 32-bit systems, all known implementations copy byte-by-byte from lower addresses to higher addresses. Some 64-bit optimized compilers may copy 8-byte chunks, making the assumption of full linearity unstable at best.
I know it sounds like I'm justifying use of nonstandard code for convenience . . . *blush* . . . it's just something that putting in a -DPEDANTIC type of preprocessor flag could fix if broken, and its SO much faster!
Note the use strcpy, which is faster than the equivalent memmove()ing. To ensure 64-bit safety, I plan to rename this function and then conditionally compile it to point to either strcpy or a 64-bit-safe memmove() implementation of strcpy.
However, even though copy order isn't guaranteed for strcpy, on 16-bit and 32-bit systems, all known implementations copy byte-by-byte from lower addresses to higher addresses. Some 64-bit optimized compilers may copy 8-byte chunks, making the assumption of full linearity unstable at best.
I know it sounds like I'm justifying use of nonstandard code for convenience . . . *blush* . . . it's just something that putting in a -DPEDANTIC type of preprocessor flag could fix if broken, and its SO much faster!
7
The modus operandi for this is similar to that taken by PHP's implementation of such functions. It's comparitively memory-intensive, but is much faster than running a whole bunch of tests.
Basically, you set a mask -- an array of 256 null bytes -- and set those that correspond to characters you wish to trim. Then, rather than having to test if a character is in the set of characters to trim(O(n), or linear time on *ws), you just test once (O(1), or unit time) to see if the byte in question is set.
And of course, to trim(), you just wrap trim() around both ltrim() and rtrim().
One point of caution: these functions trim in place, so copy strings before trimming them. (Of course, if you usually want access to both pre- and post-trimmed strings, you could always make these malloc() a new string and return a pointer to it . . . )
Basically, you set a mask -- an array of 256 null bytes -- and set those that correspond to characters you wish to trim. Then, rather than having to test if a character is in the set of characters to trim(O(n), or linear time on *ws), you just test once (O(1), or unit time) to see if the byte in question is set.
And of course, to trim(), you just wrap trim() around both ltrim() and rtrim().
One point of caution: these functions trim in place, so copy strings before trimming them. (Of course, if you usually want access to both pre- and post-trimmed strings, you could always make these malloc() a new string and return a pointer to it . . . )
8
Configurable number formatter.









