Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 29th, 2006, 3:08 AM   #1
jazz
Newbie
 
Join Date: May 2006
Posts: 18
Rep Power: 0 jazz is on a distinguished road
Sorting

Hi,

I need to sort a list of numbers, which has 3 fileds - layerCode, subCode and BrandCode, in the way we see in excel sheet, sort in Acsending order, first by LayerCode, then by Subcode and then by BrandCode.

The below is a program which sorts the list only by LayerCode, I need to modify it in such a way that it sorts as mentioned above. Can anyone please help me??

#include "stdafx.h"

FILE	*fp_dump;

/*----- Layer -----*/
/* layer Record*/
typedef struct {
    USHORT          usCode;         	/*LayerCode*/
    USHORT          usSubCode;          	/*SubCode*/
    USHORT          usBrandCode;      	/*BrandCode*/
    USHORT          usStartY;           	/*Latitude*/
    USHORT          usStartX;           	/*Longitude*/
    UINT                uiAR_DA;            	/*Offset*/
    USHORT          usCellSize;         	/*Cell Size*/
    USHORT          usDS;               	/*Relative offset*/
    USHORT	    usKey;		/*SortKey*/
} stRec_t;

/*Layer*/
typedef struct {
    USHORT          usRecCnt;           /*Number of DataRecord*/
    stRec_t     *pstRec;        /*Layer Record*/
} stLayer_t;


static int iGetLayerFile( char*, stLayer_t* );
static int iLayerCodeSort(const void *, const void * );
static int iWriteLayerFile( char *, stLayer_t * );

