Friday, November 11, 2011

What makes Android Programming Simpler and iOS difficult?

ios-5-android-ice-cream-sandwich-apple-google

I cannot start writing iPhone and iPad software because?

I don't have an Apple... No! don't bring me a KG of fresh red apples please, I am referring to the Apple which is a computer. It's impossible to code stuff up without an Apple machine, even for sake of learning. Apple MUST create a tool chain for people running Windows as we know it's the most widely used operating system, just like the iPhone which is a crazily used "Somewhat Smart" Phone. They could create a development environment, and a few emulators to get new going with iOS development. I believe it would cost less than $100k to develop such a solution, maintenance cost won't go beyond this as well.

Friday, October 7, 2011

C# string utility functions containsAtLeastOneChar-containsOnlySpecifiedSpecChars-excludesSimilar

c-sharp-green

A utility class to perform simple operations on strings using C#. The logic is simple, one can transform the code given below to C++, PHP, or JavaScript within 2 minutes. I know each one of the programming languages I mentioned here, and all other first class modern programming languages have got sophisticated regular expression packages. But, the use of regex is not an option for everyone. Some got the skill to learn, but just don't have the time needed to read docs. Some don't want to import in a full blown namespace like System.Text.RegularExpressions
And some other are plain lazy.
Here goes the code, the names are descriptive enough, first one containsAtLeastOneChar  checks whether a string contains at least one character of a certain character set. For example, we want to check whether a password generated by a automatic password generator contains at least one capital letter, one number, and one special character.
Second one, containsOnlySpecifiedSpecChars returns true when the supplied string conains only specified characters.
Third function excludesSimilar will return true when a string does not contain repeating characters.
The class Commons contains static member variables, you could try passing these values to the static member functions of this class and experiment  around to check the results. It will also save you some typing :)
You could just copy paste the code given below as it is, it's supposed to work.
Happy coding!
public class Commons
{
public static bool containsAtLeastOneChar(string strWord, string strCompairTo)
{
 int nIdx = -1;
 foreach (char c in strWord)
 {
  nIdx = strCompairTo.IndexOf(c);
   if (nIdx >= 0) return true;
 }
 return false;
}
public static bool containsOnlySpecifiedSpecChars(string strWord, string strSpecialChars)
{
foreach (char c in strWord)
 {
  if ((strCaps.IndexOf(c) < 0) & (strSmalls.IndexOf(c) < 0) & (strNums.IndexOf(c) < 0))
 {
  if (strSpecialChars.IndexOf(c) < 0)
   return false;
  }
}
return true;
}
public static bool excludesSimilar(string strWord)
{
foreach (char c in strWord)
{
 if (strWord.IndexOf(c) != strWord.LastIndexOf(c))
  return false;
 }
 return true;
}

public static string strCaps = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
public static string strSmalls = "abcdefghijklmnopqrstuvwxyz";
public static string strNums = "0123456789";
}//class

Sunday, September 4, 2011

Reverse of a string by words C# example

string-reversal-example


Creating a program to generate the reverse of a string variable in C#, VB .Net, or C++ programming is pretty a easy job. Please note that we are talking about a word by word string reversal, for example the string given below 


Hello How Are You

Becomes the following string when read backwards

You Are How Hello


Reversal of characters inside a word is a different story, which is not covered here.
Some people find word order reversal difficult, that's why I'm putting the C# example code here.

Thursday, August 4, 2011

Nested conditional statement other than switch case in C#, C++, Java, PHP, JavaScript

nested-conditional-statement


Switch statements are nice, sometimes we need to just return some value based on an input variable. For example in our case I needed to get the key field of an LDAP object class we preferred to use in our software. So I had a simple C# function which went something like this


public static string getKeyByObjectClass(string strObjectClass){
switch(strObjectClass){
    case "user":
    return "samAccountName";
    case "user-mailbox":
    return "samAccountName";
    case "user-mail":
    return "samAccountName";
    case "contact":
    return "CN";
    case "contact-mail":
    return "CN";
    case "group":
    return "samAccountName";
    default:
    return "";
 }
}

I thought it is possible to simplify this switch statement with a nested conditional statement. And following was the result of some deliberation ;)








public static string getKeyByObjectClass(string strObjectClass){
              return (strObjectClass == "user") ? "samAccountName" : (strObjectClass == "user-mailbox") ?"samAccountName" :(strObjectClass == "user-mail") ? "samAccountName" : (strObjectClass == "contact") ? "CN" :(strObjectClass == "contact-mail") ? "CN" : (strObjectClass == "group") ? "samAccountName" : "";

You can definitely use || operator, minimize the code, and refine the crude version of nested conditional statement we created earlier, let's just do that

public static string getKeyByObjectClass(string strObjectClass){
              return (strObjectClass == "user" || strObjectClass == "user-mailbox" || strObjectClass == "user-mail") ? "samAccountName" :                                      (strObjectClass == "contact" || strObjectClass == "contact-mail" ) ? "CN" : "";
}

But now we definitely have some compromise on flexibility, maybe we'll want to use a different key for an object class say "user-mail" and then we'll have to modify this code in tiny bit different way(maintenance hell anyone?) ;)
I'm sure many intelligent folks have already done this or even a better variation but I also know that there are dumb guys and gals just like me out there who would most probably yell "Eurika!!!" on understanding the magic of nested conditional statements.
Its not just and only C# stuff, in fact the code will work with JavaScript, PHP, Java, C++ and many others with little or no variation.

Saturday, July 30, 2011

Cannot rename htaccess.txt to .htaccess in cPanel File Manager

Just today I was trying to fix the 404 broken link problem with the Joomla installation on my upcoming website, http://www.ldapfilters.com which will contain certain knowledge base about LDAP and directory servers in general, along with discussion forums.
So, they say WARNING! Apache users only! Rename htaccess.txt file to .htaccess before activating.
I opened the File Manager from CPanel, scrolled down to the htaccess.txt file right clicked and renamed it and the File Manager finally said "No! You can't rename, a file already exists." I tried and retried several gimmicks but it didn't work.
Solution: Then I decided to try legacy file explorer, I opened the CPanel Legacy File Manager, located htaccess.txt, clicked it, selected Rename from the menu, typed in .htaccess and it worked well ;)

A Confusion: I got confused and start thinking, shall I rename htaccess.txt to htaccess.htaccess or just remove the .txt from end of file name, and put a dot at the start of file name? Well the answer is yes, I had to take the second option. Therefore, just rename it to .htaccess that means a file with no name but just an extension.
And one more thing, I renamed htaccess.txt to htaccess_.txt and then tried to access my website, even the home page of my Joomla based website didn't show up let alone the broken links which I was trying to fix :p
Therefore, be very careful when you touch these configuration files, they are absolutely sensitive. Some times you catch the bug right after saving that file and refreshing that page but at some other time the setting you've just changed has got a much deeper effect which gets unleashed by some twisted user action sequence, so here's the GOLDEN ADVICE

Back those files up!!!