Mindat Logo

Mindat (DOS) sourcecode from 1995

This is the final sourcecode for the DOS version of mindat, written between Christmas day 1993 and 28/08/1995. The code is unaltered, except for hiding my old (parents) address and phone number!

To view an example data file from this time, click here to view the E data file

Click back to the history page
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//                                                                   //
//     Mindat - The multi-purpose mineral database.                  //
//                                                                   //
//     Portable ANSI C code. Written by Jolyon Ralph.                //
//                                                                   //
//     (C) 1993-1994 J.Ralph, All rights reserved                    //
//                                                                   //
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

//#define AMIGA 1
#define INDEXSIZE 4300L

#define VERSION_NUMBER "V0.80"


// 25/12/93 - Project started. Various versions to current...
// 30/05/94 - V0.70 adds support for automatic synonym searching...
// 31/05/94 - V0.71 adds synonym to information list in INFO...
// 22/06/94 - V0.72 adds SITES keyword to list sites matching key
// 30/06/94 - V0.73 now checks locations correctly (commas)
// 10/07/94 - V0.74 now moved all data to alphabetical files for easier access...
// 12/06/95 - V0.75 now sets first letter to upper case (to work with new
//                                                data files where min name may not start with uppercase
// 28/06/95 - V0.76 upper case first letter rewritten. Should work better.
// 28/08/95 - V0.80 
#include 

#include 
#define MAXLINES 24
#include 
#include 
#include 
#include 
#define TRUE 1
#define FALSE 0

#define UPPER(x) if(x>='a' && x<='z') x=x-32;

#define LINESIZE 160

char Line2Print[LINESIZE];
char min[240];

char Synonym[LINESIZE];

char *index;
char *indexfile;

long IndexPos[INDEXSIZE];

char *IndexRef[INDEXSIZE];
char *IndexFileRef[INDEXSIZE];

char InputString[255];

FILE *writelocfh;

char found = 0;

int lastcr = 0;

int sortcount = 0;

int linecount = 0;

unsigned long countloc = 0;

unsigned long syncount = 0;


/* - CPU dependent functions go here... */

DIR *dirp = 0;
struct dirent *dirent = 0;

/*---- PROTOTYPES GO HERE ! ----*/
void errorexit();
void reindex(void);
char textinput(void);
void displaymindata(char *);
void listsome(char *);
void listsource(char *);
void locs(void);
char stringgreater(char *,char *);
char printmindata(char *,long);
char printmindataspecific(char *,char *,long,int);
char getline(FILE *,char *);
char *findfirstfile(char *);
char *findnextfile(void);
char setup(void);
void shutdown(void);
void qs_string(int, int);
int jprprintf(char *);
void jprsprintloc(char *,char*);
int formulaprint(char *);
int check_match(char *,char *);
void printlocbackwards(char *,char*);
void doregister(void);
void showinfo(void);
void addmins(void);
void listsites(char *);
void listloc(char *);
void clearscreen(void);
void splituserdata(void);
void splitdata(void);
unsigned long getfilesize(char *);
void copytoa(void);
char *findsynonym(char *);
/*------------------------------*/

void main(void)
{
        clearscreen();
        printf("MinDat %s Text Interface (C) 1993-1994 J.Ralph\n"
                         "Pre-release test version ONLY.\n"
                         "Type HELP for information on commands.\n",VERSION_NUMBER);
        while(textinput());
}

