Wednesday, December 23, 2015

HTML with ASP if condition

Here's a little peice of code that can come in handy. It's a way of building HTML code around an ASP if condition. The if condition in this example references the current file name.

<%
Dim my_array, fullname, fname
fullname = Request.ServerVariables("SCRIPT_NAME")
my_array=split(fullname,"/")
fname=my_array(ubound(my_array))
if (fname = "about.asp") then
%>

<ul class="blocklist">
  <li><a href="http://prbseminary.org">Home</a></li>
  <li><a href="http://prbseminary.org/about.asp">About</a></li>
  <li><a href="mailto:admin@prbseminary.org?subject=PRBS Contact">Contact</a></li>
  <li><a href="http://prbseminary.org/donate.asp">Donate</a></li>
  <li><a href="http://prbseminary.org/library.asp">PRBS Library</a></li>
  <li><a href="http://prbseminary.org/academics.asp">Academics</a></li>
  <li><a href="http://prbseminary.org/app/apply.html" target="_blank">Apply Online</a></li>
  <li><a href="http://prbseminary.org/mentor.asp">Mentors</a></li>
  <li><a href="http://prbseminary.org/faq.asp">FAQ</a></li>
</ul>

<% end if %>

Monday, November 30, 2015

Case Insensitive String Compare in C++

Sometimes you want to compare or read a string in 'case-sensitive' mode (i.e. whether a letter is capital or lowercase matters), while at other times you want to compare or read a string without worrying about the case  (i.e. case insensitive). Passwords, for instance, would be best compared/read in case-sensitive mode...while some AI interfaces that want to read what the user is inputing would be best read in case-insensitive mode.

So on to the code. This is a simple way of converting text input to all upper-case, thereby being able to read/compare the input without having to worry about case sensitivity...

#include <iostream>
using namespace std;

void tocaps(char *);
int main()
{
  char input_string[200];
  cout << "Enter a string :"; cin >> input_string;
  tocaps(input_string);
  cout << "After upper case conversion :" <<endl << input_string;
  return 0;
}

void tocaps(char *a)
{
  int i=0;
  while(*(a+i)!='\0')
  {
    if(*(a+i)>=97 && *(a+i)<=122) (*(a+i))-=32;
    i++;
  }
}

Wednesday, November 25, 2015

Old School C String Compare

Even though i prefer C++ to C, sometimes it's good to know a little old school. Here's an example of how one can compare strings with C language. Since the C language does not have a string type, strings are built with arrays or pointers.

#include <stdio.h>
int main() {
  char * first_name = "John"; /* using pointer */
  char last_name[] = "Doe"; /* using local array */
  char name[100];
  /* comparing strings */
  if (strncmp(first_name, "John") != 0) return 1;
  if (strncmp(last_name, "Doe") != 0) return 1;
  sprintf(name, "%s %s", first_name, last_name);
  return 0;
}
To break it down a bit...
The first_name variable is set as a pointer, which means that we can view it but not alter it. The last_name variable is set as a local array, which means we can also alter it. We can alter the last_name in whole or in part since it is constructed as an array.

last_name[] = "Smith"; /* this would change the whole last name to `Smith` */
last_name[0] = "R"; /* this would change the first character in the last name, so that the last name becomes `Roe`  */
You may be asking yourself, why is the array bracket empty? Well,  it doesn't have to be. When it's left empty we allow the system to determine the number of characters. The thing to remember is that the number of characters will be one more than the string that you are defining. This is because in C you need to leave  room for string termination. Therefore...

char last_name[] = "Doe";
/* would be the same as */
char name[4] = "Doe";

The `strncmp` that we used to compare strings earlier could have taken on an additional  argument  - how many characters to compare. So that if we wanted to compare only the first three charaters of the first_name variable we could have done...

  if (strncmp(first_name, "Joh", 3) != 0) return 1;


Thursday, November 19, 2015

Simple Dropdown Menu Button with Bootstrap

Bootstrap is a pretty cool framework that can be used in standard html web pages for some pretty impressive design options.
It's an open source git-hub project: Git-Hub Bootstrap

Here's an example of how easily it is to create a dropdown menu button with the bootstrap framework in html:




<head>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

</head>

<div id="sidebar" class="column-right">
<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
    <li><a href="#">HTML</a></li>
    <li><a href="#">CSS</a></li>
    <li><a href="#">JavaScript</a></li>
  </ul>
</div>
</div>

Tuesday, November 17, 2015

C++ Simple Example of Using Arguments

C++ is a great language, largely because of its mid-level functionality. It can accomplish things at the system level as well as the user level. Here's  a simple example of working with arguments, including a help message if no arguments are passed...

#include <iostream>
int main(int argc, char* argv[])
  {
    if (argc < 2) {
      std::cerr << "Usage: " << argv[0] << " NAME" << std::endl;
      return 1;
      }
    std::cout << argv[0] << "says hello, " << argv[1] << "!" << std::endl;
    return 0;
  }

A few things to note about the code above:
  • The number of arguments and the argument string is passed directly 'main'.
  • Even though the user is to pass one argument, we check to make sure there are two arguments passed. This is because the program itself is considered the first argument.
  • We return a '1' when an argument is not passed by the user, but only after displaying help to the user. This is to record that the program did not end properly.
  • We return a '0' to show the program was executed properly.


