/* ** Concordance Programming Language ** ** Copyright (c) 1995 Dataflight Software, Inc. ** ALL RIGHTS RESERVED. ** 2337 Roscomare Road, Suite 11 ** Bel Air, 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: ** ** SRCHREPL.CPL ** Version 1.0 */ int MAXREPLACE = 50; text pszSearch[MAXREPLACE], pszReplace[MAXREPLACE]; /***************************************************************/ /* Function: Main */ /* Purpose : Entry point for all programs */ /***************************************************************/ main() { text mymenu[6], menustring1, menustring2, pszDatabase, pszFile; int db, next, done; /* Initialize screen */ InitScreen(); cursoroff(); cursor(0,0); /* Set up the menu */ menustring1 = "[1] Open database : "; menustring2 = "[2] Open search and replace file : "; mymenu[0] = "Search and Replace"; mymenu[1] = menustring1 + FileName(db.database); mymenu[2] = menustring2; mymenu[3] = "[G] Go!"; mymenu[4] = "[Q] Quit"; /* Set the starting menu option */ if (db.database <> "") next = 2; else next = 1; /* Display the menu */ while (done == FALSE) { next = menu(5, 10, 12, 70, mymenu, next,"12GQ"); switch(next) { /* Open database */ case 1: if (getfile("Open database", "*.DCB", pszDatabase) == CR) { /* Close the current database */ close(db); /* Open the new database */ db = opendb(pszDatabase); } /* Set the menu */ if (db.database <> "") { mymenu[1] = menustring1 + FileName(db.database); next = 2; } else { mymenu[1] = menustring1; next = 1; } break; /* Open the search and replace file */ case 2: if (getfile("Open search and replace file", "*.TXT", pszFile) == CR) { mymenu[2] = menustring2 + FileName(pszFile); next = 3; } break; /* Go */ case 3: SearchReplace(db, pszFile); InitScreen(); next = 4; break; /* Quit */ case 4: done = TRUE; break; } } } /**************************************************************** * Name: SearchReplace * * Synopsis: Search and replace for text. * ****************************************************************/ SearchReplace(int db; text pszFile) { int nCount, i, j; /* Check input values */ if (db.database == "") return(Message("Please open a database first. ", TRUE)); if (pszFile == "") return(Message("Please open a file first. ", TRUE)); /* Open the file */ if (nCount = LoadFile(pszFile)) { /* Place a status box up */ Message("", FALSE); /* Cycle through the database */ cycle(db) { /* Put up a message box */ puts( 8, 14, pad("Processing record " + str(docno(db)) , 'C', 55 ), RGB(255,255,255), RGB(0,0,255)); /* Loop through the fields */ for (i = 1; i <= db.fields; i = i + 1) { /* Loop through the search and replace strings */ for (j = 0; j < nCount; j = j + 1) { /* Search and replace within the field */ if ((db.type[i] == 'T') or (db.type[i] == 'P')) ReplaceInField(db, i, pszSearch[j], pszReplace[j]); } } } } } /**************************************************************** * Name: ReplaceInField * * Synopsis: Search and replace text within a field. * ****************************************************************/ ReplaceInField(int db, nField; text pszSearchString, pszReplaceString) { int i, j = 1, nSearchLength, nReplaceLength; /* Search for all occurrences */ while (i = match(db->nField, pszSearchString, j)) { /* Get the length of the search and replace strings */ nSearchLength = len(pszSearchString); nReplaceLength = len(pszReplaceString); /* Replace the string */ db->nField = substr(db->nField, 1, i - 1) + pszReplaceString + substr(db->nField, i + nSearchLength); /* Increment j */ j = i + nReplaceLength; } } /**************************************************************** * Name: matchex * * Synopsis: match with wildcards * ****************************************************************/ matchex(text pszText, pszMatch; int nOffset) { int nLengthText, nLengthMatch, bFoundMatch = TRUE, i; char szText[512], c; nLengthText = len(pszText); nLengthMatch = len(pszMatch); /* Loop through the entire text */ for (i = nOffset; i <= nLengthText - nLengthMatch + 1; i = i + 1) { /* Get the text at offset i for nLengthMatch characters */ szText = substr(pszText, i, nLengthMatch); /* Check the string */ for (j = 0; j < nLengthMatch; j = j + 1) { if ((c = substr(pszMatch, j + 1, 1)) <> "?") { if (szText[j] <> c) { bFoundMatch = FALSE; break; } } } } if (bFoundMatch) puts(0,0,"Found " + pszMatch + " at " + substr(pszText, i, nLengthMatch)); if (bFoundMatch) return(i); else return(0); } ReplaceInFieldEx(int db, nField; text pszSearchString, pszReplaceString) { } /**************************************************************** * Name: LoadFile * * Synopsis: Loads the file into the arrays * ****************************************************************/ LoadFile(text pszFile) { int fh, i, nCount; char szBuffer[1024]; /* Open the file */ if ((fh = open(pszFile, "r")) <> EOF) { /* Read each line */ while ((readln(fh, szBuffer) <> EOF) and (nCount < MAXREPLACE)) { /* Parse the line */ if (i = match(szBuffer, ",", 1)) { /* Get the search and replace strings */ pszSearch[nCount] = substr(szBuffer, 1, i - 1); pszReplace[nCount] = substr(szBuffer, i + 1); /* Increment the count */ nCount = nCount + 1; } } /* Close the file */ close(fh); } /* Return how many items were loaded */ return(nCount); } /**************************************************************** * 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: initScreen * * Synopsis: Clears the screen and puts copyright. * ****************************************************************/ InitScreen() { cls(RGB(128,128,128)); puts(MaxRow_,0,pad("(C) Copyright Dataflight Software, Inc. 1999. All Rights Reserved.",'C',80)); } /**************************************************************** * Name: RGB * * Synopsis: Helper routine for Windows color creation. * ****************************************************************/ RGB(int 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 wait ) { text screen; int key; cursoroff(); screen = save( 5, 13, 10, 69 ); box( 7, 13, 9, 69, "3U", RGB(0,0,150), RGB(0,0,255) ); puts( 8, 14, pad( message, 'C', 55 ), RGB(255,255,255), RGB(0,0,255)); if( wait ) { key = getkey(); restore( 5, 13, screen ); } if( islower( key )) key = key - ( 'a' - 'A' ); return( key ); } /* Message() */ /**************************************************************** * Name: Pcls * * Synopsis: Clears the screen to the pattern parameter. * * isWindows = TRUE if operating in Windows * * change the cls(RGB(170,0,170)) to desired RGB * ****************************************************************/ Pcls(text pattern, isWindows) { int i; int oldBackground; if (isWindows) cls(RGB(170,0,170)); else { for(i = 0; i <= MaxRow_; i = i + 1) puts(i,0,pattern); } } /* Pcls() */ /**************************************************************** * Global Variable Declarations and Initialization * ****************************************************************/ int CTRLPGUP = 33792, F11 = 34048, F12 = 34304, EOF = -1; short LEFT = 19200, RIGHT = 19712, UP = 18432, DOWN = 20480, HOME = 18176, END = 20224, PGUP = 18688, PGDN = 20736, CTRLPGDN = 30208, F1 = 15104, F2 = 15360, F3 = 15616, F4 = 15872, F5 = 16128, F6 = 16384, F7 = 16640, F8 = 16896, F9 = 17152, F10 = 17408; char ESC = 27, CTRLP = 16, FALSE = 0, TRUE = 1, CR = 13, LF = 10;