char textinput(void)
{
char *string,*filename;
int len;
char *parameter;

        linecount = 0;
        printf("MINDAT:>");
        string = gets(&InputString[0]);

        // Entries are two types:

        len = strlen(string);

        if(len==0) return(1);


        parameter = strchr(string,' ');

        //*parameter = 0;

        if(parameter) parameter++;

        if(!strnicmp("add",string,3)) {
                addmins();
                return(1);
        }


        if(!strnicmp("help",string,4)) {
                if(!parameter) {
                        printf("\nText Entry Help"
                                         "\n---------------\n"
                                         "\nshow xxx    - display info on mineral"
                                         "\nexit        - exit"
                                         "\nlist xx     - display minerals matching pattern"
                                         "\nabout       - about this program"
                                         "\nloc xxx     - show minerals at locality\n"

                                         "\nregister    - register the program\n"

                                         "\nadd         - add new mineral information"
                                         "\nreindex     - rebuild index file"
                                         "\nupdate      - update main records with new minerals"
                                         "\n");
                        return(1);
                }

                if(!strnicmp("show",parameter,4)) {
                        printf("\nShow"
                                         "\n----"
                                         "\nTyping Show, followed by a mineral name will list all information in"
                                         "\nthe database about that mineral."
                                         "\n\n"
                                         "\nExamples\n"
                                         "\nshow galena"
                                         "\nshow pyromorphite"
                                         "\nshow amethyst"
                                         "\nshow chalcedony\n");
                        return(1);
                }

                if(!strnicmp("exit",parameter,4)) {
                        printf("\nExit"
                                         "\n----"
                                         "\nQuit this program and return to DOS.\n");
                        return(1);
                }

                if(!strnicmp("reindex",parameter,6)) {
                        printf("\nReindex"
                                         "\n-------"
                                         "\nWhenever data is added or changed the index must be rebuilt."
                                         "\nType in reindex to start this process.\n");
                        return(1);
                }

                if(!strnicmp("list",parameter,4)) {
                        printf("\nList"
                                         "\n----"
                                         "\nTo list all minerals in the database, type LIST. To list minerals"
                                         "\nmatching a particular pattern use standard DOS patterns. "
                                         "\n\n"
                                         "\nExamples\n"
                                         "\nlist                   - list all minerals in MinDat"
                                         "\nlist a*                - list all minerals starting with A"
                                         "\nlist qu*z              - list all minerals starting with QU and"
                                         "\n                         ending with Z"
                                         "\nlist *chalc*           - list all minerals containing CHALC"
                                         "\n                         anywhere in the name             "
                                         "\n                                                    - (Chalcocite, Aurichalcite, etc.)\n");
                        return(1);
                }

                return(1);
        }

        if(!strnicmp("register",string,7)) {
                doregister();
                return(1);
        }

        if(!strnicmp("post",string,4)) {
                copytoa();
                return(1);
        }


        if(!strnicmp("about",string,5)) {
                clearscreen();
                printf("\nMinDat %s (C) 1993-1995 J.Ralph\n"
                                        "----------------------------------\n"
                                        "This mineral database software is being written as a tool\n"
                                        "for all mineral collectors to use. Unlike most databases\n"
                                        "this one grows as you use it. If you have any data to add\n"
                                        "then add it to the datafiles with this program, copy the\n"
                                        "changed datafile to floppy disk, send it to me and you\n"
                                        "will be helping increase the information available to\n"
                                        "everyone.\n\n"
                                        "This program, MinDat, will be freely distributable\n"
                                        "This means you do not have to pay to use this software\n"
                                        "although if you register the software with me (by sending\n"
                                        "in a nice mineral sample for my collection!) then you\n"
                                        "will get updated and enhanced data files containing more\n"
                                        "information on many minerals.\n\n"
                                        "Jolyon Ralph: Tel: (0181) 668 ----\n"
                                        "---------, Coulsdon, Surrey, CR5, ---, England\n\n",VERSION_NUMBER);
                return(1);
        }

        if(!strnicmp("site",string,4)) {
                if(parameter)
                        {
                        listsites(parameter);
                        } else {
                        printf("Must enter a location name\n");
                        }
                        return(1);
        }


        if(!strnicmp("loc",string,3)) {
                if(parameter)
                        {
                        listloc(parameter);
                        } else {
                        printf("Must enter a location name\n");
                        }
                        return(1);
        }

        if(!strnicmp("dir",string,3)) {
                printf("\n");
                filename = findfirstfile("*.mdt");
                while(filename) {
                        printf(" - %s\n",filename);
                        filename = findnextfile();
                }
                printf("\n");
                return(1);
        }

        if(!strnicmp("info",string,4)) {
                showinfo();
                return(1);
        }



        if(!strnicmp("show",string,4)) {
                clearscreen();
                displaymindata(parameter);
                printf("\n");
                return(1);
        }


        if(!strnicmp("update",string,6)) {
                clearscreen();
                splituserdata();
//              reindex();
                printf("WARNING - You must REINDEX before accessing database\n");
                return(1);
        }

        if(!strnicmp("reindex",string,7)) {
                setup();
                if(indexfile) {
                        printf("\nReindexing... \n");
                        reindex();
                        printf("Reindex done\n\n");
                } else {
                        printf("\nCannot rebuild index at the moment.. \n");
                }
                shutdown();
                return(1);
        }

        if(!strnicmp("exit",string,4)) {
                printf("\nThank you for using MinDat\n\n");
                return(0);
        }

        if(!strnicmp("quit",string,4)) {
                printf("\nThank you for using MinDat\n\n");
                return(0);
        }


        if(!strnicmp("list",string,4)) {
                printf("\nCurrent Mineral Index:\n\n");
        if(parameter)
                {
                        listsome(parameter);
                } else {
                        listsome("*");
                }
                return(1);
        }


        if(!strnicmp("where",string,5)) {
        if(*parameter)  listsource(parameter);
                return(1);
        }

        if(!strnicmp("split",string,5)) {
                splitdata();
                return(1);
        }

        printf("Sorry, I did not understand that\n");
        return(1);

}



void displaymindata(char *namein)
{
FILE *fh1;
char minname[LINESIZE];
char *commapos1,*commapos2;
long filepos;
char *name;


name = namein;


        found = 0;
        fh1=fopen("smain.idx","rb");

restart:
        if(!fh1)
        {
                printf("Could not load main index file!\n");
                return;
        }

        while(!feof(fh1))
        {
                getline(fh1,&minname[0]);
                if(!feof(fh1))
                {

                        commapos1 = strchr(&minname[0],',');
                        *commapos1 = 0;
                        commapos1++;



                        if(!stricmp(&minname[0],name)) {

                                commapos2 = strchr(commapos1,',');
                                *commapos2 = 0;
                                commapos2 ++;

                                sscanf(commapos1,"%ld",&filepos);

                                printmindata(commapos2,filepos);
                        }
                }
        }

        if(!found) {
         name = findsynonym(name);
         if(name) {
                rewind(fh1);
                goto restart;
         }
        }


        if(!found) printf("Sorry, no data on %s\n\n",namein);
        fclose(fh1);
}



