autoScriptEditor ScriptEditor_createFromScript_canBeNull (Editor environment, Script script) { try { for (long ieditor = 1; ieditor <= theReferencesToAllOpenScriptEditors.size; ieditor ++) { ScriptEditor editor = theReferencesToAllOpenScriptEditors.at [ieditor]; if (MelderFile_equal (& script -> file, & editor -> file)) { Editor_raise (editor); Melder_appendError (U"The script ", & script -> file, U" is already open and has been moved to the front."); if (editor -> dirty) Melder_appendError (U"Choose \"Reopen from disk\" if you want to revert to the old version."); Melder_flushError (); return autoScriptEditor(); // safe null } } autostring32 text = MelderFile_readText (& script -> file); autoScriptEditor me = ScriptEditor_createFromText (environment, text.peek()); MelderFile_copy (& script -> file, & my file); Thing_setName (me.peek(), Melder_fileToPath (& script -> file)); return me; } catch (MelderError) { Melder_throw (U"Script window not created."); } }
static void openDocument (TextEditor me, MelderFile file) { if (theOpenTextEditors) { for (long ieditor = 1; ieditor <= theOpenTextEditors -> size; ieditor ++) { TextEditor editor = (TextEditor) theOpenTextEditors -> item [ieditor]; if (editor != me && MelderFile_equal (file, & editor -> file)) { Editor_raise (editor); Melder_appendError (U"Text file ", file, U" is already open."); forget (me); // don't forget me before Melder_appendError, because "file" is owned by one of my dialogs Melder_flushError (); return; } } } autostring32 text = MelderFile_readText (file); GuiText_setString (my textWidget, text.peek()); /* * GuiText_setString has invoked the changeCallback, * which has set `my dirty` to `true`. Fix this. */ my dirty = false; MelderFile_copy (file, & my file); Thing_setName (me, Melder_fileToPath (file)); }
ScriptEditor ScriptEditor_createFromScript (Editor environment, Script script) { try { if (theScriptEditors) { for (long ieditor = 1; ieditor <= theScriptEditors -> size; ieditor ++) { ScriptEditor editor = (ScriptEditor) theScriptEditors -> item [ieditor]; if (MelderFile_equal (& script -> file, & editor -> file)) { editor -> raise (); Melder_error_ ("The script ", & script -> file, " is already open and has been moved to the front."); if (editor -> dirty) Melder_error_ ("Choose \"Reopen from disk\" if you want to revert to the old version."); Melder_flushError (NULL); return NULL; // safe NULL } } } autostring text = MelderFile_readText (& script -> file); autoScriptEditor me = ScriptEditor_createFromText (environment, text.peek()); MelderFile_copy (& script -> file, & my file); Thing_setName (me.peek(), Melder_fileToPath (& script -> file)); return me.transfer(); } catch (MelderError) { Melder_throw ("Script window not created."); } }
TableOfReal TableOfReal_readFromHeaderlessSpreadsheetFile (MelderFile file) { try { autostring string = MelderFile_readText (file); long nrow, ncol, nelements; /* * Count columns. */ ncol = 0; wchar_t *p = & string [0]; for (;;) { wchar_t kar = *p++; if (kar == '\n' || kar == '\0') break; if (kar == ' ' || kar == '\t') continue; ncol ++; do { kar = *p++; } while (kar != ' ' && kar != '\t' && kar != '\n' && kar != '\0'); if (kar == '\n' || kar == '\0') break; } ncol --; if (ncol < 1) Melder_throw ("No columns."); /* * Count elements. */ p = & string [0]; nelements = 0; for (;;) { wchar_t kar = *p++; if (kar == '\0') break; if (kar == ' ' || kar == '\t' || kar == '\n') continue; nelements ++; do { kar = *p++; } while (kar != ' ' && kar != '\t' && kar != '\n' && kar != '\0'); if (kar == '\0') break; } /* * Check if all columns are complete. */ if (nelements == 0 || nelements % (ncol + 1) != 0) Melder_throw ("The number of elements (", nelements, ") is not a multiple of the number of columns plus 1 (", ncol + 1, ")."); /* * Create empty table. */ nrow = nelements / (ncol + 1) - 1; autoTableOfReal me = TableOfReal_create (nrow, ncol); /* * Read elements. */ p = & string [0]; while (*p == ' ' || *p == '\t') { Melder_assert (*p != '\0'); p ++; } while (*p != ' ' && *p != '\t') { Melder_assert (*p != '\0'); p ++; } // ignore the header of the zeroth column ("rowLabel" perhaps) for (long icol = 1; icol <= ncol; icol ++) { while (*p == ' ' || *p == '\t') { Melder_assert (*p != '\0'); p ++; } static MelderString buffer = { 0 }; MelderString_empty (& buffer); while (*p != ' ' && *p != '\t' && *p != '\n') { MelderString_appendCharacter (& buffer, *p); p ++; } TableOfReal_setColumnLabel (me.peek(), icol, buffer.string); MelderString_empty (& buffer); } for (long irow = 1; irow <= nrow; irow ++) { while (*p == ' ' || *p == '\t' || *p == '\n') { Melder_assert (*p != '\0'); p ++; } static MelderString buffer = { 0 }; MelderString_empty (& buffer); while (*p != ' ' && *p != '\t') { MelderString_appendCharacter (& buffer, *p); p ++; } TableOfReal_setRowLabel (me.peek(), irow, buffer.string); MelderString_empty (& buffer); for (long icol = 1; icol <= ncol; icol ++) { while (*p == ' ' || *p == '\t' || *p == '\n') { Melder_assert (*p != '\0'); p ++; } MelderString_empty (& buffer); while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') { MelderString_appendCharacter (& buffer, *p); p ++; } my data [irow] [icol] = Melder_atof (buffer.string); /* If cell contains a string, this will be 0. */ MelderString_empty (& buffer); } } return me.transfer(); } catch (MelderError) { Melder_throw ("TableOfReal: tab-separated file ", file, " not read."); } }