Blog

Powered by Blogger View My Profile

C String Handling

While attempting to write a utility for accessing the internal data of tracked modules I found that I required a C function that could break a string into an array of strings using a delimiter. I spent an hour or more just trying to figure out how to do it, and ended up with the following code (which you may use if you wish):

/* 2008-06-24 */

char **split_str(const char *str, const char *teststr) {
/* Splits a string into one or more substrings based on a test string.
* The substrings are returned as an array of strings, terminated with
* a NULL pointer.
* If the teststr is not found in the original string, an array with just
* that string is returned, as would be expected. Therefore, it might be a
* good idea to make sure the teststr is in the string before you call this
* function on it.
* If you pass an empty string as the str, you will get an empty array;
* i.e. an array with one object that is a NULL pointer.
* Note that the test str is not included in the results.
* Memory for the array is dynamically allocated using malloc, so remember
* to free it when you're done (unless you get a NULL pointer, of course).
*/
char **subs = NULL;
char const * breakstart = str;
char *breakend = NULL;
int splits = 0;
int startstrlen = 0;
int teststrlen = 0;

startstrlen = strlen(str); /* Length of original str */
teststrlen = strlen(teststr); /* Length of the test string */

/* Scan until break */
while (breakstart[0] != '\0') {
splits ++;

/* Continually add more space for the subs */
subs = realloc(subs,sizeof(char*)*splits);
/* Find the next break end */
breakend = strstr(breakstart,teststr);
/* If there was no occurence of the substr, put the break at the end */
if (!breakend)
breakend = (char*) str+strlen(str)+1;
/* Create a new block for the substr */
subs[splits-1] = malloc(sizeof(char*)*(breakend-breakstart)+1);
/* Copy the substring */
memcpy(subs[splits-1],breakstart,(breakend-breakstart));
/* Add the null byte */
subs[splits-1][breakend-breakstart] = '\0';
/* If there is more string to scan, continue scanning it */
if (breakend)
breakstart = breakend + teststrlen;
/* If there is no more to scan, don't scan anymore */
else
break;
}

/* Terminate the array */
subs = realloc(subs,sizeof(char*)*splits+1);
subs[splits] = NULL;

return subs;
}


Shortly after writing this function I stumbled upon a little library called Bstrings (Better String Library) which is supposed to implement this feature, as well as solve a lot of common problems with strings in C.

I guess I'm kind of relieved and frustrated at the same time...

Labels: , ,

<< Home

News: Blog Merging with the News Updates.

If you recently used the usual http://alphaios.net link that I keep posting everywhere you may have noticed that you were automatically redirected to this blog. That is because, from now on, I want all visitors to the site to see the blog first. Why? Because I've officially abandon the very, very old static news page in favor of using the blog engine for posting updates.

For now the old news page can still be accessed at the same URL as before, but I will no longer be using it. I'll keep it up for a while so that older updates can still be accessed, at least until I can move those updates into the blog--or until they become so incredibly old that they no longer are relevant. (And that will be very soon...)

Also, the home page is still available as an "about" page from the house-shaped icon in the navigational bar.

Labels: , ,

<< Home

New AIOS Engine Will Use PHP, Not Python

It looks like there is just no good way to make a dynamic content management system using Python through CGI. In order to make it work you have to do all kinds of crazy stuff, and I don't want to do try it anymore...So, I've quit on the Python AIOS project altogether. Instead, I'm going to go back to the old days: PHP. Yes, good ol' highly-compatible PHP.

