incubus - an obsession in computing
  • rss
  • Home
  • Linux Apps
    • C&G 4250 Linux Extension
  • C&G 4240
    • Overview
    • Validate Program
    • Sort Program
    • Update / Merge Program
    • Report Program
    • Downloads
  • C&G 4250
    • Overview
    • Data Entry Program
    • Validate Program
    • Sort Program
    • Customer Update / Merge Program
    • Customer Report Program
    • Stock Update / Merge Program
    • Stock Report Program
    • Downloads
  • About
  • Contact

Linked Lists (Code)

inc | September 23, 2008 | 12:20 pm

Here is some code that may help with creating a linked list, and also adding and removing elements from it. The code below is for a double linked list, for a single linked list all you have to update is the next pointer as the previous pointer will not be needed (as that is the difference between the two types of list). I shall be using the following data structures throughout the following code examples.

/*-- Stock Master Record ---------------------------------------------------*/
typedef struct
{ char  key[7];
  char  partDesc[20];
  short suppCode;
  long  freeStk;
  short minStk;
  char  moveDate[7];
  float price;
}STOCK_REC;
/*-- Double Linked List ---------------------------------------------------*/
typedef struct STOCK_LIST
{ STOCK_REC rec;
  struct STOCK_LIST *prev; /*-- Pointer to the previous element --*/
  struct STOCK_LIST *next; /*-- Pointer to the next element     --*/
}STOCK_LIST;

Creation of a linked list:

Declare two global pointers that will be used to keep track of the start and end of the linked list (with a single linked list you should only need to to keep a record of the start of the list):

STOCK_LIST *RecFirst  = NULL; /*-- Pointer to start of the link list. --*/
STOCK_LIST *RecFinish = NULL; /*-- Pointer to finish of the link list --*/

Declare a pointer to a list element, this is used to allocate memory for each element in the list.

STOCK_LIST *recElement;

Now follows the actual code that allocates memory for a list element and than adds this element to the end of the list (updating the list pointers as it goes). This is not full code but will do as it says and maybe used in a complete program.

/*-- Allocate memory for the next element in the list --*/
if ((recElement = (STOCK_LIST *)malloc(sizeof(STOCK_LIST))) != NULL)
{ /*-- Populate stock list elements data --*/
  strcpy(recElement->rec.key,      "123456");
  strcpy(recElement->rec.partDesc, "A PART");
  strcpy(recElement->rec.moveDate, "220670");
  recElement->rec.suppCode = 'A';
  recElement->rec.freeStk  = 100;
  recElement->rec.minStk   = 50;
  recElement->rec.price    = 5.99;
  recElement->next         = NULL;

  /*-- If start of list is empty, set this element as the start --*/
  if (RecFirst == NULL)
  { /*-- This element is the start of the list --*/
    RecFirst = recElement;

    /*-- No element precedes this element --*/
    RecFirst->prev = NULL;
  }else
  { /*-- This element is now the final element --*/
    RecFinish->next  = recElement;

    /*-- The element prior to this one is the previous final element --*/
    recElement->prev = RecFinish;
  }
  /*-- Keep the end of list pointer up-to-date --*/
  RecFinish = recElement;

}else
{ printf("Error Creating Linked List - Memory allocation error!\n");
  exit(EXIT_FAILURE);
}

To start with the above may seem quite complicated but when you actually use the code and get used to shifting pointers you will become at ease and fully understand linked list creation. As with all things in life practice makes prefect (I’m far from that).

Inserting an element into a linked list:

If the list is in order then you will want the element inserted into the correct place within the list, to do this you must search the list and locate the element where you want the insertion to follow.

static STOCK_LIST *searchList (char *partNum)
{ STOCK_LIST *recList,
             *prevRec;
  int         res;

  for (recList=RecFirst, prevRec=NULL; recList!=NULL; recList=recList->next)
  { res = strcmp(recList->index.key,partNum);
    if (res == 0)
      return recList;
    else if (res > 0)
      return prevRec;

    /*-- Keep track of the previous record --*/
    prevRec = recList;
  }

  if (prevRec != NULL && prevRec != recFirst)
  { /*-- Element is to be inserted at the end of the list --*/
    return prevRec;
  }else
  { /*-- Element is to be inserted at the start of the list --*/
    return NULL;
  }
}

