Pages

Saturday, August 7, 2010

Linux Commands

I am posting some Linux commands which might be use full for others

Create RAID on Linux

mdadm --create /dev/md0 –level=5 –raid-devices=3 /dev/sdb /dev/sdc /dev/sdd

show raid details

mdadm –- detail /dev/md0

Stop raid

mdadm –-stop /dev/md0

Remove raid

mdadm –-remove /dev/md0


Installing RPM

rpm –ivh blahblah.rpm


Upgrading rpm

rpm –uvh blahblah.rpm


Removing RPM

rpm –ev blahblah.rpm


Creating rpms


compile driver
rename blahblah_(uname –a)
tar –cvzf blahblah_prebuild.tgz blahblah_(uname –a)
rpmbuild –bb ‘target=i386’ abc.spec

To extract rpm

rpm2cpio any.rpm cpio -idv

To load usb modules

modprobe usb-storage
modprobe oxci_hcd
modprobe ehc_hcd


Search contents of the file in current folder recursively

find . xargs grep –n
  
Turning on hibernation in windows from command prompt

powercfg –h on


Compiling Linux source

make mrproper
make clean

make menuconfig

make all && make modules && make modules_install && make install

How to Applying patch

patch p1 < abc.patch

cd back up

cpio –icuvd < /dev/st0

To find current partition

df

Generate ctags

ctags –R .

To see the version of a module

modinfo –F version

Enabling scsi logs

sysctl -w dev.scsi.logging_level=0xffffffff


To see change log of a Linux source rpm

rpm -q --changelog -p kernel......rpm less


Creating tar

tar –cvzf abc.tar *

Uncompress tar

tar –xvzf abc.*

Checking Kernel Version

uname –a

64 bit or 32 bit

uname -m

To see os version

cat /etc/SuSE-release in sles

cat /etc/redhat-release in RHEL

Saturday, April 24, 2010

Binary Search tree with insert delete, print option C

#include
#include
struct node{
int val;
struct node *left;
struct node *right;
};

void insert(struct node**,int);
void insert_init(struct node**);
void inorder(struct node**);
void preorder(struct node**);
void postorder(struct node**);
void search_init(struct node**,struct node **,struct node **);
void search(struct node **,int,struct node**,struct node **);
void delete_init(struct node **,struct node**,struct node **);
void delete(struct node **,struct node **,struct node**);


int main()
{

struct node *root=NULL;
struct node *parent=NULL;
struct node *curnode=NULL;
int opt=0;

while(1){
printf("\n Select an option \n 1.Insert 2.Inorder 3. Preorder 4. Postorder 5.Seatch 6.delete 7.Exit\n");
scanf("%d",&opt);
switch(opt){
case 1:{ insert_init(&root);break; }
case 2:{ printf("\nInorder Traversal\n"); inorder(&root);break; }
case 3:{ printf("\nPreorder Traversal\n");preorder(&root);break; }
case 4:{ printf("\nPostorder Traversal\n");postorder(&root);break; }
case 5:{ search_init(&root,&parent,&curnode);break; }
case 6:{ delete_init(&root,&parent,&curnode);break; }
case 7:{ exit(0);}
}
}

}

