Cteate Stack in C++ using template

Stack is a data structure that allows access to only one data item at a time. If you remove this data item from the stack you can access to the next to last data item of the stack. Also stack perform a Last-In-First-Out(LIFO) operation on the stack. You may find more on stack here.

I will explain basic stack operations and then let's move to code.

Push - Insert a data item to the top of the stack.

Pop - Remove a data item from the top the stack.

Peek - Retrieve the data item from the top of the stack.

Other stack operations can be understand by just looking at the code simply.




This is the stack created by using template of C++. So that you can set any data or class type for stack data. Further more this post will answer the question, How to create template stack in C++?

The Stack.h source file is,

#include <stdlib.h>

#ifndef STACK_H_
#define STACK_H_

template<class T> class Stack{

private:
    unsigned int maxSize;
    T *stackData;
    int top;

public:
    Stack(int size){
        stackData = new T[size];//to hold the T ‌type data items
        top = -1;//no items on the stack
        maxSize = size;//set maximum size that stack can hold
    }


    virtual ~Stack(){}

    int count(){
        return top + 1;
    }


    bool isEmpty(){
        return top == -1 ? true : false;
    }


    bool isFull(){
        return top == maxSize - 1 ? true : false;
    }


    T* peek(){
        if(!isEmpty())//check for empty
            return &stackData[top - 1];
    }


    T* pop(){
        if(!isEmpty()){
            top -= 1;//decrease the top by 1 to indicate the delete
            return &stackData[top];//return deleted item
        }
        return NULL;
    }


    void push(T* item){
        stackData[top++] = *item;//insert to data array and increase the top by one
    }
};


#endif /* STACK_H_ */

Then the demonstration code,

#include <iostream>
#include "Stack.h"

using namespace std;

int main() {

    int data;
    Stack<int> intStack(10);//create integer stack with maximum 10 items

    //insert data to stack
    data = 1;
    intStack.push(&data);
    data = 2;
    intStack.push(&data);
    data = 3;
    intStack.push(&data);
    data = 5;
    intStack.push(&data);

    //get top item
    cout << *intStack.peek() << endl;

    intStack.pop();
    intStack.pop();

    //get top item
    cout << *intStack.peek() << endl;
   
    return 0;
}


I expect this will help anybody who want to create stack with the template!

Continue Reading...

Style source codes on the web page

Is there any way to change style of the source code that you include in the web page with simple method? Yes, there is. If you read my previous posts such as hibernate operations, ant build file, you can see the way my source codes are presented. Key words, variable names, and class names, etc are formatted according to the language that source code belongs.

How did I formatted these text on the web page? Did I change font color of each words on the text? No there is a easy method to do that. Thanks to google code prettify on google code.

This is the method that you should follow to format your source code snippets in an html page.

You can simply download their zip file and add those source file to your web page by uploading to the hosting server.

But I will explain an easy way to do that using online resources itself without downloading and uploading.

Step 1
Okay first you have to add reference CSS and JavaScript file to your web page's head section. Copy following code to head section on your web page.

<link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css' rel='stylesheet' type='text/css'/>
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js' type='text/javascript'/>

If you are a google blogger you should copy the above code to blog's layout head section.
Here we use their online resource of CSS and JavaScript file to load styles and functions to our web page.

Step 2
Now just only one initialization for our code formatter. Copy the following code to body tag on your page.
You should use the layout as same as previous step, if you are a google blogger.

onload="prettyPrint()"

As an example

<body onload="prettyPrint()">

Step 3
Now just surround your text which you want to format with code or pre tag and set the Prettier class to specify the language. that is all. following example show you how to do that.

Example -
Normal text of your source code is,

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        //read from System.in = normally keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
        //read the inputs
        x=keyboard.nextInt();
    }
}

Now surround this text with code tag and particular style on your HTML source code.

