Talk:List of listed buildings in Liverpool/Code

// ListedProcess.cpp : Defines the entry point for the console application. //


 * 1) include "stdafx.h"
 * 2) include "stdio.h"
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 


 * 1) define COMMENTLEN 200

typedef struct {	int	Day; char Month[10]; int	Year; char Grade[5]; char Street[100]; char Comment[COMMENTLEN]; char PostCode[5]; int	 SortOffset; } ListItems;

typedef struct {	char *Line; } HeaderItem;

typedef struct {	char *Line; } FooterItem;


 * 1) define BUFFSIZE	1024

bool ReadList(FILE *ListText,HeaderItem *Headers[],int &HeaderCount,ListItems List[],int &ListCount,FooterItem *Footers[],int &FooterCount) {	bool	Success=false; bool	InHeader=true; bool	InBody=false; bool	InFooter=false;

char	Line[BUFFSIZE];

while(!feof(ListText) && !ferror(ListText)) {		if(fgets(Line,BUFFSIZE,ListText)!=NULL) {			if(InHeader) {				if(strncmp(Line,"==",2)==0) {					InHeader=false; InBody=true; }				else {						*Headers=(HeaderItem*)realloc(*Headers,sizeof(*Headers)*(HeaderCount+1)); (*Headers)[HeaderCount].Line=(char *)malloc(strlen(Line)+1); strcpy((*Headers)[HeaderCount].Line,Line); HeaderCount++;

//					printf("%s",Line); }			}

if(InBody) {				if(strncmp(Line,"==External links==",18)==0) {					InBody=false; InFooter=true; }				else if(strncmp(Line,"[[Image:",8)==0)				{ //					InBody=false; //					InFooter=true;				}								else if(strncmp(Line,"==",2)==0)				{ //					InBody=false; //					InFooter=true;				}				else if(strncmp(Line,"*",1)==0)				{					char Rest[1000];					char TStreet[100];

int	j;

sscanf(Line,"%*5c%d%s%d%*4c%*s%*s%s%*9c%[^\n]",&List[ListCount].Day,List[ListCount].Month,&List[ListCount].Year,&List[ListCount].Grade,&Rest);

for(int i=0;i<strlen(List[ListCount].Grade);i++) {						if(List[ListCount].Grade[i]=='\'') {							List[ListCount].Grade[i]=0; break; }					}

bool StreetDone=false; int Num;

List[ListCount].PostCode[0]=0; List[ListCount].Comment[0]=0; List[ListCount].Street[0]=0;

for(int i=0;i<strlen(Rest);i++) {						if(Rest[i]=='L') {							if(sscanf(&Rest[i+1],"%d",&Num)==1) {								strncpy(TStreet,Rest,i); TStreet[i]=0; for(j=0;j<3;j++) {									if(Rest[i+j]==' ') {										break; }									else {										List[ListCount].PostCode[j]=Rest[i+j]; }								}								List[ListCount].PostCode[j]=0; if(i+j<strlen(Rest)-1) {									if(strlen(&Rest[i+j])<COMMENTLEN) {										strcpy(List[ListCount].Comment,&Rest[i+j]); }									else {										strcpy(List[ListCount].Comment,&Rest[i+j]); }								}								else {									List[ListCount].Comment[0]; }

strcpy(List[ListCount].Street,TStreet); StreetDone=true;

break; }						}					}

if(!StreetDone) {														strcpy(List[ListCount].Street,Rest); List[ListCount].Comment[0]=0;

}					List[ListCount].SortOffset=0; while(!isalpha(List[ListCount].Street[List[ListCount].SortOffset])) {						List[ListCount].SortOffset++; }					ListCount++; }			}

if(InFooter) {						*Footers=(FooterItem*)realloc(*Footers,sizeof(*Footers)*(FooterCount+1)); (*Footers)[FooterCount].Line=(char *)malloc(strlen(Line)+1); strcpy((*Footers)[FooterCount].Line,Line); FooterCount++; }		}	}

if(!ferror(ListText)) {		Success=true; }

return Success; }

int Compare( void *pvlocale, const void *str1, const void *str2) {	int	One=*(int *)str1; int	Two=*(int *)str2; ListItems *List=(ListItems*)pvlocale;

return stricmp(&List[One].Street[List[One].SortOffset],&List[Two].Street[List[Two].SortOffset]); }

bool SortList(int ListIndex[],ListItems List[],int ListItemCount) {	bool	Success=true;

for(int i=0;i<ListItemCount;i++) {		ListIndex[i]=i; }

qsort_s(ListIndex,ListItemCount,sizeof(int),Compare,(void *)List);

return Success; } /*


 * {| class="sortable wikitable" border="1"

! class="sortable" style="background: #f2f2f2; color: #000080" height="17" width="35%" | Location ! class="unsortable" style="background: #f2f2f2; color: #000080" height="17" width="10%" | Postcode ! class="unsortable" style="background: #f2f2f2; color: #000080" height="17" width="35%" | Notes ! class="unsortable" style="background: #f2f2f2; color: #000080" height="17" width="10%" | Year ! class="sortable" style="background: #f2f2f2; color: #000080" height="17" width="10%" | Listing
 * Abercromby Square
 * L7
 * Garden House
 * 1972
 * II
 * Abercromby Square (west side)
 * L7
 * Nos. 1 to 7 (consec) No. 7a
 * 1972
 * II
 * }
 * II
 * }



bool WriteList(FILE *ListOut,HeaderItem *Headers,int HeaderCount,ListItems List[],int ListCount,int ListIndex[],FooterItem Footers[],int FooterCount) {	bool	Success=false; char	CSort=9; bool	DoneOne=false;

for(int i=0;i<HeaderCount;i++) {		fprintf(ListOut,"%s",Headers[i].Line); }

for(int i=0;i<ListCount;i++) {		if(List[ListIndex[i]].Street[List[ListIndex[i]].SortOffset]!=CSort && DoneOne) {			fprintf(ListOut,"|}\n"); }		if(List[ListIndex[i]].Street[List[ListIndex[i]].SortOffset]!=CSort) {			CSort=List[ListIndex[i]].Street[List[ListIndex[i]].SortOffset];

fprintf(ListOut,"== %c ==\n",CSort);

fprintf(ListOut,"::{| class=\"sortable wikitable\" border=\"1\"\n"); fprintf(ListOut,"! class=\"sortable\" style=\"background: #f2f2f2; color: #000080\" height=\"17\" width=\"35%%\" | Location\n"); fprintf(ListOut,"! class=\"unsortable\" style=\"background: #f2f2f2; color: #000080\" height=\"17\" width=\"10%%\" | Postcode\n"); fprintf(ListOut,"! class=\"unsortable\" style=\"background: #f2f2f2; color: #000080\" height=\"17\" width=\"35%%\" | Notes\n"); fprintf(ListOut,"! class=\"unsortable\" style=\"background: #f2f2f2; color: #000080\" height=\"17\" width=\"10%%\" | Year\n"); fprintf(ListOut,"! class=\"sortable\" style=\"background: #f2f2f2; color: #000080\" height=\"17\" width=\"10%%\" | Listing\n");

DoneOne=true; }

fprintf(ListOut,"|-\n"); fprintf(ListOut,"| %s\n",List[ListIndex[i]].Street); fprintf(ListOut,"| %s\n",List[ListIndex[i]].PostCode); fprintf(ListOut,"| %s\n",List[ListIndex[i]].Comment); fprintf(ListOut,"| %d\n",List[ListIndex[i]].Year); fprintf(ListOut,"| %s\n",List[ListIndex[i]].Grade); }	fprintf(ListOut,"|}\n");

for(int i=0;i<FooterCount;i++) {		fprintf(ListOut,"%s",Footers[i].Line); }

return Success; }

int _tmain(int argc, _TCHAR* argv[]) {	ListItems List[3000]; int ListItemCount=0; int	ListIndex[3000];

FILE *ListTextIn; FILE *ListTextOut;

HeaderItem	*Header=NULL; FooterItem	*Footer=NULL; int		HeaderCount=0; int		FooterCount=0;

fopen_s(&ListTextIn,"ListedBuildings.txt","r"); if(ListTextIn) {		if(ReadList(ListTextIn,&Header,HeaderCount,List,ListItemCount,&Footer,FooterCount)==true) {			if(SortList(ListIndex,List,ListItemCount)) {				fopen_s(&ListTextOut,"ListedBuildingsNew.txt","w"); if(ListTextOut) {					if(WriteList(ListTextOut,Header,HeaderCount,List,ListItemCount,ListIndex,Footer,FooterCount)==true) {						printf("Process complete OK\n"); }					else {						printf("List write failed\n"); }					fclose(ListTextOut); }				else {					printf("Failed to open file ListedBuildingsNew.txt\n"); }			}			else {				printf("Sorting Error\n"); }		}		else {			printf("List read failed\n"); }		fclose(ListTextIn); }	else {		printf("Failed to open file ListedBuildings.txt\n"); }

printf("Press return to exit\n");

getc(stdin); return 0; }