void insert_init(struct node **root) //function toread insert value
{ //input: address of root
int val;

printf("Enter an integer value\n");
scanf("%d",&val);
insert(root,val);

}
void insert(struct node **root,int val) //function for insert
{ //input address of root and input value
if(*root == NULL){
*root=(struct node *)malloc(sizeof(struct node));
(*root)->val=val;
(*root)->left=NULL;
(*root)->right=NULL;
return;
}
if((*root)->val==val){
printf("\nValue Already exists in the tree\n");
return;
}
if((*root)->val > val)
insert(&(*root)->left,val);
else
insert(&(*root)->right,val);

}
void inorder(struct node **root) //function to perform inorder traversal
{ // input to function ,address of root
if(*root == NULL)return;
inorder(&(*root)->left);
printf(" %d",(*root)->val);
inorder(&(*root)->right);
}
void preorder(struct node **root) //function to perform preorder traversal
{
if(*root == NULL)return;
printf(" %d",(*root)->val);
preorder(&(*root)->left);
preorder(&(*root)->right);

}
void postorder(struct node **root) //function to perform postorder traversal
{
if(*root == NULL)return;
preorder(&(*root)->left);
preorder(&(*root)->right);
printf(" %d",(*root)->val);
}
void search_init(struct node **root,struct node **parent,struct node **curnode)
{
int searchitem;

*parent==NULL;
printf("Enter the search item\n");
scanf("%d",&searchitem);
search(root,searchitem,parent,curnode);
}
void search(struct node **root,int searchitem,struct node **parent,struct node **curnode) //function to perform search operation
{ //inputs: root address, search item parent
if(*root == NULL) {
printf("\nItem not Found\n");
*curnode=*root;
return;
}
if(searchitem == (*root)->val) {
printf("\nItem found in the tree\n");
*curnode=*root;
return;
}
*parent=*root;
if(searchitem>(*root)->val) {
search(&(*root)->right,searchitem,parent,curnode);
} else {
search(&(*root)->left,searchitem,parent,curnode);
}
}
void delete_init(struct node **root,struct node **parent,struct node **curnode) //function to read delete item
{
int item;
struct node *temp;

printf("\n Enter the item to delete");
scanf("%d",&item);
search(root,item,parent,curnode);
if(curnode==NULL)return;
delete(root,curnode,parent);
}
void delete(struct node **root,struct node **nodetodel,struct node **parent) //function to delete a node
{

struct node *tempparent;
struct node *swapnode;

if(((*nodetodel)->right == NULL )&&((*nodetodel)->left == NULL)) {
if(*parent==NULL) {
*root=NULL;
free(*nodetodel);
}else {
if((*parent)->val > (*nodetodel)->val) {
(*parent)->left=NULL;
}else {
(*parent)->right=NULL;
}
free(*nodetodel);
}
return;
}

if((*nodetodel)->right!=NULL) {

swapnode=*nodetodel;
tempparent=swapnode;
swapnode=swapnode->right;
while(swapnode->left != NULL) { //traverse to leftmost node
tempparent=swapnode;
swapnode=swapnode->left;
}
(*nodetodel)->val=swapnode->val;
*parent=tempparent;
if( (*parent)->val > swapnode->val ) {

(*parent)->left=NULL;
} else {
(*parent)->right=NULL;
}
if(swapnode->right != NULL)
(*parent)->left=swapnode->right; //new condition(for debug)
free(swapnode);
return;
}
if((*nodetodel)->left != NULL) {
swapnode=*nodetodel;
tempparent=swapnode;
swapnode=swapnode->left;

while(swapnode->right != NULL) { //traverse to right most node
tempparent=swapnode;
swapnode=swapnode->right;
}
(*nodetodel)->val=swapnode->val;
*parent=tempparent;
if( (*parent)->val > swapnode->val ) {
(*parent)->left=NULL;
} else {
(*parent)->right=NULL;
}
if(swapnode->left != NULL)
(*parent)->right=swapnode->left; //new condition (for debug)

free(swapnode);
return;
}

}

Simple Linked Stack

#include
#include
struct node{
int val;
struct node *next;
};

void push(struct node**);
void pop(struct node**);

int main()
{
struct node *head=NULL;
int opt=0;

while(1){
printf("Select an option \n 1.Push 2.Pop 3.Exit\n");
scanf("%d",&opt);
switch(opt){
case 1:{push(&head); break; }
case 2:{pop(&head);break;}
case 3:{ exit(0); }
}
}
}
void push(struct node **head) //to insert at begining
{ //input is address of head
struct node *ptr;

ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL){
printf("Allocation failed\n");
return;
}
printf("Enter an integer value\n");
scanf("%d",&ptr->val);
ptr->next=*head;
*head=ptr;

}


void pop(struct node **head) //to pop an element out
{ //input is address of head if(*head==NULL){
printf("\n Stack Empty\n");
return;
}
struct node *ptr;
ptr=*head;
printf("the poped value is%d\n",ptr->val);
*head=ptr->next;
free(ptr);
}

Simple Linked list

#include
#include
struct node{
int val;
struct node *next; // to store the next value
};

void insert(struct node**);
void delete(struct node**);
void traverse(struct node**);
int main()
{
struct node *head=NULL;
int opt=0;

while(1){
printf("Select an option \n 1.Insert 2.Delete 3.Exit 4. Traverse\n");
scanf("%d",&opt);
switch(opt){
case 1:{insert(&head); break; }
case 2:{delete(&head);break;}
case 3:{ exit(0);break; }
case 4:{traverse(&head);}
default:{printf("Invalid no");}
}
}
}


