Mental Jetsam

By Peter Finch

Archive for June, 2008

Replace a string with a character in C

Posted by pcfinch on June 26, 2008

This code is a simple string search and replace function in C/C++. It searches a NULL terminated string and replaces all the matching strings with a single character. For example, find all the occurrences of “&” is a string and replace them with “&”. The code is fast, as it only performs one single pass of the string. The code modifies the existing string, so care should be taken when using it, however, as the matched string can only ever be replaced with a single character the resulting string will never be larger that the original string, so it’s a pretty safe function.

void str_replace_c(char*pszString, char* pszMatch, char cTo)
  char* pStart = NULL ;
  char* pInMatch = pszMatch ;
  char* pInString = pszString ;
  char* pOutString = pszString ;
  char c ;

  if((pszString == NULL) ||
     (pszMatch == NULL) ||
     (*pszString == ''))
    return ;

  do {
    c = *(pInString++) ;
    if(pStart == NULL) {
      if(c == *pszMatch) {
        pStart = pOutString ;
        pInMatch = pszMatch + 1 ;
    } else {
      if(*pInMatch == '') {
        pOutString = pStart ;
        *(pOutString++) = cTo ;
        pStart = NULL ;
      } else {
        if(*pInMatch == c)
          pInMatch++ ;
          pStart = NULL ;
    *(pOutString++) = c ;
  } while(c != '') ;

int main()
  char in[] = "& amp; This is & amp; a test & amp;" ;
  str_replace_c(in, "& amp;", '&') ;
  printf("out=\"%s\"\n", in) ;

Posted in C++/MFC | Leave a Comment »

Holiday to France 2008

Posted by pcfinch on June 9, 2008

In 2008 my wife and I took a holiday to France. I have never been to Europe before, apart from on business, so I were really excited. Paris is beautiful, breathtaking and overwhelming. The French are really nice and we had very little trouble getting around and communicating with people, even though I only know a few words in French.

We spent 2 days in Hong Kong on the way there to try and minimize the Jet-lag as flying from Sydney to Paris is about 24 hours on planes with an 8 hour time difference. Hong Kong is great. I have only been there once before and it really is amazing to see it again.

We then flew on to Paris for a 6 day stay. We stayed at the Waldorf Trocadero about 10 minute walk from The Eiffel tower and the Arc De Triomphe. The hotel is great and it feels like a little French hotel and really gave us a feeling of being “in” Paris. The Australian dollar (as it is) made it a bit expensive for us, but it was worth it.

We walked heaps while in Paris, the first day we must have walked about 30 K’s and it’s a great way to see Paris (get a Lonely Panet Guide!!). It is such a beautiful and interesting place and when you are walking around you get to mix with “real” people and see all the little stuff you might miss if you just take the train or a cab. The trains, by the way, are fantastic… I don’t think we ever waited more than 5 minutes when we had reason to take one. We saw a lot of Paris, but it really is one of the most amazing places I have ever been. Just overwhelming! I think you could spend a lifetime there and only just scratch the surface. For me, I really enjoyed the Military Museum attached to Napoleons Tomb, along with everything else of-course.

We then visited Nice, on the Mediterranean, for a couple of days and took a day trip to Cannes during the film festival. We did not see anyone famous, but it was still really cool just to be there and get the “vibe” of the place. We then took the Eurostar train to London for a couple of days. London was interesting and we “took in a show”, Chicago, at the west end… that was neat! Growing up in Australia with English parents I knew all the places and it was just fantastic to actually see them for real!

Here are some photo’s if you interested. I hope you enjoy!

Posted in Personal | Leave a Comment »

Determine a shell scripts home directory.

Posted by pcfinch on June 5, 2008

The following bit of shell script is useful in determining the home directory of the script being run. When a script is executed from the command line normally it’s home directory is the directory that the script was execute from not the directory the script lives in. This can make it difficult to load other
resources that may be in the scripts home directory or relative to it.

DIRNAME=`dirname "$0"`
SCRIPTDIR=`(cd $DIRNAME && pwd)`

Posted in Programming | 2 Comments »

Using Javascript to POST data between pages

Posted by pcfinch on June 2, 2008

The following is a simple example of how to submit data from one HTML page to another using the POST method from Javascript. Normally, if data needs to be sent from one HTML page to another, it is done by appending the information to the query parameter of the the URL in the familiar “name=value” format. e.g.

< href="post.aspx?user=peter&cc=aus">Click</a>

Although this works fine, in most cases, problems occur when there is a lot of data to send and the URL exceeds about 2000 characters. The other disadvantage is that the URL looks ugly. The traditional method to get around this is to POST the data using a form. e.g.

<form name="myform" method="post" action="post.aspx">
<input name="user" value="peter"/>
<input value="cc" value="aus"/>
<a href="javascript:myform.submit()">Click</a>

This works fine, however, it does tend to make the formatting of the resulting HTML difficult and increases the size of the final HTML page significantly if there are lots of links on it.

The following javascript function takes the URL of the target page and an associative array of name/values paires and POSTs the data to the supplied URL by dynamically creating a form and then submitting it.

function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;

To insert a link into a page just use a normal anchor tag in the HTML and call the function.

<a href="javascript:postwith('post.aspx',{user:'peter',cc:'aus'})">click</a>

This works on Microsoft IE, Mozilla Firefox and OS X Safari.

Posted in HTML, javascript | 71 Comments »