Posts tagged with c++

C# Replace New Lines

October 29th, 2009

If you know you’re working on the Windows platform and all data will come from Windows then to replace newlines using C#.NET we just use.

myString=myString.Replace(Environment.NewLine,"");

In all other cases it’s much safer to string together the Replace function to cover all possibilities.

myString=myString.Replace("\r", "").Replace("\n", "");

C# IsNumeric

October 28th, 2009

If you do a search for ‘C# IsNumeric’ you’ll find a lot of hits talking about why this function is available in VB.NET but not in C#.NET. It’s used to test if a given string can be converted to a number.

I agree it’s a very useful function and there’s lots of possible solutions, the main ones being

  1. Loop through the string and check each Char is a number
  2. Use Double.TryParse is a very popular one since it doesn’t throw an exception on failure
  3. Use Convert.ToInt32 inside a try/catch block
  4. Using a Regex expression

I really wanted to use Double.TryParse but unfortunately this will return true for numbers that are decimals. Therefore if you want to test a string for whole numbers you also have to check the string for periods or commas (using string.contains) depending on your locale.

For this reason I tend to use the Regex solution, mainly because it’s very short and simple to implement.

public static bool IsNumeric(string str)
       {
            return Regex.IsMatch(str, "^[0-9]+$");
       }

It also requires the using directive System.Text.RegularExpressions

Display Server Time Using Javascript and ASP.NET in C#

November 20th, 2008

I’ve recently written an ASP.NET page using C# that required displaying the server time on the webpage using Javascript. To do this you need to output the Javascript code using C# and initialise the Javascript Date() object with the server time, after playing around I found this code does the job.

"server_time = new Date('" + DateTime.Now.ToString("MMMM d, yyyy HH:mm:ss") + "');"

The entire code looks like this assuming you have a label with id=’clock’ on the page. Just output the string js to the page to add the clock.

const string crlf = "\r\n";
string js = "<script type='text/javascript'>" + crlf +
"window.onload=startclock;" + crlf +
"var clock;" + crlf +
"var time_diff;" + crlf +
"function startclock(){" + crlf +
   "clock=document.getElementById('clock');" + crlf +
   "server_time = new Date('" + DateTime.Now.ToString("MMMM d, yyyy HH:mm:ss") + "');" + crlf +
   "time_diff=new Date()-server_time;" + crlf +
   "setInterval('runclock()',1000);" + crlf +
"}" + crlf +
"function runclock(){" + crlf +
   "var cDate=new Date();" + crlf +
   "cDate.setTime(cDate.getTime()-time_diff);" + crlf +
   "var curr_hours = cDate.getHours();" + crlf +
   "var curr_mins = cDate.getMinutes();" + crlf +
   "var curr_secs = cDate.getSeconds();" + crlf +
   "curr_hours=(curr_hours < 10)?'0' + curr_hours:curr_hours;" + crlf +
   "curr_mins=(curr_mins < 10)?'0' + curr_mins:curr_mins;" + crlf +
   "curr_secs=(curr_secs < 10)?'0' + curr_secs:curr_secs;" + crlf +
   "clock.innerHTML=curr_hours+':'+curr_mins+':'+curr_secs;" + crlf +
   "}" + crlf +
"</script>";

I should mention that if you’re using classic ASP and VBScript then the line becomes.

"server_time = new Date(" & DatePart("yyyy",Date) & "," & DatePart("m",Date)-1 & "," & DatePart("d",Date) & "," & DatePart("h",Now) & "," & DatePart("n",Now) & "," & DatePart("s",Now) & ");"

Too Busy to Blog

November 13th, 2008

I hope to add some more posts very soon, it’s not that there’s nothing happening but rather a bit too much going on.

At work I’m involved with a project in SAP that takes all my time, I’ve also written some webpages in ASP.NET using C#, something I’ve never done before. I’ve also got a new work laptop, a HP 6930p so I’ll write my impressions on that soon.

Personally I’ve had an old schoolfriend visit who now lives in Melbourne, Australia plus I’ve just come back from a week visiting family in England so spare time is in short supply right now.

On top of all that Fedora 10 comes out in 12 days!

Development tools on Windows vs Linux

March 12th, 2008

As far as desktop systems go, for email, internet and basic office functionality, there’s no denying that Windows rules the roost. I would argue that Linux is actually not difficult to set up and use, it’s just different and probably has a perception problem, but therein lies the problem.

But from a developers point of view using Linux as the platform has many advantages over Windows. Try writing a simple “Hello world” program in C or C++ on a fresh Windows install an then compile and run it. You can’t.

Simply put, most Linux distributions come with compilers (gcc in this case) installed out of the box and many more are available with an ‘apt-get’ or ‘yum install’ command. You don’t even have to do this from the command line these days as most distributions have friendly graphical install programs that put anything Windows has to shame.

Want Python or Perl, Apache or MySQL again it’s a few clicks away along with hundreds (thousands?) of other tools. Plus once they’re installed the OS takes care of notifying you (and optionally installing) any updates so you’ll never be left with 3 year old, unpatched software on your computer.

The last time I checked my Windows install I had many applications that were way out of date but the effort required to check up and manually update each one individually was just too much effort.

On a related note I have been using Eclipse as my IDE of choice, mainly because of it’s support of both Python and C++ using the Pydev and CDT modules. So I thought I’d give it a try on Windows.

After downloading and installing Python then installing the Pydev module, getting Python to work was not too difficult. Next came C++ support, it involves installing MinGW following the instructions on download page, then configuring Eclipse using these instructions.

If you’re very lucky it will all work first time, but don’t count on it. And god help you if you decide to update any packages at a later date.

Compared to all this the development tools on Linux are a breeze. Linux may or may not be ready for the desktop for the average user (if there is such a person) but when it comes to the power user or developer it’s already ahead in my books.

Programming for Performance or Simplicity

March 11th, 2008

I was recently writing some code to be run from a command line that recursively walks through a network folder and all sub folders compiling a list of all the files, their last modified date and their size.

Since writing VBScript is quick and dirty I tend to start with that and in most cases it works just fine. However when speed really matter it’s just not up to the job.

The network directory contains about 35,000 files in 3,200 folders consuming 45.4GB of disk space. The script should simply gather the data required and write it to a flat text file (delimited with semi-colons). When I ran the VBScript it took 306 seconds (5 minutes and 6 seconds) to gather the data.

Not too bad but not fast enough. So I decided to write the same script both in Python and C++. Python is a very high level scripting language, so nowhere near as fast as a complied language like C++, but I wanted to see how it compared to VBScript. Here’s the results for the 3 programs.

Language Files indexed Time taken (seconds) Files per Second
VBScript 34999 306 114.4
Python 35018 68.5 511.2
C++ 35017 13.9 2519.2

Of course, as expected, for real performance C++ outstrips the scripting languages by a mile but Python also fairs pretty well.

When I also consider how simple the code is and how long it took to develop I think Python is the clear winner unless every ounce of performance is vital. Developing real world programs in Python is extremely easy and the code is fast.

I use another short script in Python to parse Apache log files and it amazes me how fast it runs. The current log is over 122,000 lines (27.1MB of data) long but the script parses the file and outputs the results to a text file in just over a second! Very impressive for an interpreted language.