void traverse(struct node **head) // function to traverse
{ // input Address of head
printf("\n");
struct node *ptr;
if(*head==NULL){
printf("List Empty");
return;
}
ptr=*head;
while(ptr!=NULL){
printf(" %d",ptr->val);
ptr=ptr->next;
}
printf("\n");

}

void insert(struct node **head) // function to insert new node
{ // input address of head
struct node *ptr,*temp;

ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL){
printf("Allocation failed\n");
return;
}
printf("Enter an integer value\n");
scanf("%d",&ptr->val);
temp=*head;
if(temp==NULL){
ptr->next=NULL;
*head=ptr;
return;
}
while(temp->next!=NULL)temp=temp->next;
temp->next=ptr;
}

void delete(struct node **head) // function to delete a node
{ // input address of head
struct node *ptr=*head;
struct node *prevptr=NULL;
int val;
if(*head==NULL){
printf("List empty");
return;
}
printf("Enter the value to delete\n");
scanf("%d",&val);
while((ptr->val!=val)&&(ptr->next!=NULL)){
prevptr=ptr;
ptr=ptr->next;
}

if(ptr->val!=val){
printf("Value not found\n");
return;
}
if(prevptr==NULL){ //if selected element has no previous element
*head=ptr->next;
free(ptr);
return;

}
prevptr->next=ptr->next;
traverse(head);
free(ptr);
}

c search replace string example

#include
#include
int main()
{
char replaceitem[100];
char str[300];
char newstr[300];
char searchitem[100];
int flag=0;
int i=0;
int index=0;
int j=0;
int k=0;

printf("Enter String\n");
fgets(str,300,stdin);

printf("Enter Search item\n");
fgets(searchitem,100,stdin);

printf("Enter replacement item\n");
fgets(replaceitem,100,stdin);

for( i = 0;i < strlen(str); i++) {
for(j=0;searchitem[j]==str[i+j];j++){
if(searchitem[j+1]=='\0')
break;
}
if((j==strlen(searchitem)-1)){
for(k=0;k
newstr[index++]=replaceitem[k];
flag=1;
}
i+=strlen(searchitem)-2;
}
if(flag==1){
flag=0;
continue;
}
newstr[index++]=str[i];
}
newstr[index]='\0';
puts(newstr);
return 0;
}

combine 2, 32 bit no to create 64 bit no C

#define shift(val)val=val<<32
#define concat(val,num2)val=val|num2
#define call(val,num2) shift(val);concat(val,num2)
int main()
{
int num1;
int num2;
unsigned long long val;

scanf("%d",&num1);
scanf("%d",&num2);
val=(long long)num1;
call(val,num2);
printf("%lld",val);

}

How to change color of BOX2D objects

Well for changing box2d color you need to edit file b2World.as in BOX2D/Dynamics

In about 1060 line number


for (s = b.GetShapeList(); s; s = s.m_next)
{
if (b.IsStatic())
{
DrawShape(s, xf, new b2Color(1, 1,1), core);
}
else if (b.IsSleeping())
{
DrawShape(s, xf, new b2Color(0.0, 0.0, 0.9), core);
}
else
{
DrawShape(s, xf, new b2Color(0.8, 0.7, 0.5), core);
}
}

How access dll from flex

As far as i know there is no direct method through which you can
access dll files from flex.How ever one thing you can do is to
create webservice in asp .net and call that webservice from flex program.
You can do the same with webservice, httpservice, Remoting (using remote objects)


The best example i can find in adobes site here
http://www.adobe.com/devnet/flex/articles/communicating_flex_dotnet.html
communicating_flex_dotnet_06.html

you can use fluorinefx http://www.fluorinefx.com/ for flex .net remoting
in addition you can use weborg

http://www.themidnightcoders.com
example :

http://www.adobe.com/devnet/flex/articles/communicating_flex_dotnet_06.html
httpservice (For simple data transmission)
webservice (Datain Xml Format)
Remoting (Pass objects)

Another method through which an swf can call a dll is through External interfaces.
You can load the swf into your .net program. Then using external interface swf can communicate with c#.
I found a good Example here

Here is an another method using ActiveX component by srinivas

here