/******* Sample main function ********/
int main( int argc, char **argv ) {

	int	 i,j,k,l,m,sMax,dMax;
	int    cnt;

  	SearchFiles_T stSearch;
  	SearchFiles _T stSearchFiles;
  	stLayer_t stILF;

	DIR* dir;
		
	char   acFileName[128],cmd[128];
	char   *temp, *pch;

	char   acILFDir[128];                
	char   acNewILFDir[128];
  	char   dirPath[128];         

	if (argv[1] == NULL || argv[2] == NULL)
	{
	    printf("\n<Usage>\nLayerFileSort source dest\n\n");
	    exit( NG );
	} 

	sMax = strlen(argv[1]);
  	dMax = strlen(argv[2]);
	memcpy(acILFDir, argv[1],sMax+2);
 	memmove(acILFDir+sMax,"/\0",2);
	memcpy(acNewILFDir, argv[2],dMax+2);
  	memmove(acNewILFDir+dMax,"/\0",2);
 
	if ((fp_dump=fopen("LayerFile.txt", "w")) == NULL) {
        		printf( "LogFile Open error\n" );
        		exit( NG );
	}

  	memset(&stFinds, 0, sizeof(SearchFiles_T));

	/*Search ILF directories*/
    	if ((FindFiles(acILFDir, &stFinds)) == NG) {
        		exit( NG );
    	}
    	for (i=0 ; i<stFinds.iFileCnt ; i++) {
		memset(&stFindsFiles, 0, sizeof(SearchFiles _T));
		
		/*Search ILF files*/
		if ((FindFiles(stFinds.pcPathName[i], &stFindsFiles)) == NG) {
	        	exit( NG );
		}
		for (j=0 ; j<stFindsFiles.iFileCnt ; j++) {
	
			printf("%5d: %s\n", i, stFindsFiles.pcPathName[j] );
			
			/*get ILF*/
			if( iGetLayerFile( stFindsFiles.pcPathName[j], &stILF ) != OK ){
				printf("ILF reading error : %s\n",stFindsFiles.pcPathName[j] );
				exit( NG );
			}

			/*sort ILF*/

			qsort(stILF.pstRec,stILF.usRecCnt,sizeof(stRec_t),iLayerCodeSort);

			/*get filename*/
			memcpy( acFileName, acNewILFDir,strlen(acNewILFDir));

			for ( m = (int)strlen ( acNewILFDir ),l = (int)strlen ( acILFDir ); 
				l < (int)strlen( stFindsFiles.pcPathName[j] ); l++, m++ )
				acFileName[m] = stFindsFiles.pcPathName[j][l];

			acFileName[m] = '\0';
		
			temp = (char *)malloc(strlen(acFileName)+1);
			if ( NULL == temp )
			{
				printf ("temp malloc error!");
				exit (NG);
			}

			memcpy(temp,acFileName,strlen(acFileName)+1); 
			pch = (char *)malloc(strlen(acFileName)+1);
			if ( NULL == pch )
		  	{
				printf("pch malloc error!");
				exit (NG);
			}
			memcpy(pch,acFileName,strlen(acFileName)+1);
			pch = strrchr(temp,'/');
			cnt = pch - temp + 1;

			memcpy(dirPath,temp,cnt);
			
			dirPath[cnt] = '\0';

  			dir = opendir(dirPath);
			if (!dir) 
			{
				  sprintf(cmd,"mkdir -m 755 -p %s",dirPath);  
				  printf("Making directory :%s\n",dirPath);  
				/*	  printf("cmd:%s\n",cmd);  */
				  system (cmd);           
			} 
			
			else closedir(dir); 
		 
			/*write ILF(finshed sortting)*/                              
			if( iWriteLayerFile ( acFileName, &stILF ) != OK ){
				printf("ILF writing error : %s\n",acFileName );
				exit( NG );
			}
		
			/*		dump list */
			for ( k = 0; k < stILF.usRecCnt; k++ ){
fprintf(fp_dump, "%s,%d,%d,%d,0x%04x,0x%04x,0x%04x,0x%02x,0x%02x,%d\n",
				stFindsFiles.pcPathName[j],
				stILF.pstRec[k].usCode,
				stILF.pstRec[k].usSubCode,
				stILF.pstRec[k].usBrandCode,
				stILF.pstRec[k].usStartX,
				stILF.pstRec[k].usStartY,
				stILF.pstRec[k].uiAR_DA,
				stILF.pstRec[k].usCellSize,
				stILF.pstRec[k].usDS,
				stILF.pstRec[k].usKey);
			}     

			if(stILF.usRecCnt != 0) free (stILF.pstRec);		
		}
	    FindFree(&stFindsFiles);
	}
    	FindFree(&stFinds);		
	fclose(fp_dump);
 
	free(temp);
	free(pch);
	printf("Done!\n");
 	exit( OK );
}
/*======================================================================
     FUNCTION     : Get LayerFile
     INPUT        : char*			-FileName
					stLayer_t	-Layer
     RETURN       : OK / NG
======================================================================*/
static int iGetLayerFile( char *acFileName, stLayer_t *pstILF)
{
	FILE	*fp;
	int	  i,j;
	USHORT	usSortKey[SORT_REC] = {9126,7328,7373,7315,7387,7314,7313,7369,9999,7395,7311,7321};
	
	pstILF->usRecCnt = 0;

	if ((fp=fopen(acFileName, "rb")) == NULL) {
		return ( NG );
	}
	fread(&pstILF->usRecCnt, 2, 1, fp);  
    	if( pstILF->usRecCnt != 0 ){
        	pstILF->pstRec =
                (stRec_t*)calloc(pstILF->usRecCnt,sizeof(stRec_t) );
	}

	for( i = 0; i < (int)pstILF->usRecCnt; i++ ){
		fread(&pstILF->pstRec[i].usCode, 2, 1, fp);			/* Layer Code*/
		fread(&pstILF->pstRec[i].usSubCode, 2, 1, fp);		/* Sub Code*/
		fread(&pstILF->pstRec[i].usBrandCode, 2, 1, fp);		/* Brand Code*/

		fread(&pstILF->pstRec[i].usStartY, 2, 1, fp);		/* Lat*/
		fread(&pstILF->pstRec[i].usStartX, 2, 1, fp);		/* Long*/
		fread(&pstILF->pstRec[i].uiAR_DA, 4, 1, fp);		/* Offset*/
		fread(&pstILF->pstRec[i].usCellSize, 2, 1, fp);		/* CellSize*/
		fread(&pstILF->pstRec[i].usDS, 2, 1, fp);			/* RelativeOffset*/

		pstILF->pstRec[i].usKey = 0;					/* Sort Key */
		for ( j = 0; j < SORT_REC; j++ ){
			if( pstILF->pstRec[i].usCode == usSortKey[j] ){
				pstILF->pstRec[i].usKey = j + 1;		/* Sort Key*/
				break;
			}
		}
	}
  
	fclose( fp );

	return( OK );
}
/*======================================================================
     FUNCTION     : Write LayerFile
     INPUT        : char*			-FileName
					stLayer_t	-Layer
     RETURN       : OK / NG
======================================================================*/
static int iWriteLayerFile( char *acFileName, stLayer_t *pstILF)
{
	FILE	*fp;
	int		i;
	
	if ((fp=fopen(acFileName, "wb")) == NULL) {
		return ( NG );
	}
	fwrite(&pstILF->usRecCnt, 2, 1, fp);  	
	for( i = 0; i < (int)pstILF->usRecCnt; i++ ){
		fwrite(&pstILF->pstRec[i].usCode, 2, 1, fp);			/* Layer Code*/
		fwrite(&pstILF->pstRec[i].usSubCode, 2, 1, fp);		/* Sub Code*/
		fwrite(&pstILF->pstRec[i].usBrandCode, 2, 1, fp);		/* Brand Code*/
		fwrite(&pstILF->pstRec[i].usStartY, 2, 1, fp);		/* Lat*/
		fwrite(&pstILF->pstRec[i].usStartX, 2, 1, fp);		/* Long*/
		fwrite(&pstILF->pstRec[i].uiAR_DA, 4, 1, fp);		/* Offset*/
		fwrite(&pstILF->pstRec[i].usCellSize, 2, 1, fp);		/* CellSize*/
		fwrite(&pstILF->pstRec[i].usDS, 2, 1, fp);			/* RelativeOffset*/
		
	};
	fclose( fp );

	return( OK );
}
/*======================================================================
     FUNCTION     : iLayerCodeSort
     INPUT        : stLayer_t	-Layer
					stLayer_t	-Layer
     RETURN       : OK / NG
======================================================================*/