void listsome(char *pattern)
{
FILE *fh1;
char instring[LINESIZE];
char last[LINESIZE];
char *commapos1;

        fh1=fopen("smain.idx","rb");

        if(!fh1)
        {
                printf("Could not load main index file!\n");
                return;
        }

        while(!feof(fh1))
        {
                getline(fh1,&instring[0]);
                if(!feof(fh1))
                {
                        commapos1 = strchr(&instring[0],',');
                        *commapos1 = 0;
                        commapos1++;


// astcsma is amiga specific...

                        if(strcmp(&instring[0],&last[0]) && check_match(&instring[0],pattern))
                        {
                                if(!jprprintf(&instring[0])) return;
                        }
                        strncpy(&last[0],&instring[0],LINESIZE);
                }
        }
        fclose(fh1);
}


void listsource(char *pattern)
{
FILE *fh1;
char instring[LINESIZE];
char temp[LINESIZE*2];
char *commapos1,*commapos2;

	fh1=fopen("smain.idx","rb");

	if(!fh1)
	{
		printf("Could not load main index file!\n");
		return;
	}

	while(!feof(fh1))
	{
		getline(fh1,&instring[0]);
		if(!feof(fh1))
 		{
			commapos1 = strchr(&instring[0],',');
			*commapos1 = 0;
			commapos1++;
			commapos2 = strchr(commapos1,',');
			commapos2++;

			if(check_match(&instring[0],pattern))
			{
				sprintf(&temp[0]," %s in file %s",&instring[0],commapos2);
				if(!jprprintf(&temp[0])) return;
			}
		}
	}
	fclose(fh1);
}


//////////////////////////////////////////////////////////////
// void locs(void) - Build Location database index          //
//                                                          //
// also (30/5/95) now builds synonym database index         //
//                                                          //
//////////////////////////////////////////////////////////////

void locs(void)
{
char *filename;
FILE *fh;
FILE *synfile;
char str1[LINESIZE] = {0};
char str2[LINESIZE] = {0};
long locn = 0;
long syn = 0;


        printf("Rebuilding location & synonym indexes - please wait!\n");

        filename = findfirstfile("*.mdt");

        if(!filename)
        {
                printf("Cannot find any .MDT files\n\n");
                return;
        }

        writelocfh=fopen("global.loc","wb");

        if(!writelocfh)
        {
                printf("Cannot open global location file\n");
                return;
        }

        synfile = fopen("synonyms.idx","wb");

        if(!synfile)
        {
                printf("Cannot open synonym file\n");
                return;
        }

loop:
        fh = fopen(filename,"rb");
        if(!fh)
        {
                printf("Cannot open file %s!\n",filename);
                return;
        }

        printf("Processing file %s\n",filename);

        while(str1[0]!='@' && !feof(fh)) getline(fh,&str1[0]);



        newmin:
        if(!feof(fh)) {
                getline(fh,&str2[0]);


                if(str2[0] == '@') {
                        strcpy(&str1[0],&str2[0]);
                        goto newmin;
                }

                loophash:

                if(!(strncmp(&str2[0],"#Synon",6)))
                {
                // write synonym entry here...
                        loopsyn:
                        getline(fh,&str2[0]);
                        if(feof(fh)) goto newmin;
                        if(str2[0]=='#') goto loophash;
                        if(str2[0]=='/')
                        {
                         syn++;
                         fprintf(synfile,"%s:%s\n",&str2[1],&str1[1]);

                        }
                        if(str2[0]=='@')
                        {
                                strcpy(&str1[0],&str2[0]);
                                goto newmin;
                        }
                        goto loopsyn;
                }


                if(strncmp(&str2[0],"#Locali",7)) goto newmin;

                looploc:
                getline(fh,&str2[0]);
                if(feof(fh)) goto newmin;
                if(str2[0]=='#') goto loophash;

                if(str2[0]=='/') {
                                locn++;
                 printlocbackwards(&str2[1],&str1[1]);
                }

                if(str2[0]=='@') {
                        strcpy(&str1[0],&str2[0]);
                        goto newmin;
                }

                goto looploc;

        }
        // end of that file..
        fclose(fh);
        filename = findnextfile();
        if(filename) goto loop;

        fclose(writelocfh);
        fclose(synfile);

        printf("\nFinished. %ld locations in database, %ld synonyms in table.\n\n",locn,syn);

        countloc = locn;
        syncount = syn;

}




char printmindata(char *filename, long filepointer)
{
char line[LINESIZE];
char temp[LINESIZE*2];
char copy[LINESIZE];
FILE *fh;

        fh=fopen(filename,"rb");

        if(!fh) {
                printf("Can't open %s.\n",filename);
                return(0);
        }

        fseek(fh,filepointer,1);

        getline(fh,&line[0]);

        if (!found) {
                if(!jprprintf("")) {fclose(fh);return(0);}

                UPPER(line[0]);
                sprintf(&temp[0],"%s",&line[0]);
                if(!jprprintf(&temp[0])) {fclose(fh);return(0);}
        }
        found = 1;


        while(!feof(fh) && line[0]!='@') {

                if((getline(fh,&line[0])) && line[0]) {

                switch(line[0]) {

                case '/':

//                      if(lastcr) printf("\n");
                        if (!jprprintf(&line[1])){fclose(fh); return(0);}
                        lastcr = 1;
                        break;

                case '\\':

//                      if(lastcr) printf("\n");
                        formulaprint(&line[1]);
                        lastcr = 1;
                        break;

                case '#':

                        if(strcmp(&line[0],©[0])) {
                                if(!jprprintf("")) {fclose(fh);return(0);}
                                sprintf(&temp[0],"%s",&line[1]);
                                if(!jprprintf(&temp[0])) {fclose(fh);return(0);}
                                lastcr = 0;
                        }

                        strncpy(©[0],&line[0],LINESIZE);

                        break;

                case '@':
                        break;

                default:

                        sprintf(&temp[0]," %s",&line[0]);
                        if(!jprprintf(&temp[0])) {
                                fclose(fh);
                                return(0);
                                }
                        lastcr = 1;
                        break;

                }
                }

        }
        fclose(fh);
        return(1);
}