<code class="prettyprint lang-java">
//This is JAVA source code<br />
import java.util.Scanner;<br />
class keyboardDemo {<br />
&nbsp;&nbsp; public static void main(String[] arg) {<br />
&nbsp;&nbsp; &nbsp;&nbsp; //read from System.in = normally keyboard<br />
&nbsp;&nbsp; &nbsp;&nbsp; Scanner keyboard = new Scanner(System.in);<br />
&nbsp;&nbsp; &nbsp;&nbsp; System.out.print("Enter two integers : ");<br />
&nbsp;&nbsp; &nbsp;&nbsp; int x;<br />
&nbsp;&nbsp; &nbsp;&nbsp; //read the inputs<br />
&nbsp;&nbsp; &nbsp;&nbsp; x=keyboard.nextInt();<br />
&nbsp;&nbsp; }<br />
}<br />
</code>

Now you can see the formatted source code as follows on your web page,

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        //read from System.in = normally keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
        //read the inputs
        x=keyboard.nextInt();
    }
}


If you need you can surround code with a div tag to change background color and wrapping style.

My div tag looks like this,

<div style="background: none repeat scroll 0% 0% rgb(209, 206, 206); border: 2px none; overflow: auto; white-space: nowrap;">

</div>

Now final styled text is,

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        //read from System.in = normally keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
        //read the inputs
        x=keyboard.nextInt();
    }
}

Code prettify support several language for style them. To change the language that you need to style just change the class of the style to your language.


For JAVA,

<code class="prettyprint lang-java">

Example -

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
    }
}

For xml,

<code class="prettyprint lang-xml">

Example -

<project default="final" name="yourProjectName">
           <target name="final" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>
</project>


For HTML,

<code class="prettyprint lang-html">

Example -

<!DOCTYPE html>
<html>
    <body>
        <h1>My First Heading</h1>
        <p>My first paragraph.</p>
    </body>
</html>

You can refer this page for further more support languages. So that's all have fun with this!



Continue Reading...

Migrating from Visual SourceSafe to Team Foundation (VSS to TFS)

This post will describe how to totally migrate a Visual Sorcesafe database to Team Foundation Server. After migration the database you will have all the details on VSS database such as your source codes, users, and version history, etc...

There are some prerequisites which you should consider before start the migration process. Those are,
  • Team Explorer on Visual Studio.
  • Log on to the computer by using administrative credentials.
  • SQL Server Express.
  • Visual SourceSafe 2005 or later versions.
  • The Visual SourceSafe database that you want to analyze.
  • The administrator’s password for the Visual SourceSafe database.
And also some permissions, Therefore To perform the procedures in this post, you must have the following permissions.
  • In the Visual SourceSafe database that contains the data that you want to migrate, you must know the password of the Admin account.
  • On the migration machine (the machine where Visual Studio is installed), you must be a member of the Administrators group.
  • On the database that VSSConverter uses, you must have "create database" permission.
  • Be a member of the sysadmin server role for SQL Express. (By default, you are a member of the sysadmin server role if you are a member of the Administrators security group on the computer where SQL Express is installed.)
  • On your application tier for Team Foundation, you must be a member of the Team Foundation Administrators security group. For more information, see Team Foundation Server Permissions on MSDN.
Basically there are 3 main steps involved on migration.
  1. Preparing the Visual SourceSafe Database
  2. Analyzing the Projects for Migration.
  3. Migrating SourceSafe Project Folders to TFS
1. Preparing the Visual SourceSafe Database
This step also can be break down into three steps.

  1. Ask all database users to check in their files.
  2. Create a backup copy of your Visual SourceSafe Database to migrate.
  3. Use the Visual SourceSafe Analyze utility to locate and fix data integrity issues in the database.
a. Ask all database users to check in their files.
You should make sure that all the users are not loged-in to the VSS and all the files are checked in to the VSS.

So you can check loged in users from "Microsoft Visual SourceSafe Administration" tool. (Start --> All programs --> Microsoft Visual Sorcesafe --> Microsoft Visual SourceSafe Administration).

Also you can list all the check out files with the checked out user by issuing following command on cmd.
Here we are using ss.exe which is supplied by VSS therefore you should change the directory to VSS installation path.
Ex:- cd "C:\Program Files\Microsoft Visual SourceSafe"
If you need user wise list, type following command on cmd
ss status -R -Umilinda > "C:\Users\milinda\Desktop\VSS Checkouts\milindaCheckouts.txt"
This command will save all the files those checked out by milinda to the C:\Users\milinda\Desktop\VSS Checkouts\milindaCheckouts.txt file.
If you need all the checked out files list, type following command on cmd
ss status -R > "C:\Users\milinda\Desktop\VSS Checkouts\allCheckouts.txt"
This command will save all the files those checked out by all users to the C:\Users\milinda\Desktop\VSS Checkouts\milindaCheckouts.txt file.