Don't get me wrong. I love Python. But it just is no good for making a CMS unless you have the access privileges to your web server to install all the stuff you need, and you usually don't. (I certainly don't.) I will still use it for other things, but for now...Alphaios.net is going back to PHP.

Labels: , , , , ,

<< Home

Food, Drink, and Tracked Music

http://necrofamicon.com/swt/Music/index.html

While searching for code or libs that might be useful to me in producing the ultimate Python-compatible tracked music system, I stumbled across the link above. Apparently I have a lot in common with this guy...though I haven't posted them all yet, there are at least as many original songs on my hard drive as are on this page. Only I went through the trouble of actually naming mine.

Anyway, some of those songs are pretty cool, and even the randomly-generated ones at the bottom of the page sound pretty good. Some of

<< Home

MikMod in Python

Well, after neglecting my studies for about a day and I half I finally managed to make libmikmod.so work with Python using ctypes. That means it is, in fact, possible to play tracked module music using Python. Soon I will release a Python package that will wrap MikMod completely and implement some Python classes that will make it very easy to use MikMod in a so-called "Pythonic" way. I will use it to implement the script on Alphaios.net that will automatically display information about modules that I upload.

...at least, that's the plan. Everyone knows how bad I am about keeping those kind of promises. ^_^

Labels: , , , , , , ,

<< Home

The Danger of Microwaved Water

A friend of mine recently sent me the following email, which I'm sure has spread all over the internet by now:

> > Microwaving Water!
> >
> > A 26-year old man decided to have a cup of coffee. He took a cup of
> > water
> > and put it in the microwave to heat it up (something that he had
> > done
> > numerous times before). I am not sure how long he set the timer for,
> > but
> > he wanted to bring the water to a boil. When the timer shut the oven
> > off,
> > he removed the cup from the oven. As he lookIed into the cup, he
> > noted that
> > the water was not boiling, but suddenly the water in the cup 'blew
> > up'
> > into his face. The cup remained intact until he threw it out of his
> > hand ,
> > but all the water had flown out into his face due to the build up
> > of
> > energy. His whole face is blistered and he has 1st and 2nd degree
> > burns
> > to his face which may leave scarring.
> >
> > He also may have lost partial sight in his left eye. While at the
> > hospital, the doctor who was attending to him stated that this is a
> > fairly
> > common occurrence and water (alone) should never be heated in a
> > microwave
> > oven. If water is heated in this manner, something should be placed
> > in
> > the cup to diffuse the energy such as a wooden stir stick, tea bag,
> > etc.,
> > (nothing metal).
> >
> > It is however a much safer choice to boil the water in a tea
> > kettle.
> >
> > General Electric's
> > Response:
> >
> > Thanks for contacting us, I will be happy to assist you. The e-mail
> > that you
> > received is correct. Microwaved water and other liquids do not
> > always
> > bubble when they reach the boiling point. They can actually get
> > superheated and not bubble at all. The superheated liquid will
> > bubble up
> > out of the cup when it is moved or when something like a spoon or
> > tea bag is
> > put into it.
> >
> > To prevent this from happening and causing injury, do not heat any
> > liquid
> > for more than two minutes per cup. After heating, let the cup stand
> > in the
> > microwave for thirty seconds! before moving it or adding anything
> > into it
> >
> > Here is what our local science teacher had to say on the matter:
> > 'Thanks for the microwave warning. I have
> > seen this happen before. It is caused by a phenomenon known as
> > super
> > heating. It can occur anytime water is heated and will particularly
> > occur
> > if the vessel that the water is heated in is new, or when heating a
> > small
> > amount of water (less than half a cup).
> >
> > What happens is that the water heats faster than the vapor bubbles
> > can form.
> > If the cup is very new then it is unlikely to have small surface
> > scratches inside it that provide a place for the bubbles to form. As
> > the
> > bubbles cannot form and release some of the heat that has built up,
> > the
> > liquid does not boil, and the liquid continues to heat up well past
> > its
> > boiling point.
> >
> > What then usually happens is that the liquid is bumped or jarred,
> > which is
> > just enough of a shock to cause the bubbles to rapidly form and
> > expel the
> > hot liquid. The rapid formation of bubbles is also why a carbonated
> > beverage
> > spews when opened after having been
> > shaken.'
> >
> > If you pass this on you could very well save someone from a lot of
> > pain and
> > suffering

I knew water could be supercooled, which is really awesome to watch, but
I didn't know it could be superheated as well. That explains the "flash
boiling" effect I sometimes get when I heat a mug of water in a
microwave for about 4 minutes and then drop a tea bag in. I always
thought it was caused by small amounts of air in the teabag suddenly
expanding because of the heat in the water...apparently it's the water,
not the air, that is expanding.

I didn't actually forward this to anybody...

Labels: , , ,

<< Home

A Completely Useless Title

Unfortunately I've never been any good at fooling anyone so I'm not going to do any April fooling today.


































































































































Just kidding. April Fools!

<< Home