char printmindataspecific(char *filename,char *searchitem,long filepointer, int justslash)
{
char line[LINESIZE];
int printon = FALSE;
FILE *fh;

        fh=fopen(filename,"rb");

        if(!fh) {
                printf("Can't open %s.\n",filename);
                return(0);
        }

                fseek(fh,filepointer,1);

                getline(fh,&line[0]);


                while(!feof(fh) && line[0]!='@') {

                        if((getline(fh,&line[0])) && line[0]) {

                        switch(line[0]) {

                        case '/':
                        if(printon) {
                                if(!justslash) {printf("\n%s\n",&line[1]);}
            else {
                                        printlocbackwards(&line[1],&min[0]);
                           }
                        }
                        break;

                        case '#':
                        if(!strcmp(&line[1],searchitem)) {
                        printon = TRUE;
                } else {
                        printon = FALSE;
                        }
                        break;

                        case '@':
                                break;

                        default:

                                if (printon && !justslash) printf(" %s\n",&line[0]);
                                break;

                }
                }

        }
fclose(fh);
return(1);
}


void showinfo()
{
 FILE *info;
 unsigned long mins;
 unsigned long locs;
 unsigned long syns;

 if(!(info = fopen("min.inf","rb")))
 {
  printf("Couldn't open min.inf file. Try re-index.\n");
  return;
 }
 fscanf(info,"%ld",&mins);
 fscanf(info,"%ld",&locs);
 fscanf(info,"%ld",&syns);
 printf("Mindat statistics:\n\n");
 printf("Number of mineral entries    = %ld\n",mins);
 printf("Number of synonyms listed    = %ld\n",syns);
 printf("Total minerals in database   = %ld\n\n",mins+syns);
 printf("Number of locality entries   = %ld\n\n",locs);

 fclose(info);


}



char stringgreater(char *first, char *second)
{
unsigned char x,y;

        if(!*first) return(1);
        if(!*second) return(0);

        while(1)
        {
                x=(unsigned char) *first++;
                y=(unsigned char) *second++;

        if((xy) || y ==0) return(1);

        }
}


void reindex(void)
{
FILE *fh,*fh2,*info;
char next;
char *memptr1;
char *memptr2;
char *filename;
char *bogus;
long start;
int i;
int min = 0;
unsigned long totmins = 0;

	if(!(info = fopen("min.inf","wb")))
	{
		printf("Couldn't open info file for write\n");
		return;
	}

// first clear array entries. May not be necessary, but WTF...

	for(i=0;id_name);
}

char * findnextfile(void)
{
	dirent = readdir(dirp);
        if(dirent==0)
		{
			closedir(dirp);
			dirp=0;
			return(NULL);
		}
        return(dirent->d_name);
}

char setup(void)
{

	index = malloc(INDEXSIZE*80L+10L);

	if(!index)
	{
		printf("Not enough ram for index\n");
		return(FALSE);
	}

	indexfile = malloc(INDEXSIZE*14+10);	

	if(!indexfile)
	{
		printf("Not enough ram for filename index\n");
		return(FALSE);
	}

	return(TRUE);
}

void shutdown(void)
{
	if(indexfile) free(indexfile);
	if(index) free(index);

	index = 0;
	indexfile = 0;
}

void qs_string(int left, int right)
{
int i,j;
char *x;
char *temp;
long temp2;
	i=left; j = right;
	x = (char *) IndexRef[(i+j)/2];

	do
	{
		while(_fstrcmp(IndexRef[i],x)<0 && i0 && j>left) j--;
		if(i<=j)
		{
			temp = IndexRef[i];
			IndexRef[i] = IndexRef[j];
			IndexRef[j] = temp;
			temp = IndexFileRef[i];
			IndexFileRef[i] = IndexFileRef[j];
			IndexFileRef[j] = temp;
			temp2 = IndexPos[i];
			IndexPos[i] = IndexPos[j];
			IndexPos[j] = temp2;

			sortcount++;
			if(sortcount==30)
			{
				printf(".");
				sortcount = 0;
			}

			i++;j--;
		}
	} while(i<=j);

	if(leftMORE<");
                f = getch();
                linecount = 0;
                printf("\r       \r");  
                if(f=='n' || f=='N') return(0);
        }

        printf(string);
        printf("\n");
        return(1);
}