b. Create a backup copy of your Visual SourceSafe Database to migrate.
Make sure that no one is using the database and that Analyze will not begin to run while you are backing up the database. To backup the VSS database just copy the following folders and files which are located on sorcesafe's database path. (You may find this database path from Visual SourceSafe Explorer File --> Open sorcesafe databse or press Ctrl+P).
  • \DATA
  • \Temp
  • \USERS
  • user.txt
  • srcsafe.ini
When you follow this procedure, you can do a full restore of the database by replacing the existing Users, Temp and Data folders as well as the Users.txt and Srcsafe.ini files with the copied versions.
You can also use this procedure to move the database to another location by placing the copied files into a new folder. To open the database, on the File menu in the Visual SourceSafe Explorer, click Open SourceSafe Database to browse to the new location.


c. Use the Visual SourceSafe Analyze utility to locate and fix data integrity issues in the database.

Just run analyze.exe on command prompt as “analyze -F -V3 <sourcesafe data directory>” which is located onC:\Program Files\Microsoft Visual SourceSafe”

-F stands for fix, This will automatically repairs inconsistencies and corruption that are detected, as long as no users are logged in.

-V3 to displays all errors.

More command line options can be found here.

Remember you should give the data path (\DATA) as argument not the database path which you found from Visual SourceSafe Explorer.

As my experience it will take 3 hours to 4 hours time duration to complete the analysis on 4.2GB database. Somehow this duration depends on errors on your database also.

Now preperation of Visual SourceSafe Database is finished. Let's move to next step.

2. Analyzing the Projects for Migration.

This process is based on xml input file. After running this step you will get an user mapping file and analyze report file which are needed for next step.

VSSConverter.exe tool is used to analyze the VSS projects that located on “C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE”. But still you can be invoked it from the Visual Studio command prompt. Or simply cd to the above path from cmd and follow the following commands.

My recommendation is copy VSSConverter.exe and VSSConverter.exe.config file to a different location and perform following commands.

VSSConverter.exe accepts following inputs to perform its operations
  • A path of the folder that contains the srcsafe.ini file for the Visual SourceSafe database under migration.
  • An XML-based file that contains settings.
This path you may find on Visual SourceSafe Explorer (File --> Open sorcesafe databse or press Ctrl+P)

The XML-based file format is,


<?xml version="1.0" encoding="utf-8"?>
<SourceControlConverter>
      <ConverterSpecificSetting>

            <Source name="VSS">
                  <VSSDatabase name="VSSDatabase_name"></VSSDatabase>
            </Source>

            <ProjectMap>
            <Project Source="$/FolderA"></Project>
            <Project Source="$/FolderB"></Project>
            </ProjectMap>

      </ConverterSpecificSetting>

      <Settings>
       <Output file="Analysis.xml"></Output>
     </Settings>
</SourceControlConverter>

  • Modify the VSSDatabase_name to point to the path to your VSS database.
  • In the <projectmap> section, specify the Visual SourceSafe folders to analyze. 
    • To analyze the whole database, use <Project Source="$/"></Project>
  • Save above XML file as settingsAnalyze.xml to the directory where VSSConverter.exe and VSSConverter.exe.config file located.
Now you can run the converter tool to analyze VSS projects. The user who is performing the analysis must be a system administrator for SQLExpress. By default, the user who installs Visual Studio will receive the required permissions for SQLExpress.
  • cd to directory where VSSConverter.exe and VSSConverter.exe.config file located and run following command
    • VSSConverter Analyze settingsAnalyze.xml
  • When you are prompted, provide the Visual SourceSafe administrator password.
The converter tool displays the status of the pre-conversion analysis. When it is completed, it generates a report (VSSAnalysisReport.xml) and a user mapping file (usermap.xml), and saves them in the current directory.

You can use this user mapping file to map Visual SourceSafe users to Team Foundation users.

3. Migrating SourceSafe Project Folders to Team Foundation Server


The converter also needs an xml file. It is most similar to settings.xml file which is created at analyze step. Therefore,

  1. Copy settingsAnalyze.xml file content and save it as settingsMigration.xml. (Ensure that the system drive has sufficient space to hold the largest file size under migration.)

  2. Change the settings file as follows.
    • In the <ProjectMap> sections, for each Visual SourceSafe folder, add the destination folders in TFS. This destination should be already created on TFS.
      <Project Source="$/FolderA" Destination="$/Team_ProjectA"> </Project>

    • To migrate everything in your Visual SourceSafe database, insert the following XML instead.
      <Project Source="$/" Destination="$/Team_Project/"> </Project>

    • Under the <Settings> section, add a <TeamFoundationServer> section, and specify the name, port, and protocol for the Team Foundation Server to which you are migrating. Use the following format. Here you should set the collection to a collection that you created on TFS insted of 'DefaultCollection'.
      <TeamFoundationServer name="TFS_server_name" port="port_number" protocol="http" collection="tfs/DefaultCollection"></TeamFoundationServer>

    • Remove <Output file="Analysis.xml"></Output> from the <Settings> section, or rename the output file to "migrationAnalysis.xml".
  3. Overall structure of a migration settings file as follows

    <?xml version="1.0" encoding="utf-8"?>
    <SourceControlConverter>
       <ConverterSpecificSetting>
          <Source name="VSS">
             <VSSDatabase name="VSSDatabase_name"></VSSDatabase>
             <UserMap name="c:\Migrate\Usermap.xml"></UserMap>
          </Source>
          <ProjectMap>
             <Project Source="$/FolderA" Destination="$/TeamProjectA">
             </Project>  
             <Project Source="$/FolderB" Destination="$/TeamProjectB/ProjectB">
             </Project>
          </ProjectMap>
       </ConverterSpecificSetting>
       <Settings>

          <TeamFoundationServer name="server_name" port="port_number" protocol="protocol" collection="tfs/DefaultCollection"></TeamFoundationServer>

        </Settings>
    </SourceControlConverter>

  4. Edit the Source Control Migration User Mapping File
    If you want to map user name to TFS from VSS as it is, you can keep usermapping.xml file remain unchanged and go to next step.

    The analysis phase generated a user map file. The user map file consists of all Visual SourceSafe users who have performed any source control operation on the folders that you specified for the migration. You use this file to map Visual SourceSafe users to Team Foundation users.

    For each Visual SourceSafe username you want to map, add a valid Windows user name or Team Foundation Server user name in To field, as shown in the following example. The ADMIN user in VSS should be mapped to a real user in TFS, typically the project administrator. Users in VSS who are no longer with the company should be mapped to existing users.

    For example,

    <?xml version="1.0" encoding="utf-8"?>
    <UserMappings>
        <UserMap From="Admin" To="MYDOMAIN\Jennifer"></UserMap>
        <UserMap From="guest" To="TestAlias1"></UserMap>
        <UserMap From="Jane" To="Jane"></UserMap>
        <UserMap From="Mike" To=""></UserMap>
    </UserMappings>

    If you provide a user name mapping similar to <UserMap From="Mike" To=""></UserMap> where "Mike" is a valid Windows user name, Team Foundation maps "Mike" to "MYDOMAIN\Mike," where MYDOMAIN is the default domain.

  5. Before you run the following command you should create the team project as you define on settingsMigration.xml file. You can refer this to create new team project

  6. Execute following command on cmd to migrate the VSS database to TFS as you define in above xml files.
              VSSConverter Migrate settingsMigration.xml

  7. When you are prompted, provide the password for the Visual SourceSafe admin user.

So that is it.........
After long post, you may have a totally migrated TFS database from VSS with all the VSS data such as source files, histories, users, etc..

The whole process takes 6-7 hours time duration to migrate 4.2GB VSS database. But this may change up to your database size, users, histories, network speed, etc.

I apologize, If this post is getting too long but these are the basic step that you should follow to migrate. Have a nice day thanks.
Continue Reading...