ttp://livedocs.adobe.com/flex/3/html/help.html?content=19_External_Interface_10.html

Traversing and adding file structure to a tree node

using System.Drawing;
using System.Text;
using System.Threading;
using System.IO;
public void ListDir(string src, TreeNode d){
try{
DirectoryInfo dinfo = new DirectoryInfo(@"" + src);
FileInfo[] finfo = dinfo.GetFiles();
for (int j = 0; j < finfo.Length; j++)
{
d.Nodes.Add(@"" + finfo[j].Name);
}

DirectoryInfo[] dname = dinfo.GetDirectories();
TreeNode[] treend = new TreeNode[dname.Length];
for (int i = 0; i < dname.Length; i++)
{
treend[i] = new TreeNode(dname[i].Name);
d.Nodes.Add(treend[i]);
}
for (int i = 0; i < dname.Length; i++)
{
ListDir(dname[i].FullName, treend[i]);
Thread.Sleep(5);
}
}

Listing Logical Drives

DriveInfo[] dinf = DriveInfo.GetDrives();
int i = 0;
while (i < dinf.Length)
{
MessageBox.show(dinf[i]);
}

Saturday, April 17, 2010

Create XML file using C#

using System.Data;
using System.Drawing;
using System.Xml;using System.IO;
//codeXmlTextWriter xmlWriter = new XmlTextWriter("temp.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("root");
xmlWriter.Close();
// creation complete
doc = new XmlDocument();doc.Load("temp.xml");
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "firstnode", ""); // creating NodenewNode.InnerText = “Content”; // set your contentdoc.DocumentElement.AppendChild(newNode); //Adds a Node
doc.Save("temp.xml");

Dynamically add Picture box using C# delegate

The below code is just abstract just to show simple use of delegate
using System.Data;
using System.IO;
using System.Drawing; using System.Text; using System.Threading; public partial class ImageView : Form{
Mydel a;
public delegate void Mydel(PictureBox p); public ImageView(){ InitializeComponent(); }
private void ImageView_Load(object sender, EventArgs e){
a = new Mydel(Add); Thread t = new Thread(load); t.Start(); }
public void load(){
DirectoryInfo dinfo = new DirectoryInfo(@"F:\Mypics"); FileInfo[] finfo = dinfo.GetFiles(); Graphics g = panel1.CreateGraphics(); int x = 20; int y = 20; for (int i = 0; i if (Image.FromFile(finfo[i].FullName) != null){
PictureBox p = new PictureBox(); p.Image = Image.FromFile(finfo[i].FullName);
p.SizeMode = PictureBoxSizeMode.StretchImage;
p.Left = x; p.Top = y;
p.ImageLocation = finfo[i].FullName;
p.Width = 140; p.Height = 90;
Object[] obj = new Object[1];
obj[0] = p;
Controls[Controls.IndexOf(panel1)].Invoke(a, obj); x += 150;
if (x > this.Width-100){
y += 100; x = 20; }
} if (i > 35) break;
}
}
public void Add(PictureBox p) {
panel1.Controls.Add(p);
}
}
}

Function to retrieve RAM SIZE in MB C#

This is the sample code to retrieve total ram size of system using WMI (Windows Management Instrumentation) well it’s not perfect but it works
Name spaces
using System; using System.Management; using System.Collections.Generic;
using System.Text;
public int getRAMSize() {
int total=0;
try {
ConnectionOptions connection = new ConnectionOptions();
ManagementScope scope; scope = new ManagementScope("root\\CIMV2", connection); ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem"); ManagementObjectSearcher searcher =new ManagementObjectSearcher(scope, query);
foreach (ManagementObject queryObj in searcher.Get(){
total = Convert.ToInt32(queryObj["TotalPhysicalMemory"].ToString()); }
total = total / 1024; }
catch {} }
return total;
}

Welcome to my Blog

Hai all I am abhilash,
software programmer from India. I am onto programming from quite a time now. I am posting all these because I have a bad memory Previously I had a blog codemanual I forget the password of that itself so i am re pubishing those documents. Later I thought of putting in blogs. So that I can share with you all whatever I know. I will try to give small code samples and all but I am not guaranteeing its correctness because still I am studying and I am careless and less worries about implementation. All the code given are simple and abstract . But I am sure even beginners can figure out what I ment, You can use the code at your own risk.