static int iLayerCodeSort(const void *data1, const void *data2 )
{
	stRec_t     *pst1;
	stRec_t     *pst2;
	int             iDiff;

	pst1 = (stRec_t*)data1;
	pst2 = (stRec_t*)data2;

	iDiff = (int)pst1->usKey - (int)pst2->usKey;

	return( iDiff );
}

thankx jazz
jazz is offline   Reply With Quote
Old May 29th, 2006, 3:15 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 773
Rep Power: 3 Jimbo is on a distinguished road
Can't you just make another two functions for comparing the different fields? IIRC that's the 3rd parameter to qsort, right?
Jimbo is offline   Reply With Quote
Old May 29th, 2006, 3:24 AM   #3
jazz
Newbie
 
Join Date: May 2006
Posts: 18
Rep Power: 0 jazz is on a distinguished road
Iam very new to programming, and very bad at sorting alogorithms...
can u help me plz?
jazz is offline   Reply With Quote
Old May 29th, 2006, 3:30 AM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 773
Rep Power: 3 Jimbo is on a distinguished road
take the iLayerCodeSort function, copy it, and instead of comparing the usCode, compare the usSubCode and usBrandCode, probably as iSubCodeSort(/*params*/) and iBrandCodeSort(/*params*/) respectively, and then call qsort using those functions as the 3rd parameter.
Jimbo is offline   Reply With Quote
Old May 29th, 2006, 3:42 AM   #5
jazz
Newbie
 
Join Date: May 2006
Posts: 18
Rep Power: 0 jazz is on a distinguished road
But in iLayerCodeSort function, where is it comparing usCode, I dint understand that.

static int iLayerCodeSort(const void *data1, const void *data2 )
{
	stRec_t     *pst1;
	stRec_t     *pst2;
	int             iDiff;

	pst1 = (stRec_t*)data1;
	pst2 = (stRec_t*)data2;

	iDiff = (int)pst1->usKey - (int)pst2->usKey;

	return( iDiff );
}
jazz is offline   Reply With Quote
Old May 29th, 2006, 4:09 AM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 773
Rep Power: 3 Jimbo is on a distinguished road
static int iLayerCodeSort(const void *data1, const void *data2 )
{
	stRec_t     *pst1;
	stRec_t     *pst2;
	int             iDiff;

	pst1 = (stRec_t*)data1; // cast data1 to a stRec_t pointer
	pst2 = (stRec_t*)data2; // cast data2 to a stRec_t pointer

	iDiff = (int)pst1->usKey - (int)pst2->usKey; // return the difference between their usKey values

	return( iDiff );
}
This just follows a comparison convention of comparing two objects a and b:
if a < b return something below 0
if a == b return 0
if a > b return something above 0
Jimbo is offline   Reply With Quote
Old May 29th, 2006, 5:05 AM   #7
jazz
Newbie
 
Join Date: May 2006
Posts: 18
Rep Power: 0 jazz is on a distinguished road
Hi, Thankx for the help.
I have an other doubt. Can u explain me the following bit plz. How do I assign subcode and brandcode to usKey??

pstILF->pstRec[i].usKey = 0;			/* Sort Key */
		for ( j = 0; j < SORT_REC; j++ )
                {
		     if( pstILF->pstRec[i].usCode == usSortKey[j] )
                      {
				pstILF->pstRec[i].usKey = j + 1;  /* Sort Key*/
				break;
			}
		}

Thankx jazz
jazz is offline   Reply With Quote
Old May 29th, 2006, 5:09 AM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 773
Rep Power: 3 Jimbo is on a distinguished road
this part of the code is checking to see if the currently selected item (pstILF->pstRec[i]) matches codes with a list of keys (usSortKey). If it does match, it sets the key for the item to the index into the list + 1
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC