/* * Concordance Programming Language * * Copyright (c) 2000 Dataflight Software, Inc. * ALL RIGHTS RESERVED. * 2337 Roscomare Road, Suite 11 * Los Angeles, CA 90077 * * Unauthorized distribution, adaptation or use may be * subject to civil and criminal penalties. * * You may incorporate this program into your own programs * ONLY if you incorporate the following copyright notice: * * Copyright (c) 2000 YOUR NAME. ALL RIGHTS RESERVED. * Portions copyright (c) 1994 Dataflight Software, Inc. */ /* This program looks for names between delimiters. Change the ** delimiter below to any single character you wish. It must ** be enclosed in quotes, not apostrophes. */ text szDelimiter = ";"; text namearray[102]; int db; int CR = 13; int ESC = 27; int EOF = -1; /******************************************************** * Name: Field sort program * Synopsis: Sorts field entries ********************************************************/ main() { char string[256], fname[50], fname2[50], szTempFile[256]; text screen; text newlist; int i, a, f, fieldLength, offset, duplicates, length, error, lines, btree; cls(); /* open a database if one isn't open. */ if( db.fields <= 0 ) { if( getfile('Database',"*.DCB",string) == CR ) { closedb( db ); first( db = opendb( string )); } } /* Return if the database isn't open. */ if( db.fields <= 0 ) return; InitMenu(); /* Avoid ending the loop with a function key. */ cls(); /* Choose the first field to compare. */ i = db.fields + 2; while( i > db.fields+1 ) i = menu( 2, 22, 25, 47,namearray,a ); if( i == 0 ) return; fname = db.name[i]; /* Choose the second field to compare. */ i = db.fields + 2; while( i > db.fields+1 ) i = menu( 2, 50, 25, 77,namearray,a ); if( i == 0 ) return; fname2 = db.name[i]; cls(); szTempFile = mkuniq(".\", ".TMP"); cycle(db) { erase(szTempFile); if (exist(szTempFile)) return(messageBox("Couldn't erase temporary file "+szTempFile, program(), MB_OK)); /* Create a case insensitive b+tree that will reject duplicate entries. */ if ((btree = btcreate(szTempFile, 2)) < 0) return(messageBox("Couldn't create temporary file "+szTempFile, program(), MB_OK)); puts( 2, 1, "Locating duplicates... "+str(duplicates)); Message("Document: "+str(docno(db))+" of "+str(count(db)) , FALSE); /* Store all names from the first field. */ fieldLength = len(db->fname); offset = match(db->fname, szDelimiter, f = 1); while ((offset > 0) and (f < fieldLength)) { string = trim(substr( db->fname, f, offset - f)); btinsert( btree, string, 0 ); if ((offset = match(db->fname, szDelimiter, f = offset + 1)) == 0) offset = fieldLength + 1; } /* Insert the last entry. */ if (f < fieldLength) btinsert(btree, trim(substr(db->fname, f)), 0 ); /* Now look in the 2nd field. */ offset = match(db->fname2, szDelimiter, f = 1); fieldLength = len(db->fname2); while ((offset > 0) and (f < fieldLength)) { string = trim(substr( db->fname2, f, offset - f)); if (btfind( btree, string, i ) == 0) { duplicates = duplicates + 1; tag(db, TRUE, "Duplicate name in fields "+fname+", "+fname2); } if ((offset = match(db->fname, szDelimiter, f = offset + 1)) == 0) offset = fieldLength + 1; } /* Compare the last entry. */ if (f < fieldLength) { if (btfind( btree, trim(substr(db->fname2, f)), i ) == 0) { duplicates = duplicates + 1; tag(db, TRUE, "Duplicate name in fields "+fname+", "+fname2); } } btclose(btree); if (erase(szTempFile)) return(Message("Error erasing file: "+szTempFile, TRUE)); if( keypress() ) if( getkey() == ESC ) break; } /* cycle */ } /** main() **/ /**************************************************************** * Name: Normalize * * Synopsis: Remove underscores and capitalize field names. * * Return: Name of a field. * ****************************************************************/ normalize(text name) { char string[20]; int a, LastChar; string = name; for(a = 1; string[a]; a = a + 1) { if(string[a] == '_') string[a] = ' '; else if (LastChar <> ' ') string[a] = lower(string[a]); LastChar = string[a]; } return(string); } /** Normalize() **/ RGB(char red, grn, blu) { return(((blu & 255) * 65536) | ((grn & 255) * 256) | (red & 255)); } /**************************************************************** * Name: Message * * Synopsis: Displays error message and waits for key. * ****************************************************************/ Message(text message; int pause) { text screen; int key; cursoroff(); screen = save(5,13,8,69); box(5,13,8,69,"3D", RGB(160,160,160), RGB(190,190,190)); puts(6,14,pad(message,'C',53), RGB(0,0,0), RGB(190,190,190)); if (pause) { key = getkey(); restore(5,13,screen); } return(asc(upper(chr(key)))); } /* Message() */ /**************************************************************** * Name: FileName * * Synopsis: Trims the path from the file name. * ****************************************************************/ FileName(text name) { int i; while(i = match(name,"\",1)) name = substr(name,i+1); return(name); } /* FileName() */ /**************************************************************** * Name: mkuniq * * Synopsis: Creates a unique file name. * ****************************************************************/ mkuniq(text dosPath, ext) { char string[256]; int i; for(i = 0; i < 10000; i = i + 1) if (exist(string = dosPath + "Erase me-"+str(i) + ext) == 0) break; return(string); } /* mkuniq() */ /**************************************************************** * Name: Path * * Synopsis: Returns the path up to the last \ and without * * the file's name. * ****************************************************************/ Path(text dosPath) { int i, j; if ((i = match(dosPath,":",1)) == 0) i = match(dosPath,"\",1); while(j = match(dosPath,"\",i+1)) i = j; return(substr(dosPath,1,i)); } /* Path() */ /**************************************************************** * Name: InitMenu * * Synopsis: Initialize the field selection menu. * ****************************************************************/ InitMenu() { int i; for(i = 1; i <= db.fields; i = i +1) switch(db.type[i]) { case 'T' : namearray[i] = pad(normalize(db.name[i]),'L',13)+ "Text "; case 'P' : namearray[i] = pad(normalize(db.name[i]),'L',13)+ "Full Text"; case 'N' : namearray[i] = pad(normalize(db.name[i]),'L',13)+ "Numeric "; case 'D' : namearray[i] = pad(normalize(db.name[i]),'L',13)+ "Date "; } /* Clear out the last menu item and set the menu title. */ namearray[i] = ""; namearray[0] = "Field Type "; } /** InitMenu() **/