Perl & C Code Samples

The Perl code sample can be added to a form to verify an address by asking the user to enter it twice. The C code sample will parse HTML files in C CGI scripts; this will allow you to use page templates and dynamically create result pages.

Perl Sample Code

To add an e-mail address checker to your order form, begin by adding a second e-mail field to the form. In this example, we'll call the first e-mail field Email and the second one Em2.

Add the following lines of code to the CGI script that processes your order form. Add the code before the script checks for missing information.

$Form{Em2} =~ s/\s+//g;
$Form{Em2} =~ s/\///g;
$Form{Email} =~ s/\s+//g;
$Form{Email} =~ s/\///g;
if ($Form{Em2} ne $Form{Email}) {
    $Form{Em2} = "Did not match";
    $Form{Email} = "";
}

This checks whether Email and Em2 match. If not, then Email is cleared.

When your script then checks whether all fields have values, Email will be blank and the script will ask the customer to enter both e-mail fields again.

C Sample Code

These two functions parse text files. str_replace takes the input string from a file, the output string you're going to print, the string to search for, and the string to replace. Here's an example:

fread(proc_buf, sizeof(char), 100000, ACCEPT);

strcpy(print_buf, "");
str_replace(proc_buf, print_buf, "<ICS_TAG
    name=accept_url>", ics_fgetbyname(res,
    "download_url0"));

void str_replace(char *input_str, char
    *output_str, char *search_str,
    char *replace_str) {

   int found=0, start_pos=0, search_len =
    strlen(search_str);

   while (found != -1) {

    found = pmatch(input_str+start_pos,
      search_str);

    if (found != -1) {
      strncat(output_str, input_str +
      start_pos, found);
      start_pos = start_pos + found +
      search_len;
      strcat(output_str, replace_str);
    }
   }

   strcat(output_str, input_str+start_pos);

}

int pmatch(char *string, char *pat) {
   int i,j=0, start=0;
   int lasts = strlen(string)-1;
   int lastp = strlen(pat)-1;
   int endmatch = lastp;

   for (i = 0; endmatch <= lasts; endmatch++,
    start++) {
    if (string[endmatch] == pat[lastp])
    for (j = 0, i = start; j < lastp &&
      string[i] == pat[j]; i++, j++);
    if (j == lastp)
    return start;
   }
   return -1;
}