/* ** Concordance Programming Language ** ** Copyright (c) 2002 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: ** ** ** Text Date to Date Field.CPL ** ** This CPL will retrieve the contents of a text date from ** a field in the following format: ** ** January 1, 2002 ** ** It will convert this to a Concordance date and copy this ** information into a date field. ** ** The program will prompt for a text field for the source and ** a date field for the destination. This program will overwrite ** any existing dates. */ text gszTextDateToDateField = "Text Date to Date Field"; /*****************************************************/ /* Function: Main */ /* Purpose : Entry point for all programs */ /*****************************************************/ main() { int db, nNext, bDone, nTextField, nDateField; text menustring1, menustring2, menustring3, mymenu[6], pszDatabase; /* Set up the menu */ menustring1 = "[1] Open database : "; menustring2 = "[2] Select source text field : "; menustring3 = "[3] Select destination date field : "; mymenu[0] = gszTextDateToDateField; mymenu[1] = menustring1 + FileName(db.database); mymenu[2] = menustring2; mymenu[3] = menustring3; mymenu[4] = "[G] Go"; mymenu[5] = "[Q] Quit"; if (db.database <> "") nNext = 2; else { db = EOF; nNext = 1; } /* Loop continuously until the user exits */ while (bDone == FALSE) { /* Display the menu */ nNext = menu(5, 10, 13, 70, mymenu, nNext, "123GQ"); /* Process the menu */ switch(nNext) { case 0: /* Quit */ bDone = TRUE; break; /* Open database */ case 1: /* Get the file name */ if (getfile("Open database", "*.DCB", pszDatabase) == CR) { /* Close the old database */ closedb(db); db = EOF; /* Reset the menus */ mymenu[1] = menustring1; nTextField = 0; mymenu[2] = menustring2; nDateField = 0; mymenu[3] = menustring3; /* Open the database */ if ((db = opendb(pszDatabase)) <> EOF) { /* Reset the database name */ mymenu[1] = menustring1 + FileName(db.database); nNext = 2; } } break; /* Select source text field */ case 2: /* Make sure the database is open */ if (db <> EOF) { /* Prompt the user for a field */ if ((nTextField = GetField(db, nTextField)) <> 0) { /* Make sure it is a text or paragraph field */ if ((db.type[nTextField] <> 'T') and (db.type[nTextField] <> 'P')) { /* Tell the user we can only accept text or paragraph */ messageBox("Please select a TEXT or PARAGRAPH field for the source field.", gszTextDateToDateField, MB_ICONEXCLAMATION | MB_OK); nNext = 2; } else { /* Update the menu */ mymenu[2] = menustring2 + db.name[nTextField]; nNext = 3; } } else nNext = 2; } else { /* Tell the user to open a database first */ messageBox("Please open a database first.", gszTextDateToDateField, MB_ICONEXCLAMATION | MB_OK); nNext = 1; } break; /* Select destination date field */ case 3: /* Make sure the database is open */ if (db <> EOF) { /* Prompt the user for a field */ if ((nDateField = GetField(db, nDateField)) <> 0) { /* Make sure it is a date field */ if (db.type[nDateField] <> 'D') { /* Tell the user we can only accept date fields */ messageBox("Please select a DATE field for the destination field.", gszTextDateToDateField, MB_ICONEXCLAMATION | MB_OK); nNext = 3; } else { /* Update the menu */ mymenu[3] = menustring3 + db.name[nDateField]; nNext = 4; } } else nNext = 3; } else { /* Tell the user to open a database first */ messageBox("Please open a database first.", gszTextDateToDateField, MB_ICONEXCLAMATION | MB_OK); nNext = 1; } break; /* Go */ case 4: if ((nNext = TextDateToDateField(db, nTextField, nDateField)) == 0) nNext = 5; break; /* Quit */ case 5: bDone = TRUE; break; } } } /**************************************************************** * Name: TextDateToDateField() * * Synopsis: Main function to process text dates to dates. * ****************************************************************/ TextDateToDateField(int db, nTextField, nDateField) { int nError, i; /* Make sure we have all the information and the user wants to proceed */ if (db.database == "") { /* Tell the user to open a database */ messageBox("Please open a database.", gszTextDateToDateField, MB_ICONEXCLAMATION | MB_OK); nError = 1; } else { if (nTextField == 0) { /* Tell the user to select a text field */ messageBox("Please select a text field.", gszTextDateToDateField, MB_ICONEXCLAMATION | MB_OK); nError = 2; } else { if (nDateField == 0) { /* Tell the user to select a date field */ messageBox("Please select a date field.", gszTextDateToDateField, MB_ICONEXCLAMATION | MB_OK); nError = 3; } else { /* Make sure the user wants to proceed */ if (messageBox("Are you sure you want to convert the dates from the " + db.name[nTextField] + " field " + newline() + "to the " + db.name[nDateField] + " field?" + newline() + newline() + "NOTE: This procedure will OVERWRITE the contents of the " + db.name[nDateField] + " field.", gszTextDateToDateField, MB_ICONQUESTION | MB_YESNOCANCEL) <> IDYES) nError = 4; } } } if (nError == 0) { /* Cycle through the database */ cycle(db) { puts(0, 0, "Processing record " + str(docno(db)) + " "); db->nDateField = FixDate(db->nTextField); } } return(nError); } /**************************************************************** * Name: FixDate() * * Synopsis: Converts a text date to a numeric date. * ****************************************************************/ FixDate(text pszDate) { int nMonth, nDay, nYear, i; if (pszDate <> "") { /* The first part of the string is the month */ if ((i = match(pszDate, " ", 1)) <> 0) { /* Convert the month to a two digit month */ switch(upper(substr(pszDate, 1, 3))) { case "JAN": nMonth = 1; break; case "FEB": nMonth = 2; break; case "MAR": nMonth = 3; break; case "APR": nMonth = 4; break; case "MAY": nMonth = 5; break; case "JUN": nMonth = 6; break; case "JUL": nMonth = 7; break; case "AUG": nMonth = 8; break; case "SEP": nMonth = 9; break; case "OCT": nMonth = 10; break; case "NOV": nMonth = 11; break; case "DEC": nMonth = 12; break; } /* Substring out the month */ pszDate = trim(substr(pszDate, i + 1)); /* Get the day */ nDay = num(pszDate); /* Find the comma */ if ((i = match(pszDate, ",", 1)) <> 0) { /* Get the year */ nYear = num(trim(substr(pszDate, i + 1))); } } } return(ctod(str(nMonth) + "/" + str(nDay) + "/" + str(nYear))); } /**************************************************************** * Name: GetField * * Synopsis: Prompt user for field name. * ****************************************************************/ GetField(int db, next) { int i, n; text field[256]; text screen; if (db.documents >= 0) { field[0] = "Field Type "; for(i = 1; i <= db.fields; i = i +1) switch(db.type[i]) { case 'T' : field[i] = pad(db.name[i],'L',13)+ "Text "; case 'P' : field[i] = pad(db.name[i],'L',13)+ "Paragraph"; case 'N' : field[i] = pad(db.name[i],'L',13)+ "Numeric "; case 'D' : field[i] = pad(db.name[i],'L',13)+ "Date "; } i = db.fields + 1; screen = save(11,30,21,57); while(i > db.fields) i = menu(11, 30, 21, 57, field, next,""); restore(11,30,screen); if (i) next = i; } return(next); } /* GetField() */ /**************************************************************** * 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() */