Now the recList pointer will either point to no where or the element where the insertion is to follow. If the pointer is NULL the element will be inserted at the start of the list. Here is some code that will hopefully show this in action.

/*-- Search the list for the given part number ----------------*/
/*-- Return the previous record if the part num is not found --*/
recList = searchList(partNum);

/*-- Allocate memory for new stock record element --*/
if ((newRec = (STOCK_LIST *) malloc (sizeof(STOCK_LIST))) != NULL)
{ /*-- Populate stock list elements data --*/
  strcpy(recElement->rec.key,      "123457");
  strcpy(recElement->rec.partDesc, "B PART");
  strcpy(recElement->rec.moveDate, "220670");
  recElement->rec.suppCode = 'A';
  recElement->rec.freeStk  = 100;
  recElement->rec.minStk   = 50;
  recElement->rec.price    = 5.99;
  recElement->next = NULL;

  /*-- Insert element in correct position of the list --*/
  if (recList == NULL)            /*-- Start of List --*/
  { newRec->next    = RecFirst;
    newRec->prev    = NULL;
    RecFirst->prev  = newRec;
    RecFirst        = newRec;
  }else if (recList == recFinish) /*-- End of list   --*/
  { RecFinish->next = newRec;
    newRec->prev    = RecFinish;
    newRec->next    = NULL;
    RecFinish       = newRec;
  }else                          /*-- Middle of list --*/
  { newRec->next    = recList->next;
    newRec->prev    = recList;
    recList->next   = newRec;
    newRec->next->prev = newRec;
  }
}else
{ printf("Error Adding to List - Memory allocation error!\n");
  exit(EXIT_FAILURE);
}

Notice the lack of code to handle if the element already exists within the list, this is quite straight forward to add so I will leave for you to complete.

Deleting an element from a linked list:

Again we must search the list for the correct element to remove, we use the same function as used above. Note again I have not included any code to deal with the element not being in the list.

/*-- Search the list for the given part number ----------------*/
/*-- Return the previous record if the part num is not found --*/
recList = searchList(partNum);

/*-- The record was found in the linked list --*/
if(!strcmp(recList->index.key, partNum))
{ zapRec = recList;

  if (recList == recFirst)        /*-- Remove first element --*/
  { recFirst = recList->next;
    recFirst->prev = NULL;
  }else if (recList == recFinish) /*-- Remove last element --*/
  { recFinish = recList->prev;
    recFinish->next = NULL;
  }else                           /*-- Remove middle element --*/
  { recList->prev->next = recList->next;
    recList->next->prev = recList->prev;
  }
  /*-- Free deleted elements memory --*/
  free(zapRec);
}
Comments
No Comments »
Categories
Code, Linux
Comments rss Comments rss
Trackback Trackback

Linked Lists (Diagrams)

inc | September 22, 2008 | 8:00 pm

OK here is a blast from the past … some diagrams that may help a little with the linked lists and their manipulation. These were originally drawn / written when I was studying for the City & Guilds 4250 course, I hope someone someday finds them of some use.

Insertion at the start of the List:
* Before Insertion into List
* After Insertion into List

Insertion at the end of the List:
* Before Insertion into List
* After Insertion into List

Insertion in the middle of the List:
* Before Insertion into List
* After Insertion into List

Deletion from the start of the List:
* Before Deletion from List
* After Deletion from List

Deletion from the end of the List:
* Before Deletion from List
* After Deletion from List

Deletion from the middle of the List:
* Before Deletion from List
* After Deletion from List

Comments
No Comments »
Categories
Linux, Tips & Tricks
Comments rss Comments rss
Trackback Trackback

Nautilus / GNOME Empty Trash

inc | September 18, 2008 | 6:59 pm

I had some trouble emptying my Trash with Nautilus/GNOME the other day. So here is the trick for removing stubborn items from your Trash (GNOME 2.22.3) if you ever experience the same:

