//---------------------------------------------------------------------------------------- nsresult nsFileSpec::MoveToDir(const nsFileSpec& inNewParentDirectory) //---------------------------------------------------------------------------------------- { // We can only copy into a directory, and (for now) can not copy entire directories nsresult result = NS_FILE_FAILURE; if (inNewParentDirectory.IsDirectory() && !IsDirectory()) { char *leafname = GetLeafName(); nsSimpleCharString destPath(inNewParentDirectory.GetCString()); destPath += "/"; destPath += leafname; nsCRT::free(leafname); result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), (const char*)destPath)); if (result == NS_OK) { // cast to fix const-ness ((nsFileSpec*)this)->Delete(PR_FALSE); *this = inNewParentDirectory + GetLeafName(); } } return result; }
//---------------------------------------------------------------------------------------- void nsFileSpec::MakeUnique() //---------------------------------------------------------------------------------------- { if (!Exists()) return; char* leafName = GetLeafName(); if (!leafName) return; char* lastDot = strrchr(leafName, '.'); char* suffix = ""; if (lastDot) { suffix = nsCRT::strdup(lastDot); // include '.' *lastDot = '\0'; // strip suffix and dot. } const int kMaxRootLength = nsFileSpecHelpers::kMaxCoreLeafNameLength - strlen(suffix) - 1; if ((int)strlen(leafName) > (int)kMaxRootLength) leafName[kMaxRootLength] = '\0'; for (short indx = 1; indx < 1000 && Exists(); indx++) { // start with "Picture-1.jpg" after "Picture.jpg" exists char newName[nsFileSpecHelpers::kMaxFilenameLength + 1]; sprintf(newName, "%s-%d%s", leafName, indx, suffix); SetLeafName(newName); } if (*suffix) nsCRT::free(suffix); nsCRT::free(leafName); } // nsFileSpec::MakeUnique
//---------------------------------------------------------------------------------------- nsresult nsFileSpec::MoveToDir(const nsFileSpec& inNewParentDirectory) //---------------------------------------------------------------------------------------- { // We can only copy into a directory, and (for now) can not copy entire directories if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); nsSimpleCharString destPath(inNewParentDirectory.GetCString()); destPath += "\\"; destPath += leafname; nsCRT::free(leafname); // MoveFile returns non-zero if succeeds int copyOK = MoveFile(GetCString(), destPath); if (copyOK) { *this = inNewParentDirectory + GetLeafName(); return NS_OK; } } return NS_FILE_FAILURE; } // nsFileSpec::MoveToDir
//---------------------------------------------------------------------------------------- PRBool nsFileSpec::IsHidden() const //---------------------------------------------------------------------------------------- { PRBool hidden = PR_TRUE; char *leafname = GetLeafName(); if (nsnull != leafname) { if ((!strcmp(leafname, ".")) || (!strcmp(leafname, ".."))) { hidden = PR_FALSE; } nsCRT::free(leafname); } return hidden; } // nsFileSpec::IsHidden
/** * * @brief * * @param[out] tree * created upgma tree. will be allocated here. use FreeMuscleTree() * to free * @param[in] mseq * @param[in] ftree * * @return non-zero on error * */ int GuideTreeFromFile(tree_t **tree, mseq_t *mseq, char *ftree) { int iNodeCount; int iNodeIndex; (*tree) = (tree_t *) CKMALLOC(1 * sizeof(tree_t)); if (MuscleTreeFromFile((*tree), ftree)!=0) { Log(&rLog, LOG_ERROR, "%s", "MuscleTreeFromFile failed"); return -1; } /* Make sure tree is rooted */ if (!IsRooted((*tree))) { Log(&rLog, LOG_ERROR, "User tree must be rooted"); return -1; } if ((int)GetLeafCount((*tree)) != mseq->nseqs) { Log(&rLog, LOG_ERROR, "User tree does not match input sequences"); return -1; } /* compare tree labels and sequence names and set leaf-ids */ iNodeCount = GetNodeCount((*tree)); for (iNodeIndex = 0; iNodeIndex < iNodeCount; ++iNodeIndex) { char *LeafName; int iSeqIndex; if (!IsLeaf(iNodeIndex, (*tree))) continue; LeafName = GetLeafName(iNodeIndex, (*tree)); if ((iSeqIndex=FindSeqName(LeafName, mseq))==-1) { Log(&rLog, LOG_ERROR, "Label '%s' in tree could not be found in sequence names", LeafName); return -1; } SetLeafId((*tree), iNodeIndex, iSeqIndex); } if (rLog.iLogLevelEnabled <= LOG_DEBUG) { Log(&rLog, LOG_DEBUG, "tree logging..."); LogTree((*tree), LogGetFP(&rLog, LOG_DEBUG)); } return 0; }
//---------------------------------------------------------------------------------------- nsresult nsFileSpec::CopyToDir(const nsFileSpec& inParentDirectory) const //---------------------------------------------------------------------------------------- { // We can only copy into a directory, and (for now) can not copy entire directories nsresult result = NS_FILE_FAILURE; if (inParentDirectory.IsDirectory() && (! IsDirectory() ) ) { char *leafname = GetLeafName(); nsSimpleCharString destPath(inParentDirectory.GetCString()); destPath += "/"; destPath += leafname; nsCRT::free(leafname); result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), destPath)); } return result; } // nsFileSpec::CopyToDir
//---------------------------------------------------------------------------------------- PRBool nsFileSpec::IsHidden() const //---------------------------------------------------------------------------------------- { PRBool hidden = PR_FALSE; char *leafname = GetLeafName(); if (nsnull != leafname) { // rjc: don't return ".", "..", or any file/directory that begins with a "." /* if ((!strcmp(leafname, ".")) || (!strcmp(leafname, ".."))) */ if (leafname[0] == '.') { hidden = PR_TRUE; } nsCRT::free(leafname); } return hidden; } // nsFileSpec::IsHidden
NS_IMETHODIMP nsLocalFile::CreateUnique(uint32_t aType, uint32_t aAttributes) { nsresult rv; bool longName; #ifdef XP_WIN nsAutoString pathName, leafName, rootName, suffix; rv = GetPath(pathName); #else nsAutoCString pathName, leafName, rootName, suffix; rv = GetNativePath(pathName); #endif if (NS_FAILED(rv)) { return rv; } longName = (pathName.Length() + kMaxSequenceNumberLength > kMaxFilenameLength); if (!longName) { rv = Create(aType, aAttributes); if (rv != NS_ERROR_FILE_ALREADY_EXISTS) { return rv; } } #ifdef XP_WIN rv = GetLeafName(leafName); if (NS_FAILED(rv)) { return rv; } const int32_t lastDot = leafName.RFindChar(char16_t('.')); #else rv = GetNativeLeafName(leafName); if (NS_FAILED(rv)) { return rv; } const int32_t lastDot = leafName.RFindChar('.'); #endif if (lastDot == kNotFound) { rootName = leafName; } else { suffix = Substring(leafName, lastDot); // include '.' rootName = Substring(leafName, 0, lastDot); // strip suffix and dot } if (longName) { int32_t maxRootLength = (kMaxFilenameLength - (pathName.Length() - leafName.Length()) - suffix.Length() - kMaxSequenceNumberLength); // We cannot create an item inside a directory whose name is too long. // Also, ensure that at least one character remains after we truncate // the root name, as we don't want to end up with an empty leaf name. if (maxRootLength < 2) { return NS_ERROR_FILE_UNRECOGNIZED_PATH; } #ifdef XP_WIN // ensure that we don't cut the name in mid-UTF16-character rootName.SetLength(NS_IS_LOW_SURROGATE(rootName[maxRootLength]) ? maxRootLength - 1 : maxRootLength); SetLeafName(rootName + suffix); #else if (NS_IsNativeUTF8()) { // ensure that we don't cut the name in mid-UTF8-character // (assume the name is valid UTF8 to begin with) while (UTF8traits::isInSeq(rootName[maxRootLength])) { --maxRootLength; } // Another check to avoid ending up with an empty leaf name. if (maxRootLength == 0 && suffix.IsEmpty()) { return NS_ERROR_FILE_UNRECOGNIZED_PATH; } } rootName.SetLength(maxRootLength); SetNativeLeafName(rootName + suffix); #endif nsresult rv = Create(aType, aAttributes); if (rv != NS_ERROR_FILE_ALREADY_EXISTS) { return rv; } } for (int indx = 1; indx < 10000; ++indx) { // start with "Picture-1.jpg" after "Picture.jpg" exists #ifdef XP_WIN SetLeafName(rootName + NS_ConvertASCIItoUTF16(nsPrintfCString("-%d", indx)) + suffix); #else SetNativeLeafName(rootName + nsPrintfCString("-%d", indx) + suffix); #endif rv = Create(aType, aAttributes); if (NS_SUCCEEDED(rv) || rv != NS_ERROR_FILE_ALREADY_EXISTS) { return rv; } } // The disk is full, sort of return NS_ERROR_FILE_TOO_BIG; }
/* XOPSaveFileDialog(prompt, fileFilterStr, fileIndexPtr, initialDir, defaultExtensionStr, filePath) Displays the save file dialog. Returns 0 if the user provides a file name or -1 if the user cancels or another non-zero number in the event of an error. Returns the full path to the file via filePath. filePath is both an input and an output as explained below. In the event of a cancel, filePath is unmodified. filePath is a Macintosh HFS path on Macintosh and a Windows path on Windows. On Windows, prompt sets the dialog caption. On Macintosh, it sets a prompt string in the dialog. fileFilterStr is now used to control the contents of the Format popup menu in the Save File dialog. On Macintosh, if there is only one format in which you can save the file, pass "" for fileFilterStr. This will cause the Format menu to be hidden. If you can save the file in more than one format, pass a string like this: "Plain Text:TEXT:.txt;Igor Text:IGTX:.itx;" This would give you a Format menu like this: Plain Text Igor Text fileFilterStr on Macintosh fileFilterStr consists of sections terminated by a semicolon. For example, here is one section: "Data Files:TEXT:.dat;" Each section consists of three components: a menu item string (e.g., Data Files) to be displayed in the Format popup menu, a Macintosh file type (e.g., TEXT), and an extension (e.g., .dat). At present, only the menu item string and extension are used. The Macintosh file type is currently not used. If there is no meaningful Macintosh file type, leave the file type component empty. If there is no meaningful extension, leave the extension component empty. fileFilterStr on Windows On Windows, fileFilterStr identifies the types of files to display and the types of files that can be created. It is constructed as for the lpstrFilter field of the OPENFILENAME structure for the Windows GetSaveFileName routine. For example, to allow the user to save as a text file or as an Igor Text file, use "Text Files (*.txt)\0*.txt\0Igor Text Files (*.itx)\0*.itx\0\0". Note that the string ends with two null characters (\0\0). If fileFilterStr is "", this behaves the same as "Text Files (*.txt)\0*.txt\0\0". fileIndexPtr it is ignored if it is NULL. If it is not NULL, then *fileIndexPtr is the one-based index of the file type filter to be initially selected. In the example given above, setting *fileIndexPtr to 2 would select the Igor Text file type on entry to the dialog. On exit from the dialog, *fileIndexPtr is set to the index of the file type string that the user last selected. initialDir can be "" or it can point to a full path to a directory. It determines the directory that will be initially displayed in the save file dialog. If "", the directory will be the last directory that was seen in the open or save file dialogs. If initialDir points to a valid path to a directory, then this directory will be initially displayed in the dialog. On Macintosh, initialDir is a Macintosh HFS path. On Windows, it is a Windows path. defaultExtensionStr points to the extension to be added to the file name if the user does not enter an extension. For example, pass "txt" to have ".txt" appended if the user does not enter an extension. If you don't want any extension to be added in this case, pass NULL. Prior to XOP Toolkit 6.00, defaultExtensionStr was ignored on Macintosh. Returns via filePath the full path to the file that the user chose or "" if the user cancelled. The path is a Macintosh HFS path on Macintosh and a Windows path on Windows. filePath must point to a buffer of at least MAX_PATH_LEN+1 bytes. On Windows and Macintosh, the initial value of filePath sets the initial contents of the File Name edit control in the save file dialog. The following values are valid: "" If there is no initial file name a file name a full Mac or Win path to a file In the event of an error other than a cancel, XOPSaveFileDialog displays an error dialog. This should never or rarely happen. WINDOWS NOTES The dialog will appear in the upper left corner of the screen. This is because Windows provides no straight-forward way to set the position of the dialog. Thread Safety: XOPSaveFileDialog is not thread-safe. */ int XOPSaveFileDialog( const char* prompt, const char* fileFilterStr, int* fileIndexPtr, const char* initialDir, const char* defaultExtensionStr, char filePath[MAX_PATH_LEN+1]) { OPENFILENAME ofn; char filePath2[MAX_PATH_LEN+1]; char initialDir2[MAX_PATH_LEN+1]; if (!CheckRunningInMainThread("XOPSaveFileDialog")) return NOT_IN_THREADSAFE; if (*fileFilterStr == 0) fileFilterStr = "Text Files (*.txt)\0*.txt\0\0"; if (*initialDir == 0) { GetStandardFileWinPath(initialDir2); // Get Igor's save file dialog directory. } else { strcpy(initialDir2, initialDir); SetStandardFileWinPath(initialDir); // Sets initial directory for next save file dialog. This will be overridden below, but not if the user cancels. } /* HR, 040928, XOP Toolkit 5.04 Previously this copied filePath to filePath2. This was correct because the filePath parameter was supposed to be either "" or just the proposed file name. However, I incorrectly passed a full path for the filePath parameter in all of the sample XOPs. This mistake undoubtedly leaked into users' XOPs. Therefore I now allow filePath to be either "", just a file name, or a full path. */ // strcpy(filePath2, filePath); // HR, 010815: Previously filePath2 was set to "" which prevented the File Name item in the Windows Open File dialog from being preset as the comment above says it should be. GetLeafName(filePath, filePath2); MemClear(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = IgorClientHWND(); ofn.lpstrFile = filePath2; ofn.nMaxFile = MAX_PATH_LEN+1; ofn.lpstrFilter = fileFilterStr; ofn.nFilterIndex = fileIndexPtr==NULL ? 1 : *fileIndexPtr; ofn.lpstrDefExt = defaultExtensionStr; ofn.lpstrTitle = prompt; ofn.lpstrFileTitle = NULL; ofn.lpstrInitialDir = initialDir2; ofn.lpfnHook = OpenOrSaveFileNameHook; // Needed to set position of the dialog. Otherwise, it is in top/left corner of screen. ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; ofn.Flags |= OFN_EXPLORER; ofn.Flags |= OFN_ENABLEHOOK; // Needed so that hook will be called. ofn.Flags |= OFN_ENABLESIZING; // HR, 090121: Added this to get resizeable dialog. ofn.Flags |= OFN_HIDEREADONLY; ofn.Flags |= OFN_NOCHANGEDIR; // Changing the current directory causes problems. e.g., if set to floppy disk and the floppy is removed, the system slows down. if (GetSaveFileName(&ofn) == 0) { int err; err = CommDlgExtendedError(); // err will be zero if cancel. if (err == 0) return -1; // We got an error other than cancel. *filePath2 = 0; // HR, 021114: Clear possible bad fields *initialDir2 = 0; // and try again. if (GetSaveFileName(&ofn) != 0) { // Succeeded this time? err = 0; } else { if (CommDlgExtendedError() == 0) return -1; // User canceled. // Report the original error. err = WindowsErrorToIgorError(err); IgorError("XOPSaveFileDialog", err); return err; } } if (fileIndexPtr != NULL) *fileIndexPtr = ofn.nFilterIndex; strcpy(filePath, filePath2); SetStandardFileWinPath(filePath); // Update Igor's open file dialog directory. return 0; }
NODE *create_tree_sub (const char **tree_str, NODE * parent, TREE * tree) { NODE *node,*node_new; char c; int bufflen; double l; char * name; node = CreateNode (); if (parent != NULL) { node->nbran++; node->branch[0] = parent; } else{ (*tree_str)++; } while ( (c = GetTreeElt (tree_str)) != EOF) { if (c == '(') { node->branch[node->nbran] = create_tree_sub (tree_str, node, tree); (node->branch[node->nbran])->bnumber = tree->n_br; tree->branches[tree->n_br++] = node->branch[node->nbran++]; if ( node->nbran >= node->maxbran - 1 ){ ExtendNode(node); } } else if (c == ')') { node->branch[node->nbran] = NULL; return node; } else if (c == ',') { node_new = CreateNode(); node->branch[node->nbran] = node_new; node_new->bnumber = tree->n_br; tree->branches[tree->n_br++] = node->branch[node->nbran]; CHILD (node, node->nbran)->branch[0] = node; CHILD (node, node->nbran)->branch[1] = NULL; CHILD (node, node->nbran)->nbran = 1; name = GetLeafName(tree_str); bufflen = 1+strlen(name); node_new->name = malloc (bufflen*sizeof(char)); strncpy(node_new->name,name,bufflen); if ( insertelt_rbtree(tree->leaves,name,node_new) ){ fprintf(stderr,"Species name %s already used in tree. Please make unique and rerun program.\n",name); exit(EXIT_FAILURE); } tree->n_sp++; node->nbran++; if ( node->nbran >= node->maxbran - 1){ ExtendNode(node); } } else if (c == ':') { l = GetLength (tree_str); node->blength[node->nbran - 1] = l; CHILD (node, node->nbran - 1)->blength[0] = l; } } fprintf (stderr,"Error creating tree.\n"); fprintf (stderr,"Tree string is %s.\n",*tree_str); fprintf (stderr,"This is probably a bug. Please report to [email protected]\n"); print_tree(stderr, parent,parent,tree); exit(EXIT_FAILURE); return node; }