int formulaprint(char *string)
{
char line1[101];
char line2[101];
char ourstring[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.()[]{},";
char *pointer;
char in = 0;
int lower = 0;
int last = 0;
int use2lines = 0;
int i;


        pointer = string;

        for(i=0;i<99;line1[i]=line2[i++]=' ');
        line1[99]= line2[99] = 0;
        i=0;
        while((in = *pointer++))
        {
                if(in == '^') 
                {
                        use2lines = 1;
                        lower = 1;
                        last = 1;
                } else
                {
                        if(lower)
                        {
                                if((!strchr(&ourstring[0],in)) || last)
                                {
                                        last = 0;
                                        line2[i++]=in;
                                } else {
                                        line1[i++]=in;
                                        lower = 0;
                                }
                        } else {
                                line1[i++]=in;
                        }
                }
        }
        line1[i]=0;
        line2[i]=0;

        printf("%s\n",&line1[0]);
if (use2lines)  printf("%s\n",&line2[0]);
        return(1);


}


int check_match(char*string,char*pattern)
{
        char fs,fp;
        int i,j,k;

loop:
        if(!*pattern && !*string) return(1);

        fp=*pattern++;                                  // fp = "*" *pattern = "l*l*o"  *string = "ello"

        if(fp=='?') {
                string++;
                goto loop;
        }

        if(fp=='*') {
                if(!*pattern) return(1) ;

                while(*pattern =='?') {
                        pattern++; string++;};

                j=0;

                while(*(pattern+j)!='?' && *(pattern+j)!='*' && *(pattern+j)) j++;

                k=strlen(string);

                if(!*(pattern+j)) {
                        if(strnicmp(string+k-j,pattern,j)) {
                                return(0);
                        }
                        return(1);
                }

                if(j) {
                        i=-1;
                        while(strnicmp(string++,pattern,j)) {
                                i++;
                                if(i==(k-1)) {
                                        return(0);
                                }
                        }
                        pattern=pattern+j;
                        string=string+j-1;
                }

                goto loop;
        }

        fs=*string++;

        if(fs && !fp) {
                return(0);}

        if(strnicmp(&fs,&fp,1)) {
                return(0);}

        goto loop;
}

void printlocbackwards(char *string, char *min)
{
int len;
int i;
        len = strlen(string);

        fprintf(writelocfh,"|");

        for(i=len-1;i>0;i--) {

                if(*(string+i) == ',') {
                        fprintf(writelocfh,"%s, ",string+i+2);
         *(string+i) = 0;
                }
        }
   if(min) {
                if(*string == '(' && *(string+1) == 'T') string+=5;
                fprintf(writelocfh,"%s \\ %s\n",string,min);
        } else
   {
        fprintf(writelocfh,"%s\n",string);
        }
}

void doregister(void)
{
char input[255]={0};
char input2[255]={0};
long regnumber;
long calc = 0;
int i;
FILE *fh;


        printf("\nPlease enter your name: ");
        gets(&input[0]);
        if(strlen(&input[0]) < 5)
        {
                printf("Sorry, name is too short. Must be at least 5 characters\n");
                return;
        }
        printf("\nPlease enter your registration number: ");
        gets(&input2[0]);

        if(strlen(&input2[0])!=9)
        {
                printf("\nSorry, registration is not valid.\n");
                return;
        }

        sscanf(&input2[0],"%ld",®number);

        for(i=0;i<5;i++)
        {
                calc = calc * input[i];
        }

        for(i=5;i 999999999) calc = calc - 999999999;
        while(calc < 100000000) calc = calc * 2 + 1341;

        if(!strcmp(&input2[0],"184619205")) {
                printf("Reg No. = %ld\n",calc);
                return;
        }

        if(calc != regnumber)
        {
                printf("\nSorry, registration is not valid.\n");
                return;
        }

        printf("Thank you - registration being initialised\n");


        input2[8] = '.';
        input2[9] = 'R';
        input2[10] = 'E';
        input2[11] = 'G';
        input2[12] = 0;

        printf("Creating %s\n",&input2[0]);

        fh=fopen(&input2[0],"wb");
        input2[9] = 'M';
        input2[10] = 'D';
        input2[11] = 'T';
        fprintf(fh,"%s\n",&input[0]);
        fprintf(fh,"%s\n",&input2[0]);
        fclose(fh);

        fh=fopen(&input2[0],"wb");
        fprintf(fh,"/* mindat data added by %s */\n",&input[0]);
        fclose(fh);

        printf("All done! Now quit MinDat and reload.\n");

}

void addmins(void)
{
 char *file;
 FILE *fh;
 char input[255] = {0};
 char min[255] = {0};

        file = findfirstfile("*.reg");

        if(!file) {
                printf("\nSorry, must be registered to add data.\n\n");
                return;
        }

        *(file+9) = 'm';
        *(file+10) = 'd';
   		*(file+11) = 't';


        if(!(fh=fopen(file,"ab"))) {
                printf("\nRegistration is not correct! Please re-run registration.\n");
                return;
        }

main:
        input[0] = 0;

        while(input[0]!='m' && input[0]!='l' && input[0]!='q' &&input[0]!='M' && input[0]!='L' && input[0]!='Q') {

                printf("\n\nAdd data by Mineral (M), Localitiy (L) or Quit (Q): ");
                gets(&input[0]);
        }

        if(input[0]=='m' || input[0]=='M')
        {
                printf("\nEnter mineral name: ");
                gets(&input[0]);
                if(input[0]) {
                        fprintf(fh,"@%s\n",&input[0]);
                        fprintf(fh,"#Localities:\n");

                        mlp:
                        printf("\nEnter locality:\n");
                        gets(&input[0]);
                        if(input[0]) {
                                fprintf(fh,"/%s\n",&input[0]);
                                printf("Enter comments:\n");
                                mik:
                                gets(&input[0]);
                                if(!input[0]) goto mlp;
                                fprintf(fh,"%s\n",&input[0]);
                                goto mik;
                        }
                }
                goto main;
        }

        if(input[0]=='l' || input[0]=='L')
        {
                printf("\nEnter locality: ");
                gets(&input[0]);
                if(!input[0]) goto main;


                printf("\nEnter minerals found here, one at a time\n");

                locloop:

                gets(&min[0]);

                if(min[0]) {
                        fprintf(fh,"@%s\n",&min[0]);
                        fprintf(fh,"#Localities:\n");
                        fprintf(fh,"/%s\n",&input[0]);

                        printf("Enter any comments (return to quit)\n");
                        while(min[0]) {
                                gets(&min[0]);
                                if(min[0]) fprintf(fh,"%s\n",&min[0]);
                        }
                        printf("\nEnter another mineral found here, or return to quit\n");
                        goto locloop;
                }
                goto main;
        }
        fclose(fh);
   return;

}

void listloc(char *name)
{
char	pat[255];
char	line[255] = {0};
char	last[255] = {6};
char	formatted[255] = {0};
FILE	*fh;
char	*min;

	sprintf(&pat[0],"*%s*",name);
	if(!(fh=fopen("global.loc","rb")))
	{
		printf("Cannot open global locality index\n");
		return;
	}

	do
	{
		if(getline(fh,&line[0]))
		{
			min = strchr(&line[0],'\\');
			if(min)
			{
				*min++ = 0;
				jprsprintloc(&formatted[0],&line[1]);
				if(check_match(&formatted[0],&pat[0]))
				{
					if(stricmp(&line[1],&last[1]))
					{
						//if(!jprprintf("")) return;
						if(!jprprintf(&formatted[0])) return;
						printf("");
						strcpy(&last[1],&line[1]);
					}
					if(!jprprintf(min)) return;
				}
			}
		}
	} while(!feof(fh));
	
	fclose(fh);
	printf("\n");
}

void listsites(char *name)
{

// Poor code - needs to write entries to array and sort it!

char    pat[255];
char line[255] = {0};
char last[255] = {6};
char formatted[255] = {0};
FILE  *fh;
char    *min;

        sprintf(&pat[0],"*%s*",name);
        if(!(fh=fopen("global.loc","rb"))) {
                printf("Cannot open global locality index\n");
                return;
        }

        do {
        if(getline(fh,&line[0])) {
                min = strchr(&line[0],'\\');
                if(min)
                        {
                        *min++ = 0;
                                jprsprintloc(&formatted[0],&line[1]);
                                if(check_match(&formatted[0],&pat[0]))
                                        {
                                        if(stricmp(&line[1],&last[1]))
                                                {
                                                        //if(!jprprintf("")) return;
                                                        if(!jprprintf(&formatted[0])) return;

                                                        printf("");
                                                        strcpy(&last[1],&line[1]);
                                                }
                                }
                        }
                }
        } while(!feof(fh));
        fclose(fh);
        printf("\n");
}


void jprsprintloc(char *output,char *string)
{
int len;
int i;
char copy[255];


        strcpy(output,"");

        strcpy(©[0],string);

        len = strlen(©[0]);

        if(copy[len-1] == ' ') copy[len-1] = 0;

        for(i=len-1;i>0;i--) {

                if(copy[i] == ',') {
                        strcat(output,©[i+2]);
                        strcat(output,", ");
                        copy[i] = 0;
                }
        }
        strcat(output,©[0]);
        strcat(output,"");
}

void clearscreen(void)
{
 system("cls");
}



void splituserdata(void)
{
 char *file;
 FILE *mainindex,*currentuserfile,*newtempuserfile,*currentminfile,*tempminfile;
 char search[255] = {0};
 char last[255] = {0};
 char input[255] = {0};
 char input2[255] = {0};
 char mindatname[255] = {0};
 char mindattempname[255] = {0};
 char minfilename[255] = {0};
 char *commapos1,*commapos2;
 FILE *changes;
 void *mem;
 int test,quit,found;
 unsigned long filepos;

 int num = 0;


        changes = fopen("changes.idx","ab");
        if(!changes)
        {
                printf("Couldn't open changes.idx\n\n");
                return;
        }



        mainindex=fopen("smain.idx","rb");

        if(!mainindex)
        {
                printf("Could not load main index file!\n");
                fclose(changes);
                return;
        }

        file = findfirstfile("*.reg");

        if(!file) {
                printf("\nSorry, must be registered to update database.\n\n");
                fclose(mainindex);
                fclose(changes);
                return;
        }

        *(file+9) = 't';
        *(file+10) = 'm';
        *(file+11) = 'p';

        if(!(newtempuserfile=fopen(file,"wb"))) {
                printf("\nCannot open temporary file.\n");
                fclose(mainindex);
                fclose(changes);
                return;
        }
        strcpy(&mindattempname[0],file);

        *(file+9) = 'm';
        *(file+10) = 'd';
        *(file+11) = 't';

        strcpy(&mindatname[0],file);

        if(!(currentuserfile=fopen(file,"rb"))) {
                printf("\nRegistration is not correct! Please re-run registration.\n");
                fclose(newtempuserfile);
                fclose(mainindex);
                fclose(changes);
                return;
        }


        while(!feof(currentuserfile))
        {
        // Called here with &last[] pointing to last entry (if there)

                strcpy(&last[0],&search[0]);
                search[0] = 0;

                while(search[0]!='@' && !feof(currentuserfile)) getline(currentuserfile,&search[0]);


        //This is where we jump in after finding a new mineral in the user file

        // at this point, either we have reached eof (finish) or we

        continuesearch:
                printf("\nUpdating %s",&search[1]);

                if(feof(currentuserfile))
                {
                        fclose(mainindex);
                        fclose(currentuserfile);
                        fclose(newtempuserfile);
                        printf("User file ended!!!!! add code jolyon...\n");
                        // need to delete currentuserfile (should be 0 bytes at this point)
                        // and rename newtempuserfile as oldtempuserfile!
                        fclose(changes);
                        return;
                }

//         if(stricmp(&search[1],&last[1]) > 0) rewind(mainindex);
                rewind(mainindex);

                quit = 1; found = 0;
                while(!feof(mainindex) && quit)
                {
                        getline(mainindex,&input[0]);

                        if(!feof(mainindex))
                        {

                                commapos1 = strchr(&input[0],',');
                                *commapos1 = 0;
                                commapos1++;
                                commapos2 = strchr(commapos1,',');
                                commapos2++;

                                test = stricmp(&input[0],&search[1]);
                                if(!test)
                                {

                                        if(stricmp(commapos2,file))
                                        {
                                                num++;
                                                printf(" - *MOVED*");
//                                              printf("Moving data on %s to file %s\n",&search[1],commapos2);
                                                sscanf(commapos1,"%ld",&filepos);
                                                filepos --;
                                                strcpy(&minfilename[0],commapos2);

                                                if(!(currentminfile=fopen(commapos2,"rb")) || !(tempminfile=fopen("userdata.tmp","wb")) || !(mem = malloc(2048)))                                               {
                                                        printf("File open / Memory allocation error!\n");
                                                        if(tempminfile) fclose(tempminfile);
                                                        if(currentminfile) fclose(currentminfile);
                                                        fclose(mainindex);
                                                        fclose(currentuserfile);
                                                        fclose(newtempuserfile);
                                                        fclose(changes);
                                                        return;
                                                }
                                                // now read in bytes...


//                  getline(currentuserfile,&input2[0]);

/*                                              while(filepos>2047) {
                                                        fread(mem,2048,1,currentminfile);
                                                        fwrite(mem,2048,1,tempminfile);
                                                        filepos-=2048;
                                                }
                                                if(filepos) {
                                                        fread(mem,filepos,1,currentminfile);
                                                        fwrite(mem,filepos,1,tempminfile);
                                                }
*/


                                         getline(currentminfile,&input[0]);

                                                while(stricmp(&input[0],&search[0]) && !feof(currentminfile))
                                                {
                                                        fprintf(tempminfile,"%s\n",&input[0]);
                                                        getline(currentminfile,&input[0]);
                                                }

                                                if(feof(currentminfile)) {
                                                        printf("Last input is %s\n",&input[0]);
                                                        printf("Last search is %s\n",&input2[0]);
                                                        printf("Serious indexing error! Aborting.\n");
                                                        fclose(mainindex);
                                                        fclose(currentuserfile);
                                                        fclose(newtempuserfile);
                                                        fclose(currentminfile);
                                                        fclose(tempminfile);
                                                        fclose(changes);
                                                        if(mem) free(mem);
                                                        return;
                                                }
//                                              printf("Search line is %s\n",&input2[0]);

//                  getline(currentminfile,&input[0]);
                         addloop:
                                                if(input[0]) {
//                                                      printf("Adding %s (from original) \n",&input[0]);
                                                        fprintf(tempminfile,"%s\n",&input[0]);
                                                        getline(currentminfile,&input[0]);


                                                        if(input[0]!='#' && input[0]!='@' && !feof(mainindex)) {
//                                                              printf(" going to add loop - input = %s!\n",&input[0]);
                                                                goto addloop;
                                                        }
                                                }

                                                if (input[0]=='@' || feof(currentminfile)) goto endbodge;

                                                if(!input[0])
                                                {
//                                                      printf("Adding #Localities: (2) \n");
                                                        fprintf(tempminfile,"#Localities:\n");
                                                        goto writenewdata;
                                                }

                                                if(!(strnicmp(&input[0],"#Loc",4)))
                                                {
                                                getline(currentminfile,&input[0]);
//                                              printf("Adding #Localities: (3) \n");
                                                fprintf(tempminfile,"#Localities:\n");

          writenewdata:
                                                        getline(currentuserfile,&input2[0]);
                                                        if(input2[0]=='@' || feof(currentuserfile)) goto endbodge;

                                                        if(input2[0] && strnicmp(&input2[0],"#Loc",4)) {
//                                                              printf("Adding %s (4) \n",&input2[0]);
                                                                fprintf(tempminfile,"%s\n",&input2[0]);
                                                        }

                                                        goto writenewdata;
                                                }

                                                goto addloop;

                        endbodge:

                                                // now write out rest of file.

//                  getline(currentminfile,&input[0]);

                                                while(!feof(currentminfile))
                                                {
                                                        fprintf(tempminfile,"%s\n",&input[0]);
                                                        getline(currentminfile,&input[0]);
                                                }

                                                found = 1;
                                                quit = 0;
                                                free(mem);
                                                fclose(currentminfile);
                                                fclose(tempminfile);

                                                remove(&minfilename[0]);
                                                rename("userdata.tmp",&minfilename[0]);


                                                if(input2[0] == '@') {
                                                        strcpy(&last[0],&search[0]);
                                                        strcpy(&search[0],&input2[0]);
                                                        goto continuesearch;
                                                }

                                        }
                                        if(test>0) quit = 0;
                                }
                        }
                }
                if(!found)              // keeping old data
                {
//                      printf("Keeping old data on %s\n",&search[1]);
                loop:
//                      printf("Writing %s (6)\n",&search[0]);
                        fprintf(newtempuserfile,"%s\n",&search[0]);
                        getline(currentuserfile,&search[0]);
                        if(search[0]!='@' && !feof(currentuserfile)) goto loop;
                        if(search[0]=='@') goto continuesearch;

                }
        }

        printf("\n\nFinished adding %d entries\n\n",num);
        fclose(newtempuserfile);
        fclose(currentuserfile);
        fclose(mainindex);
        fclose(changes);

        remove(&mindatname[0]);
        rename(&mindattempname[0],&mindatname[0]);

}

unsigned long getfilesize(char *filename)
{
        struct stat statbuf;
        if(stat(filename,&statbuf)) {
                printf("Unable to examine file\n");
                return(0);
        }
        return((unsigned long) statbuf.st_size);
}

void copytoa(void)
{
        printf("\nThis will copy the new data that you have created to the"
                         "\nA: drive (diskette) so you can return it to Jolyon for inclusion"
                         "\nin the next version. ONLY when Jolyon responds and"
                         "\ngives you the OK, use the KILLPOST option to remove unwanted"
                         "\ndata.\n\n"
                         "Make sure that there is a FORMATTED diskette in drive A: and press return."
                         "\n\n");
        getch();
        printf("\nCopying to a:\n");
}

//////////////////////////////////////////////////////////
//                                                      //
// findsynonym - find official name for given synonym.  //
//                                                      //
// Reads data from synonyms.idx                         //
//                                                      //
//////////////////////////////////////////////////////////
char *findsynonym(char *in)
{
FILE *syn;
char *colonpos =0;
int found = 0;
char buffer[LINESIZE];

        if(!(syn=fopen("synonyms.idx","rb"))) return(0);

        while (!found && !feof(syn))
        {
                getline(syn,&buffer[0]);

                if(colonpos = strchr(&buffer[0],':'))
                {
                        *colonpos++ = 0;

                        if(!(stricmp(&buffer[0],in)))
                        {
                                strcpy(&Synonym[0],colonpos);
                                found = 1;
                        }
                }
        }

        fclose(syn);
        if(found) return(&Synonym[0]);
                else return(0);

}

// SplitData()

void splitdata(void)
{
FILE *index;
FILE *new;
FILE *old;
char IndexLine[255];
char DataLine[255];
char current = 0;
char *commapos1,*commapos2;
unsigned long filepos;
char fname[127];

        if(!(index=fopen("smain.idx","rb")))
        {
                printf("Unable to open main index.\n");
                return;
        }

        // get line from file

        while(!feof(index))
        {
                getline(index,&IndexLine[0]);

                if(!feof(index))
                {

                        commapos1 = strchr(&IndexLine[0],',');
                        *commapos1 = 0;
                        commapos1++;

                        commapos2 = strchr(commapos1,',');
                        *commapos2 = 0;
                        commapos2 ++;

                        sscanf(commapos1,"%ld",&filepos);

                        if(IndexLine[0]!=current)
                        {
                                // close old file if open
                                // open new file

                                if(current) fclose(new);
                                sprintf(&fname[0],"%cMINS.DAT",IndexLine[0]);
                                if(!(new = fopen(&fname[0],"wb")))
                                {
                                        printf("File creation error!");
                                        return;
                                }
                                current = IndexLine[0];
                        }


                        if(old = fopen(commapos2,"rb"))
                        {

                                fseek(old,filepos-1,1);

                                getline(old,&DataLine[0]);
                                fprintf(new,"%s\n",&DataLine[0]);

                                getline(old,&DataLine[0]);
                                while(!feof(old) && !(DataLine[0]=='@'))
                                {

                                        fprintf(new,"%s\n",&DataLine[0]);
                                        getline(old,&DataLine[0]);
                                }

                                printf("Moved %s from %s to %cmin.dat\n",&IndexLine[0],commapos2,current);

                                fclose(old);
                        } else {
                                printf("File open error!\n");
                                return;
                        }
                }

        }

        fclose(new);
        fclose(index);

}
Mineral and/or Locality
Search Google
 
Copyright © Jolyon Ralph and Ida Chau 1993-2012. Site Map. Locality, mineral & photograph data are the copyright of the individuals who submitted them. Further information contact the Site hosted & developed by Jolyon Ralph. Mindat.org is an online information resource dedicated to providing free mineralogical information to all. Mindat relies on the contributions of hundreds of members and supporters. Mindat does not offer minerals for sale. If you would like to add information to improve the quality of our database, then click here to register.
Current server date and time: 22nd Feb 2012 22:04:31
Mineral and Locality Search
Mineral:
and/or Locality:
Options
Fade toolbar when not in focusFix toolbar to bottom of page
Hide Social Media Links
Slideshow frame delay seconds