sudo rm -Rf ~/.local/share/Trash/*

If the ‘File Operations’ window is also stuck displaying a non-moving progress bar then you will also need to perform the following action:

killall nautilus

Comments
No Comments »
Categories
Linux, Tips & Tricks
Comments rss Comments rss
Trackback Trackback

Secure Online Backup (GNOME Support)

inc | September 16, 2008 | 7:15 pm

Dropbox is pure magic, no it is honestly. Dropbox provides 2GB of free online storage with the added ability to keep your files synchronised across machines (and operating systems). The sync process is where I think the magic lies, it just works, try it for yourself if you do not believe me …

http://www.getdropbox.com

I have this installed on 1 Windows Machine and 2 Linux Machines, and files get sync instantly  with no user interaction necessary. The Linux version integrates nicely with the Nautilus file manager. You copy files (or delete / modify them) into the Dropbox directory and they get synced for you.

There is an online tour of the features provided by Dropbox, go take a look !!!

http://www.getdropbox.com/tour#1

For Gentoo follow these simple instructions on getting Dropbox installed:

Download the Dropbox source archive from the Dropbox site.
tar -xjvf nautilus-dropbox-x.x.x.tar.bz2
cd nautilus-dropbox-x.x.x
./configure
make
make install
killall nautilus

Dropbox

UPDATE: There appears to be an open Gentoo Bug and attached ebuild for the latest version of Dropbox.

UPDATE: If you are thinking about signing up to the free Dropbox server then please feel free to follow this link, https://www.getdropbox.com/referrals/NTE3NTk0ODk. By following the afore mentioned link you will receive an extra 250MB of storage.

Comments
No Comments »
Categories
Linux
Tags
Dropbox
Comments rss Comments rss
Trackback Trackback

C&G 4240 & G&G 4250 Content

inc | September 7, 2008 | 8:27 pm

You may have noticed that I have made a start at bringing the C&G 4240 and 4250 content (ZenithPaints) over to @incubus. This will help me keep that part of the @incubus family up to date, with a consistent look and feel to all the @incubus sites. I still have a bit of the content to move over, so until everything has been moved the ZenithPaints site will remain in tandem with the @incubus content.

I have also made a few improvements to the past posts (adding some download links and pictures where applicable).

I have managed to get my hands on an Acer Aspire One (A110) NetBook. This little machine now has Gentoo fully installed, I’ll be posting more on this little box of tricks soon. If in the mean time you require some information about this machine and Gentoo then please checkout the following site:

http://gentoo-wiki.com/Acer_Aspire_One_A110L

Update:

Added 4240 and 4250 download pages with all code now available for download. Also added the 4250 Linux Extension project complete with relevant downloads.

Comments
No Comments »
Categories
4240, 4250, Linux
Comments rss Comments rss
Trackback Trackback

NeoVI Green with Linux

inc | September 5, 2008 | 7:11 pm

With just a minor change to the supplied Linux Library for the NeoVI Blue, we have a Library that also supports the older NeoVI Green under Linux (over the RS232 port).

NeoVI Blue
NeoVI Green

Essentially it is a two line change to the supplied library, as the patch below shows:

diff -rup can_sniff-0.3/libintrepidcs/intrepidcs.c can_sniff-0.3_lm/libintrepidcs/intrepidcs.c
— can_sniff-0.3/libintrepidcs/intrepidcs.c 2005-04-11 19:59:06.000000000 +0100
+++ can_sniff-0.3_lm/libintrepidcs/intrepidcs.c 2008-09-05 08:33:05.000000000 +0100
@@ -45,11 +45,13 @@ struct ics_interface_type {
#ifdef linux
static const struct ics_interface_type Interfaces[] = {
{B2000000, “ValueCAN”, 0.000001, 0.065536}, //Linux needs these constants for cfsetispeed(3) and cfsetospeed(3)
+ {B57600, “neoVI”, 0.0000001, 0.104768},
{B3000000, “neoVI”, 0.0000016, 0.1048576}
};
#else
static const struct ics_interface_type Interfaces[] = {
{2000000, “ValueCAN”, 0.000001, 0.065536},
+ {57600, “neoVI”, 0.0000001, 0.104768},
{3000000, “neoVI”, 0.0000016, 0.1048576}
};
#endif

With this change in place the supplied can_sniff application (recompiled against the updated lib) works under Linux over the RS232 port. I do not require USB driver support with the NeoVI Green. I am happy I now have another working CAN Bus Sniffer (and soon to become an ECU simulator).

When using can_sniff with a NeoVI Green then the following command line argument must be given (example below shows the NeoVI being plugged into COM1 of the host PC):

can_sniff -p /dev/ttyS0

The following command line arguments are also supported by the CAN Sniff application (most of these are undocumented):

Options:
-p port                     Port that the neoVI is connected (/dev/ttyUSB0, /dev/ttyS0) -d Set Delta Flag.
-v                            Set Verbose Flag.
-f id:mask                Set a filter (18DAF12B:FF00FFFF).
-[0-9] “id: d1 d2 …” Add TX Message (-0 “123: 01 02 03 04″).

I have also patched can_sniff to monitor the MSCAN bus in addition to the HSCAN and SWCAN buses.

LibIntrepidcs NeoVI Green Patch
CAN Sniff (MSCAN) Patch

I would like to get the gtk+ application working, but have yet to get the time to have a look at this. It would be nice to have a GUI Linux CAN Bus Sniffer application (there’s nothing like watching hex data stream by…).

Comments
No Comments »
Categories
Linux, ODB2
Comments rss Comments rss
Trackback Trackback

SRR Kernel Module

inc | September 3, 2008 | 8:09 pm

Well I was doing some testing with the latest released Gentoo Kernel (2.6.26-gentoo-r1), and discovered that the Cogent SRRIPC (Send/Receive/Reply) Kernel Module that we use fails to compile with this kernel version (removal of the ‘create_proc_info_entry’ interface and a couple of other slight changes).

QNX SRR Architecture

The SRR module is essential if you are porting applications from QNX (4.2x) over to Linux (it makes the process quite painless too). The performance is extremely impressive (in fact our system runs faster under Linux than it does under QNX using the same hardware). The QNX IPC mechanism works really well and to be honest I like it a lot.

I have posted a basic patch on the Cogent Forums that resolves this slight problem, all seems well with testing so far.

SRRIPC 1.4.37 – 2.6.26 Kernel Patch

Comments
No Comments »
Categories
Linux
Comments rss Comments rss
Trackback Trackback

Search @incubus

Recent Posts

  • Compressed Hard Disk Image
  • It’s been along time …
  • T209 ECA Result
  • Old Linux Discs…
  • Panasonic CF-U1 / 2D Barcode Imager
  • Useful Gentoo Aliases
  • Hosting Provider Changed.
  • Linux – Merge AVI Files
  • Coders at Work
  • Remove OGA Office Not Genuine Notifications (KB949810)

Links

  • Demonoid
  • Engadget
  • Gentoo Linux
  • Gentoo Planet
  • Gentoo Universe
  • GNOME
  • GNOME Planet
  • ISO Hunt
  • OS News
  • Piratebay

Navigation

  • Register
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Archives

  • May 2010 (1)
  • February 2010 (1)
  • December 2009 (3)
  • November 2009 (2)
  • September 2009 (4)
  • August 2009 (1)
  • July 2009 (1)
  • May 2009 (2)
  • April 2009 (1)
  • February 2009 (2)
  • January 2009 (1)
  • December 2008 (1)
  • November 2008 (1)
  • October 2008 (3)
  • September 2008 (7)
  • August 2008 (16)

Categories

  • 4240 (1)
  • 4250 (1)
  • Code (8)
  • Linux (29)
  • ODB2 (2)
  • Open University (6)
  • Tips & Tricks (13)
  • Uncategorized (5)

Stats

Visits Today: 7
rss Comments rss design by jide powered by Wordpress get firefox
© Copyright 1999-2010 @incubus. All Rights Reserved. All trademarks acknowledged.
incubus.co.uk || incubus.mobi || rankinstine.co.uk