Friday, November 13, 2015

Arrays: Pushes and Pops

Arrays can be a very helpful way of dealing with groups of data. There are a variety of ways of working with arrays through different programming languages, some more complex than others, some more functional, etc.

I love how PHP works with arrays because it makes them easy to create and has some great functions for working with them. Pushes and pops are nice quick ways of adding or taking out members at the end of an array that many programming languages have. I'll be using PHP for my examples.

Push example:
$num_set = [1,2,3,4]; //creates  array $num_set
array_push($num_set, 5); // now array is [1,2,3,4,5]
Notice that one of the unique things with PHP is that you don't have to declare your variables. PHP does the work in identifying what kind of variable it is by seeing what  kind of data is stored in it. I find this quite convenient, though it has its drawbacks as well.
Pop example:
$num_set = [1,2,3,4];
array_pop($num_set); //$num_set is now [1,2,3]
But what if you wanted to deal with more than just the last member of an array? No problemo...that's where array_shift (similar to pop) and array_unshift (similar to push) come in...

array_shift example:
$num_set = [1,2,3,4];
array_shift($num_set); // now array is [2,3,4];
Note: This affects array keys and literal keys differently. The array keys shift with the array change while literal keys to not change.

array_unshift example:
$num_set = [1,2,3,4];
array_unshift($num_set, 0); // "0" is the index of the  array (counting starts with 0 in many programming  languages). Now array is [0,1,2,3,4];
Sometimes it's helpful to merge two arrays together. Depending on the outcome you are looking for you may want to only do a merge, or you may want  to also do a sort.

merge only example:
$random_1 = [38193, 83493, 11027, 34272];
$random_2 = [23115, 12372, 25168, 18307];
$random_all = array_merge($random_1, $random_2);
//result will be a combination of the two  arrays with random_2  following random_1 in the order that they are represented
merge with sort example:
$books_1 = ["The Idiot", "Crime and Punishment", "War and Peace"];
$books_2 = ["Gulliver's Travels", "A Tale of Two Cities", "Pilgrim's Progress"];
$books_all = array_merge($books_1, $books_2);

//at this point the books would be ordered in the position they appear from the first array followed  by the second array.

sort($books_all);

//now the books are listed alphabetically.
But what if you wanted "The Idiot" and "A Tale of Two Cities" to be sorted based on the second word  rather than the first? Well, an easy way to accomplish this  is to set your array up for such by putting the title in like: "Idiot, The" and "Tale of Two Cities, A"

Thursday, November 12, 2015

Reading a File with Python

Python's a pretty cool language. It's cross-system, you can complile the code to be an executable by bundling the files necessary to run the program together. Programs like "PyInstaller" help with this process.

It also has a pretty cool logo:
So, seeing how cool Python is (as i'm sure i've convinced you), check out this code for Python reading a file...

import sys
if __name__ == "__main__":
         # if len(sys.argv) > 1:
         if len("fileread.txt") > 1:
                 try:
                        #  f = open(sys.argv[1], "rb")
                        f = open("fileread.txt", "rb")
                 except:
                        # print "No file named %s exists!" % (sys.argv[1],)
                        print "No file named %s exists!" % ("fileread.txt",)
                 while 1:
                        t = f.readline()
                        if t == '':
                            break
                        if '\n' in t:
                            t = t[:-1]
                        if '\r' in t:
                            t = t[:-1]
                        sys.stdout.write(t + '\n')
                 f.close()

Wednesday, November 11, 2015

C++ Password with Asterisks

This is my first post! I plan on using this blog for various programming posts. From talking about concepts to actual code...who knows what might show up here!

This is C++ code for simple password check with asterisks to hide the password from onlookers.

#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int getch() {
    int ch;
    struct termios t_old, t_new;

    tcgetattr(STDIN_FILENO, &t_old);
    t_new = t_old;
    t_new.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &t_new);

    ch = getchar();

    tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
    return ch;
}



string getpass(const char *prompt, bool show_asterisk=true)
{
  const char BACKSPACE=127;
  const char RETURN=10;

  string password;
  unsigned char ch=0;

  cout <<prompt<<endl;

  while((ch=getch())!=RETURN)
    {
       if(ch==BACKSPACE)
         {
            if(password.length()!=0)
              {
                 if(show_asterisk)
                 cout <<"\b \b";
                 password.resize(password.length()-1);
              }
         }
       else
         {
             password+=ch;
             if(show_asterisk)
                 cout <<'*';
         }
    }
  cout <<endl;
  return password;
}


int main()
{
  const char *correct_password="null";

  string password=getpass("Please enter the password: ",true); // Show asterisks
  if(password==correct_password)
      cout <<"Correct password"<<endl;
  else
      cout <<"Incorrect password. Try again"<<endl;


  password=getpass("Please enter the password: ",false); // Do not show asterisks
  if(password==correct_password)
      cout <<"Correct password"<<endl;
  else
      cout <<"Incorrect password. Try again"<<endl;

  return 0;
}