/***************************************************************************** FILE : $Source: /projects/higgs1/SNNS/CVS/SNNS/xgui/sources/ui_file.c,v $ SHORTNAME : file.c SNNS VERSION : 4.2 PURPOSE : popups a window with all filenames. The user may alter the names and may request a save or load operation with the correspondent kind of file (NET, GUI, PAT, CFG, TXT). NOTES : is called only during initialisation AUTHOR : Ralf Huebner DATE : 06.04.1992 CHANGED BY : Michael Vogt, Guenter Mamier RCS VERSION : $Revision: 2.13 $ LAST CHANGE : $Date: 1998/03/13 16:31:54 $ Copyright (c) 1990-1995 SNNS Group, IPVR, Univ. Stuttgart, FRG Copyright (c) 1996-1998 SNNS Group, WSI, Univ. Tuebingen, FRG ******************************************************************************/ #include #include #include #include #include #include #include #include "ui.h" #ifdef ultrix #ifdef HAVE_SYS_FILE_H #include #endif #else #ifdef HAVE_UNISTD_H #include #endif #endif #ifdef NeXT #ifdef HAVE_SYS_FILE_H #include #endif #include #define dirent direct #endif #include #include #include #include #include #include #include #include "ui_fileP.h" #include "ui_xWidgets.h" #include "ui_confirmer.h" #include "ui_main.h" #include "ui_mainP.h" #include "ui_file.ph" #ifndef S_ISDIR #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR)) #endif #ifdef NeXT char *tempnam(dir,pfx) char *dir, *pfx; { char *filename,*start,*t; int n,count; filename = (char*)malloc(MAXPATHLEN); if((dir == NULL)&&(pfx == NULL)) tmpnam(filename); if((dir == NULL)&&(pfx != NULL)) { t=tmpnam(NULL); start = rindex(t,'/'); *start = '\0'; start++; sprintf(filename,"%s/%s%s",t,pfx,start); } if(dir != NULL) { t=tmpnam(NULL); start = rindex(t,'/'); *start = '\0'; start++; if(pfx != NULL) { if(dir[strlen(dir)-1] != '/') sprintf(filename,"%s/%s%s",dir,pfx,start); else sprintf(filename,"%s%s%s",dir,pfx,start); } else { if(dir[strlen(dir)-1] != '/') sprintf(filename,"%s/%s",dir,start); else sprintf(filename,"%s%s",dir,start); } } return(filename); } #endif /* NeXT */ /***************************************************************************** FUNCTION : ui_loadSelectedFile PURPOSE : loads the current file RETURNS : void NOTES : UPDATE : *****************************************************************************/ static void ui_loadSelectedFile (Widget w, Widget button, caddr_t call_data) { char selectedName[SELECTED_NAME_LENGTH]; ui_xStringFromAsciiWidget(fileBox, selectedName, SELECTED_NAME_LENGTH); ui_xStringFromAsciiWidget(ui_path, ui_pathname, MAX_NAME_LENGTH); switch (currentFileType) { case UI_FILE_NET: strcpy (ui_filenameNET, selectedName); ui_file_loadNet(w, (XtPointer) button, call_data); break; case UI_FILE_PAT: strcpy (ui_filenamePAT, selectedName); ui_file_loadPatterns(w, (XtPointer) button, call_data); break; case UI_FILE_RES: strcpy (ui_filenameRES, selectedName); ui_file_loadResult(w, (XtPointer) button, call_data); break; case UI_FILE_CFG: strcpy (ui_filenameCFG, selectedName); ui_file_loadConfiguration(w, (XtPointer) button, call_data); break; case UI_FILE_TXT: strcpy (ui_filenameTXT, selectedName); ui_file_loadText(w, (XtPointer) button, call_data); break; } } /***************************************************************************** FUNCTION : ui_saveSelectedFile PURPOSE : saves the current file RETURNS : void NOTES : UPDATE : *****************************************************************************/ static void ui_saveSelectedFile (Widget w, Widget button, caddr_t call_data) { char selectedName[SELECTED_NAME_LENGTH]; ui_xStringFromAsciiWidget(fileBox, selectedName, SELECTED_NAME_LENGTH); ui_xStringFromAsciiWidget(ui_path, ui_pathname, MAX_NAME_LENGTH); switch (currentFileType) { case UI_FILE_NET: strcpy (ui_filenameNET, selectedName); ui_file_saveNet(w, (XtPointer) button, call_data); break; case UI_FILE_PAT: strcpy (ui_filenamePAT, selectedName); ui_file_savePatterns(w, (XtPointer) button, call_data); break; case UI_FILE_RES: strcpy (ui_filenameRES, selectedName); ui_file_saveResult(w, (XtPointer) button, call_data); break; case UI_FILE_CFG: strcpy (ui_filenameCFG, selectedName); ui_file_saveConfiguration(w, (XtPointer) button, call_data); break; case UI_FILE_TXT: strcpy (ui_filenameTXT, selectedName); ui_file_saveText(w, (XtPointer) button, call_data); break; } } /***************************************************************************** FUNCTION : ui_expandPath PURPOSE : expands to full pathname if path = dir/../file RETURNS : void NOTES : UPDATE : *****************************************************************************/ static void ui_expandPath (char *path) { char cwd[SELECTED_NAME_LENGTH]; if (*path != '/') { getcwd(cwd, SELECTED_NAME_LENGTH); strcat(cwd, "/"); strcat(cwd, path); strcpy(path, cwd); } } /***************************************************************************** FUNCTION : ui_checkPath PURPOSE : checks the existance of a directory RETURNS : current directory if failed NOTES : UPDATE : *****************************************************************************/ static void ui_checkPath (char *path) { char cwd[SELECTED_NAME_LENGTH]; char errorMsg[2048]; DIR *dirp; dirp = opendir (path); if (dirp == NULL) { getcwd(cwd, SELECTED_NAME_LENGTH); sprintf (errorMsg, "Error! Can't read path:\n%s\nchanging to\n%s\n", ui_pathname, cwd); ui_confirmOk(errorMsg); strcpy(path, cwd); ui_xSetString(ui_path, path); } else closedir (dirp); } /***************************************************************************** FUNCTION : ui_strSort PURPOSE : sorts the current directory RETURNS : void NOTES : modified quicksort algorithm for strings UPDATE : *****************************************************************************/ static void ui_strSort (int left, int right) { int i, j; char *px, *pw; do { px = dirPtr[(left+right) / 2]; i = left; j = right; do { while (strcmp(dirPtr[i], px) < 0) i++; while (strcmp(dirPtr[j], px) > 0) j--; if (i > j) break; pw = dirPtr[i]; dirPtr[i] = dirPtr[j]; dirPtr[j] = pw; } while (++i <= --j); if (j - left < right - i) { if (left < j) ui_strSort (left, j); left = i; j = right; } else { if (i < right) ui_strSort (i, right); right = j; i = left; } } while (left < right); } /***************************************************************************** FUNCTION : ui_readDirectory PURPOSE : reads the contents of the current directory RETURNS : void NOTES : puts 0x01 and 0x02 around the name if entry is a subdirectory UPDATE : *****************************************************************************/ static void ui_readDirectory (void) { struct dirent *dp; DIR *dirp; int i; char *sptr, *pptr; char newpath[MAX_NAME_LENGTH]; char tempName[MAX_NAME_LENGTH]; dirEntries[0] = '\0'; dirp = opendir (ui_pathname); stat_buf = (struct stat *) malloc (sizeof (struct stat)); while((dp = readdir(dirp)) != NULL){ strcpy (newpath, ui_pathname); strcat (newpath, "/"); strcat (newpath, dp->d_name); stat (newpath, stat_buf); if (S_ISDIR(stat_buf->st_mode) AND (access(newpath,R_OK | X_OK)==0)) { strcat (dirEntries, "\1"); strcat (dirEntries, dp->d_name); strcat (dirEntries, "\2"); strcat (dirEntries, "\n"); } else { sptr = strrchr (dp->d_name, '.'); if (sptr != NULL) { if (strcmp(sptr, extMask) == 0) { strcpy (tempName, dp->d_name); pptr = strrchr (tempName, '.'); *pptr = '\0'; strcat (dirEntries, tempName); strcat (dirEntries, "\n"); } } } } closedir (dirp); free (stat_buf); maxEntries = 1; dirPtr[0] = dirEntries; for (sptr = dirEntries; *sptr != '\0'; sptr++) { if (*sptr == '\n') { *sptr = '\0'; dirPtr[maxEntries] = sptr + 1; maxEntries++; if (maxEntries == MAX_DIR_ENTRIES) { ui_confirmOk("Error! Directory too large"); return; } } } ui_strSort (0, maxEntries-2); sortedDirEntries[0] = '\0'; for (i=0; itype) { case ButtonRelease: if (event->xbutton.button == 1) { XawTextGetSelectionPos(selectorBox, &start, &end); if (start != end) { n = 0; XtSetArg(args[n], XtNtextSource, &src); n++; XtGetValues (selectorBox, args, n); found = XawTextSourceRead(src, (int) start, &textBlock, (int) (end-start)); if (found > 0) { nlStarts = nlEnds = FALSE; strncpy(selectedName, textBlock.ptr-1, (unsigned int) (end-start+2)); selectedName[end-start+2] = '\0'; if (selectedName[0] == '\n') nlStarts = TRUE; if (selectedName[strlen(selectedName)-1] == '\n') nlEnds = TRUE; if (nlStarts AND nlEnds) { for (i=0; i