Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Simple way to check command line arguments in a C program

A simple way to check command line arguments.

Author: Joana Matos Fonseca da Trindade
Date: 2008.02.25

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* minimum required number of parameters */
#define MIN_REQUIRED 2

/* display usage */
int help() {
   printf("Usage: myprogram [-s <arg0>] [-n <arg1>] [-true]\n");
   printf("\t-s: a string a\n");
   printf("\t-n: a number\n");
   printf("\t-true: a single parameter\n");

   return 1;
}

/* main */
int main(int argc, char *argv[]) {
   if (argc < MIN_REQUIRED) {
      return help();
   }
   int i;

   /* iterate over all arguments */
   for (i = 1; i < (argc - 1); i++) {
       if (strcmp("-s", argv[i]) == 0) {
          /* do something with it */ 
          printf("string = %s\n", argv[++i]);
          continue;
       }
       if (strcmp("-n", argv[i]) == 0) {
          /* do something with it. for example, convert it to an integer */
          printf("number = %i\n", atoi(argv[++i]));
          continue;
       }
       if (strcmp("-true", argv[i]) == 0) {
          printf("true activated\n");
          continue;
       }
       return help();
   }
   return 0;
}

PHP : Comprobar e-mail válido / Check valid e-mail

Comprobar e-mail válido / Check valid e-mail
Código fuente / Source code :

function esEmailValido($email)
{
    if (ereg("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([_a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]{2,200}\.[a-zA-Z]{2,6}$", $email ) )
	{
       return true;
    }
	else
	{
       return false;
    }
}

Check email in html form

// description of your code here

 <script type="text/javascript">
        function check_email(email_id,err_id){
            emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/;
            var err_mail='Email addres incorect!';
            if(emailRegExp.test(document.getElementById(email_id).value)){
                alert('true');
                return true;
            }else{
                document.getElementById(err_id).innerHTML=err_mail;
                alert(err_mail);
                return false;
            }
        }
    </script>

        <span id="err_msg" style="color: red;"></span>
    <form id="myForm" name="myForm" action="./register.php" method="post" onsubmit="return check_email('email','err_msg');">
        <input type="text" name="email" id="email"/>
        <input  type="submit" name="send" value="Send" />
    </form>

PHP variable check

ugh

$page = isset($_GET['page']) ? $_GET['page'] : 'home';

Is Date //JavaScript Function


Checks if a date is valid and returns error codes described bellow.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/geral/is-date [v1.0]

isDate = function(y, m, d){ //v1.0
        if(typeof y == "string" && m instanceof RegExp && d){
            if(!m.test(y)) return 1;
            y = RegExp["$" + d.y], m = RegExp["$" + d.m], d = RegExp["$" + d.d];
        }
        d = Math.abs(d) || 0, m = Math.abs(m) || 0, y = Math.abs(y) || 0;
        return arguments.length != 3 ? 1 : d < 1 || d > 31 ? 2 : m < 1 || m > 12 ? 3 : /4|6|9|11/.test(m) && d == 31 ? 4
        : m == 2 && (d > ((y = !(y % 4) && (y % 1e2) || !(y % 4e2)) ? 29 : 28)) ? 5 + !!y : 0;
};


Usage
<script type="text/javascript">

getDateMsg = function(x){
    return x == 0 ? "Valid Date"
    : x == 1 ? "Invalid date format"
    : x == 2 ? "Invalid day"
    : x == 3 ? "Invalid month"
    : x == 4 ? "In April, June, September and November there's no 31 day"
    : x == 5 ? "February has only 28 days"
    : x == 6 ? "In leap years, February has 29 days": "nothing =]";
};

var x = [
    isDate("22/07/1984", /^([0-9]{1,2})[\/]([0-9]{1,2})[\/]([0-9]{1,4})$/, {d: 1, m: 2, y: 3}),
    isDate("1984-07-22", /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/, {d: 3, m: 2, y: 1}),
    isDate("07-22-1984", /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/, {d: 3, m: 2, y: 1}),
    isDate(2000, 1, 32),
    isDate(2000, 0, 1),
    isDate(2000, 4, 31),
    isDate(2001, 2, 29),
    isDate(2004, 2, 30)
];

for(var i = -1, l = x.length; ++i < l; document.write(getDateMsg(x[i]), "<br />"));

</script>


Help
isDate(y: Integer, m: Integer, d: Integer): Integer
    Checks a date and returns 0 if it's valid or one of the error codes bellow.
    y year
    m month
    d day

	
isDate(date: String, matcher: RegExp, map: Object): Integer
    Checks a date and returns 0 if it's valid or one of the error codes bellow.
    date date in a string form
    matcher regular expression responsible to find and store the day, month and year
    			
    map object containing the position where each date component is localized inside the regular expression. Its format is the following: {d: positionOfTheDay, m: positionOfTheMonth, y: positionOfTheYear}								
Return codes
    * 0 = Valid date
    * 1 = Date format invalid (regular expression failed or amount of arguments != 3)
    * 2 = Day isn't between 1 and 31
    * 3 = Month isn't between 1 and 12
    * 4 = On April, June, September and November there isn't the day 31
    * 5 = On February the month has only 28 days
    * 6 = Leap year, February has only 29 days

Check the syntax of a bunch of PHP files all at once

Will find all files ending with 'php' and execute the syntax check of php for every found file. Useful if you quickly want to see which php file contains a parse error.

find ./ -type f -name \*.php -exec php -l {} \;

Thai election ID checking

The online service here aims to help people
check where they are to vote. However, it can be
used as ID certification as well.
# nnnn is the id to be checked
http://www.dopa.go.th/cgi-bin/inqelect.sh?pid=nnnn

Here I make it into a function call.
import urllib, re
def getname(id):
  url = 'http://www.dopa.go.th/cgi-bin/inqelect.sh?pid=' + str(id)
  src = urllib.urlopen(url).read()
  pat = '<H3> *(.*?) *<'
  name = re.findall(pat, src)[0]  # don't use other info
  return name

print getname(5100900050063)  # show a name (one of my relatives)
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS