bool MCImageRepGetReferencedWithScale(const char *p_base, const char *p_extension, MCGFloat p_scale, MCImageRep *&r_rep)
{
	bool t_success;
	t_success = true;
	
	MCImageRep *t_rep;
	t_rep = nil;
	
	char *t_default_path;
	t_default_path = nil;
	
	const char **t_labels;
	t_labels = nil;
	
	if (t_success)
		t_success = MCImageGetLabelsForScale(p_scale, t_labels);
	
	// construct default path as base path with first tag for the given scale
	if (t_success)
		t_success = MCCStringFormat(t_default_path, "%s%s%s", p_base, t_labels[0], p_extension);
	
	if (t_success)
	{
		MCCachedImageRep *t_cached_rep;
		t_cached_rep = nil;
		
		if (MCCachedImageRep::FindWithKey(t_default_path, t_cached_rep))
			t_rep = t_cached_rep->Retain();
		// not in cache, so see if default path exists.
		else if (MCS_exists(t_default_path, True))
			t_success = MCImageRepCreateReferencedWithSearchKey(t_default_path, t_default_path, t_rep);
		// else loop through remaining labels and check for matching files
		else
		{
			for (uint32_t i = 1; t_success && t_rep == nil && t_labels[i] != nil; i++)
			{
				char *t_scaled_path;
				t_scaled_path = nil;
				
				t_success = MCCStringFormat(t_scaled_path, "%s%s%s", p_base, t_labels[i], p_extension);
				
				if (t_success && MCS_exists(t_scaled_path, True))
					t_success = MCImageRepCreateReferencedWithSearchKey(t_scaled_path, t_default_path, t_rep);
				
				MCCStringFree(t_scaled_path);
			}
		}
	}
	
	MCCStringFree(t_default_path);
	
	if (t_success)
		r_rep = t_rep;
	
	return t_success;
}
示例#2
0
static bool load_custom_font_file_into_buffer_from_path(const char *p_path, char *&r_buffer, uint32_t &r_size)
{     
    bool t_success;
    t_success = true;
    
    char *t_font_path;
    t_font_path = nil;
    if (t_success)
        t_success = MCCStringFormat(t_font_path, "%s/%s%s", MCcmd, s_font_folder, p_path);
    
    if (t_success)
        t_success = MCS_exists(t_font_path, true);
    
    IO_handle t_font_file_handle;
    t_font_file_handle = nil;
    if (t_success)
	{
        t_font_file_handle = MCS_open(t_font_path, IO_READ_MODE, false, false, 0);
		t_success = t_font_file_handle != nil;
	}
    
    uint32_t t_file_size;
    t_file_size = 0;
    char *t_buffer;
    t_buffer = nil;
	if (t_success)
	{
		t_file_size = MCS_fsize(t_font_file_handle);
		t_success = MCMemoryAllocate(t_file_size + 1, t_buffer);
	}
    
	if (t_success)
	{
		IO_stat t_read_stat;
		uint32_t t_bytes_read;
        t_bytes_read = 0;
		while (t_success && t_bytes_read < t_file_size)
		{
			uint32_t t_count;
			t_count = t_file_size - t_bytes_read;
			t_read_stat = MCS_read(t_buffer + t_bytes_read, 1, t_count, t_font_file_handle);
			t_bytes_read += t_count;
			t_success = (t_read_stat == IO_NORMAL || (t_read_stat == IO_EOF && t_bytes_read == t_file_size));
		}
	}
    
    if (t_success)
    {
        r_buffer = t_buffer;
        r_size = t_file_size;
    }
    else
        /*UNCHECKED */ MCMemoryDelete(t_buffer);
    
    /*UNCHECKED */ MCCStringFree(t_font_path);
    
    return t_success;
}
示例#3
0
文件: w32dc.cpp 项目: kant/livecode
// TD-2013-07-01 [[ DynamicFonts ]]
bool MCScreenDC::loadfont(const char *p_path, bool p_globally, void*& r_loaded_font_handle)
{
	bool t_success = true;
    DWORD t_private = NULL;
    
    if (!p_globally)
        t_private = FR_PRIVATE;
    
	if (t_success)
		t_success = (MCS_exists(p_path, True) == True);
	
	if (t_success)
		t_success = (AddFontResourceExA(p_path, t_private, 0) != 0);
    
	if (t_success && p_globally)
		PostMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    
	return t_success;
}
示例#4
0
// TD-2013-07-01 [[ DynamicFonts ]]
bool MCScreenDC::loadfont(MCStringRef p_path, bool p_globally, void*& r_loaded_font_handle)
{
	bool t_success = true;
    DWORD t_private = NULL;
    
    if (!p_globally)
        t_private = FR_PRIVATE;
    
	if (t_success)
		t_success = (MCS_exists(p_path, true) == True);
	
    MCAutoStringRefAsWString t_wide_path;
    if (t_success)
        t_success = t_wide_path . Lock(p_path);
    
	if (t_success)
		t_success = (AddFontResourceExW(*t_wide_path, t_private, 0) != 0);
    
	if (t_success && p_globally)
		PostMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    
	return t_success;
}
示例#5
0
文件: srvcgi.cpp 项目: bduck/livecode
static void cgi_fix_path_variables()
{
	char *t_path, *t_path_end;
	t_path = strdup(MCS_getenv("PATH_TRANSLATED"));
	t_path_end = t_path + strlen(t_path);

#ifdef _WINDOWS_SERVER
	for(uint32_t i = 0; t_path[i] != '\0'; i++)
		if (t_path[i] == '\\')
			t_path[i] = '/';
#endif

	char t_sep;
	t_sep = '\0';

	while (!MCS_exists(t_path, True))
	{
		char *t_new_end;
		t_new_end = strrchr(t_path, '/');
		*t_path_end = t_sep;
		if (t_new_end == NULL)
		{
			t_sep = '\0';
			break;
		}
		t_path_end = t_new_end;
		t_sep = *t_path_end;
		*t_path_end = '\0';
	}

	MCS_setenv("PATH_TRANSLATED", t_path);
	*t_path_end = t_sep;
	MCS_setenv("PATH_INFO", t_path_end);

	free(t_path);
}
示例#6
0
int MCA_ask_file_with_types(MCExecPoint& ep, const char *p_title, const char *p_prompt, char * const p_types[], uint4 p_type_count, const char *p_initial, unsigned int p_options)
{
	if (!MCModeMakeLocalWindows())
	{
		bool t_plural = (p_options & MCA_OPTION_PLURAL) != 0;
		char *t_resolved_path = MCS_resolvepath(p_initial);
		char **t_rtypes;
		if (types_to_remote_types(p_types, p_type_count, t_rtypes))
		{
			MCRemoteFileDialog(ep, p_title, p_prompt, t_rtypes, p_type_count * 2, NULL, t_resolved_path, true, t_plural);
			MCCStringArrayFree(t_rtypes, p_type_count * 2);
		}
		delete t_resolved_path;
		return 1;
	}

	GtkWidget *dialog ;
	
	dialog = create_open_dialog( p_title == NULL  ? p_prompt : p_title, GTK_FILE_CHOOSER_ACTION_SAVE );

	if ( p_type_count > 0 ) 
		add_dialog_filters ( dialog, p_types , p_type_count );

	// If we are given an initial
	if (p_initial != nil)
	{
		if (MCS_exists(p_initial, True))
		{
			char *t_path;
			t_path = MCS_resolvepath(p_initial);
			gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), t_path);
			delete t_path;
		}
		else
		{
			char *t_folder;
			const char *t_name;
			if (strchr(p_initial, '/') == NULL)
			{
				t_folder = NULL;
				t_name = p_initial;
			}
			else
			{
				t_folder = strdup(p_initial);
				strrchr(t_folder, '/')[0] = '\0';
				t_name = strrchr(p_initial, '/') + 1;
				if (MCS_exists(t_folder, False))
				{
					char *t_new_folder;
					t_new_folder = MCS_resolvepath(t_folder);
					delete t_folder;
					t_folder = t_new_folder;
				}
				else
					t_folder = NULL;
			}
			gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), t_folder == NULL ? G_last_saved_path : t_folder);
			gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), t_name);
			delete t_folder;
		}
	}
	else
	{
		gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), G_last_saved_path);
	}
	
	run_dialog ( dialog, ep) ;

	MCresult -> clear();
	MCresult -> copysvalue(get_current_filter_name ( dialog ) );
	

	if (G_last_saved_path != NULL)
		g_free(G_last_saved_path);
	G_last_saved_path = gtk_file_chooser_get_current_folder ( GTK_FILE_CHOOSER ( dialog ) ) ;

	close_dialog ( dialog ) ;
        
        return(1);
	
}
示例#7
0
static int MCA_do_file_dialog(MCExecPoint& ep, const char *p_title, const char *p_prompt, const char *p_filter, const char *p_initial, unsigned int p_options)
{
	int t_result = 0;

	char *t_initial_file;
	t_initial_file = NULL;

	char *t_initial_folder;
	t_initial_folder = NULL;

	ep . clear();

	if (*p_initial != '\0')
	{
		char *t_initial_clone;
		t_initial_clone = strdup(p_initial);
		MCU_w32path2std(t_initial_clone);
		MCU_fix_path(t_initial_clone);

		if (MCS_exists(t_initial_clone, False))
			t_initial_folder = t_initial_clone;
		else if ((p_options & MCA_OPTION_SAVE_DIALOG) != 0)
		{
			t_initial_file = strrchr(t_initial_clone, '/');
			if (t_initial_file == NULL)
			{
				if (strlen(t_initial_clone) != 0)
					t_initial_file = t_initial_clone;
			}
			else
			{
				*t_initial_file = '\0';
				t_initial_file++;
				
				if (t_initial_file[0] == '\0')
					t_initial_file = NULL;

				if (MCS_exists(t_initial_clone, False))
					t_initial_folder = t_initial_clone;
			}
		}
		else
		{
			char *t_leaf;
			t_leaf = strrchr(t_initial_clone, '/');
			if (t_leaf != NULL)
			{
				*t_leaf = '\0';
				if (MCS_exists(t_initial_clone, False))
					t_initial_folder = t_initial_clone;
			}
		}

		t_initial_file = strdup(t_initial_file);
		t_initial_folder = MCS_resolvepath(t_initial_folder);

		delete t_initial_clone;
	}

	if (!MCModeMakeLocalWindows())
	{
		char ** t_filters = NULL;
		uint32_t t_filter_count = 0;

		if (p_filter != NULL)
		{
			const char *t_strptr = p_filter;
			while (t_strptr[0] != '\0')
			{
				t_filter_count++;
				t_filters = (char**)realloc(t_filters, t_filter_count * sizeof(char*));
				t_filters[t_filter_count - 1] = (char *)t_strptr;
				t_strptr += strlen(t_strptr) + 1;
			}
		}

		MCRemoteFileDialog(ep, p_title, p_prompt, t_filters, t_filter_count, t_initial_folder, t_initial_file, (p_options & MCA_OPTION_SAVE_DIALOG) != 0, (p_options & MCA_OPTION_PLURAL) != 0);

		free(t_filters);
		return 0;
	}

	Window t_window;
	t_window = MCModeGetParentWindow();

	bool t_succeeded;
	int t_filter_index;

	if (MCmajorosversion >= 0x0600)
	{
		static SHCreateItemFromParsingNamePtr  s_shcreateitemfromparsingname = NULL;
		if (s_shcreateitemfromparsingname == NULL)
		{
			static HMODULE s_shell32_module = NULL;
			s_shell32_module = LoadLibraryA("shell32.dll");
			s_shcreateitemfromparsingname = (SHCreateItemFromParsingNamePtr)GetProcAddress(s_shell32_module, "SHCreateItemFromParsingName");
		}

		IFileSaveDialog *t_file_save_dialog;
		IFileOpenDialog *t_file_open_dialog;
		IFileDialog *t_file_dialog;

		t_file_dialog = NULL;

		HRESULT t_hresult;

		if ((p_options & MCA_OPTION_SAVE_DIALOG) == 0)
		{
			t_hresult = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, __uuidof(IFileOpenDialog), (LPVOID *)&t_file_open_dialog);
			t_succeeded = SUCCEEDED(t_hresult);

			t_file_dialog = t_file_open_dialog;
		}
		else
		{
			t_hresult = CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, __uuidof(IFileSaveDialog), (LPVOID *)&t_file_save_dialog);
			t_succeeded = SUCCEEDED(t_hresult);

			t_file_dialog = t_file_save_dialog;
		}

		if (t_succeeded)
		{
			DWORD t_options;

			t_options = FOS_FORCEFILESYSTEM | FOS_NOCHANGEDIR | FOS_PATHMUSTEXIST;
			if (p_options & MCA_OPTION_PLURAL)
				t_options |= FOS_ALLOWMULTISELECT;
			if (p_options & MCA_OPTION_SAVE_DIALOG)
				t_options |= FOS_OVERWRITEPROMPT;
			if (p_options & MCA_OPTION_FOLDER_DIALOG)
				t_options |= FOS_PICKFOLDERS;
			else
				t_options |= FOS_FILEMUSTEXIST;

			t_hresult = t_file_dialog -> SetOptions(t_options);
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded && t_initial_folder != NULL)
		{
			IShellItem *t_initial_folder_shellitem;
			t_initial_folder_shellitem = NULL;
			t_hresult = s_shcreateitemfromparsingname(WideCString(t_initial_folder), NULL, __uuidof(IShellItem), (LPVOID *)&t_initial_folder_shellitem);
			if (SUCCEEDED(t_hresult))
				t_file_dialog -> SetFolder(t_initial_folder_shellitem);
			if (t_initial_folder_shellitem != NULL)
				t_initial_folder_shellitem -> Release();
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded && t_initial_file != NULL)
		{
			t_hresult = t_file_dialog -> SetFileName(WideCString(t_initial_file));
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded && p_filter != NULL && (p_options & MCA_OPTION_FOLDER_DIALOG) == 0)
		{
			uint4 t_filter_length, t_filter_count;
			measure_filter(p_filter, t_filter_length, t_filter_count);

			WideCString t_filters(p_filter, t_filter_length);
			COMDLG_FILTERSPEC *t_filter_spec;

			filter_to_spec(t_filters, t_filter_count, t_filter_spec);

			t_hresult = t_file_dialog -> SetFileTypes(t_filter_count, t_filter_spec);
			t_succeeded = SUCCEEDED(t_hresult);

			delete t_filter_spec;
		}

		if (t_succeeded && p_filter != NULL && (p_options & MCA_OPTION_FOLDER_DIALOG) == 0)
		{
			t_hresult = t_file_dialog -> SetFileTypeIndex(1);
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded)
			t_hresult = t_file_dialog -> SetTitle(WideCString(p_prompt));

		if (t_succeeded)
		{
			t_hresult = t_file_dialog -> Show(t_window != NULL ? (HWND)t_window -> handle . window : NULL);
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if ((p_options & MCA_OPTION_SAVE_DIALOG) == 0)
		{
			IShellItemArray *t_file_items;
			t_file_items = NULL;
			if (t_succeeded)
			{
				t_hresult = t_file_open_dialog -> GetResults(&t_file_items);
				t_succeeded = SUCCEEDED(t_hresult);
			}

			DWORD t_file_item_count;
			if (t_succeeded)
			{
				t_hresult = t_file_items -> GetCount(&t_file_item_count);
				t_succeeded = SUCCEEDED(t_hresult);
			}

			if (t_succeeded)
			{
				ep . clear();
				for(uint4 t_index = 0; t_index < t_file_item_count && t_succeeded; ++t_index)
				{
					IShellItem *t_file_item;
					t_file_item = NULL;
					if (t_succeeded)
					{
						t_hresult = t_file_items -> GetItemAt(t_index, &t_file_item);
						t_succeeded = SUCCEEDED(t_hresult);
					}

					if (t_succeeded)
					{
						t_hresult = append_shellitem_path_and_release(ep, t_file_item, t_index == 0);
						t_succeeded = SUCCEEDED(t_hresult);
					}
				}
			}

			if (t_file_items != NULL)
				t_file_items -> Release();
		}
		else
		{
			IShellItem *t_file_item;
			t_file_item = NULL;
			if (t_succeeded)
			{
				t_hresult = t_file_dialog -> GetResult(&t_file_item);
				t_succeeded = SUCCEEDED(t_hresult);
			}

			if (t_succeeded)
			{
				ep . clear();
				t_hresult = append_shellitem_path_and_release(ep, t_file_item, true);
				t_succeeded = SUCCEEDED(t_hresult);
			}
		}

		t_filter_index = 0;
		if (t_succeeded && (p_options & MCA_OPTION_FOLDER_DIALOG) == 0)
		{
			UINT t_index;
			t_hresult = t_file_dialog -> GetFileTypeIndex(&t_index);
			t_succeeded = SUCCEEDED(t_hresult);
			if (t_succeeded)
				t_filter_index = (int)t_index;
		}

		if (t_file_dialog != NULL)
			t_file_dialog -> Release();

		if (!t_succeeded)
			t_result = t_hresult;
		else
			t_result = 0;
	}
	else
	{
		OPENFILENAMEA t_open_dialog;
		memset(&t_open_dialog, 0, sizeof(OPENFILENAMEA));
		t_open_dialog . lStructSize = sizeof(OPENFILENAMEA);

		char *t_initial_file_buffer = new char[MAX_PATH];
		if (t_initial_file != NULL)
			strcpy(t_initial_file_buffer, t_initial_file);
		else
			*t_initial_file_buffer = '\0';

		t_open_dialog . lpstrFilter = p_filter;
		t_open_dialog . nFilterIndex = 1;
		t_open_dialog . lpstrFile = t_initial_file_buffer;
		t_open_dialog . nMaxFile = MAX_PATH;
		t_open_dialog . lpstrInitialDir = t_initial_folder;
		t_open_dialog . lpstrTitle = p_prompt;
		t_open_dialog . Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR |
														OFN_LONGNAMES | OFN_PATHMUSTEXIST | OFN_EXPLORER |
														OFN_ENABLEHOOK | OFN_ENABLESIZING;

		if (p_options & MCA_OPTION_PLURAL)
			t_open_dialog . Flags |= OFN_ALLOWMULTISELECT;

		if (p_options & MCA_OPTION_SAVE_DIALOG)
			t_open_dialog . Flags |= OFN_OVERWRITEPROMPT;

		t_open_dialog . lpstrFilter = p_filter;
		t_open_dialog . lpfnHook = open_dialog_hook;
		t_open_dialog . hwndOwner = t_window != NULL ? (HWND)t_window -> handle . window : NULL;

		if (p_options & MCA_OPTION_SAVE_DIALOG)
			t_succeeded = GetSaveFileNameA((LPOPENFILENAMEA)&t_open_dialog) == TRUE;
		else
		{
			*t_open_dialog . lpstrFile = '\0';
			t_succeeded = GetOpenFileNameA((LPOPENFILENAMEA)&t_open_dialog) == TRUE;
		}

		if (!t_succeeded)
			t_result = CommDlgExtendedError();

		// MW-2005-07-26: Try again without the specified filename if it was invalid
		if (t_result == FNERR_INVALIDFILENAME)
		{
			*t_open_dialog . lpstrFile = '\0';
			if (p_options & MCA_OPTION_SAVE_DIALOG)
				t_succeeded = GetSaveFileNameA((LPOPENFILENAMEA)&t_open_dialog) == TRUE;
			else
				t_succeeded = GetOpenFileNameA((LPOPENFILENAMEA)&t_open_dialog) == TRUE;

			if (!t_succeeded)
				t_result = CommDlgExtendedError();	
		}

		if (t_result == FNERR_BUFFERTOOSMALL)
			t_succeeded = true;

		if (t_succeeded)
		{
			build_paths(ep);
			t_filter_index = t_open_dialog . nFilterIndex;
		}

		delete t_initial_file_buffer;
	}

	if (t_succeeded)
	{
		if (p_options & MCA_OPTION_RETURN_FILTER)
		{
			const char *t_type = p_filter;
			const char *t_types = p_filter;
			for(int t_index = t_filter_index * 2 - 1; t_index > 1; t_types += 1)
				if (*t_types == '\0')
					t_type = t_types + 1, t_index -= 1;
			MCresult -> copysvalue(t_type);
		}

		t_result = 0;
	}

	waitonbutton();

	if (t_initial_folder != NULL)
		delete t_initial_folder;

	if (t_initial_file != NULL)
		delete t_initial_file;

	return t_result;
}
示例#8
0
bool MCSessionCleanup(void)
{
	bool t_success = true;
	
	MCSessionIndexRef t_index = NULL;
	t_success = MCSessionOpenIndex(t_index);
	
	real8 t_time;
	t_time = MCS_time();
	
	for (uint32_t i = 0; t_success && i < t_index->session_count; i++)
	{
		if (t_index->session[i]->expires <= t_time)
		{
			bool t_deleted = false;
			// check file not locked
			MCSystemFileHandle *t_file;
			MCAutoStringRef t_full_path_string;
            if (MCStringFormat(&t_full_path_string, "%@/%s", t_index->save_path, t_index->session[i]->filename)  && MCS_exists(*t_full_path_string, True))
			{
				t_file = MCsystem->OpenFile(*t_full_path_string, kMCOpenFileModeRead, false);
				if (t_file != NULL)
				{
					bool t_locked = false;
					t_locked = MCSystemLockFile(t_file, false, false);
					t_file->Close();
					
					if (t_locked)
						t_deleted = MCsystem->DeleteFile(*t_full_path_string);
				}
			}
			else
				t_deleted = true;

			if (t_deleted)
				MCSessionIndexRemoveSession(t_index, t_index->session[i]);
		}
	}
	
	if (t_index != NULL)
		t_success &= MCSessionCloseIndex(t_index, t_success);
	
	return t_success;
}
示例#9
0
IO_stat MCDispatch::dosavestack(MCStack *sptr, const MCString &fname)
{
	if (MCModeCheckSaveStack(sptr, fname) != IO_NORMAL)
		return IO_ERROR;
	
	char *linkname;
	if (fname.getlength() != 0)
		linkname = fname.clone();
	else
		if ((linkname = strclone(sptr->getfilename())) == NULL)
		{
			MCresult->sets("stack does not have a filename");
			return IO_ERROR;
		}
	if (linkname == NULL)
	{
		MCresult->sets("can't open stack file, bad path");
		return IO_ERROR;
	}
	if (MCS_noperm(linkname))
	{
		MCresult->sets("can't open stack file, no permission");
		delete linkname;
		return IO_ERROR;
	}
	char *oldfiletype = MCfiletype;
	MCfiletype = MCstackfiletype;
	char *backup = new char[strlen(linkname) + 2];
	strcpy(backup, linkname);
	strcat(backup, "~");
	MCS_unlink(backup);
	if (MCS_exists(linkname, True) && !MCS_backup(linkname, backup))
	{
		MCresult->sets("can't open stack backup file");
		MCfiletype = oldfiletype;
		delete linkname;
		delete backup;
		return IO_ERROR;
	}
	IO_handle stream;
	if ((stream = MCS_open(linkname, IO_WRITE_MODE, True, False, 0)) == NULL)
	{
		MCresult->sets("can't open stack file");
		cleanup(stream, linkname, backup);
		MCfiletype = oldfiletype;
		return IO_ERROR;
	}
	MCfiletype = oldfiletype;
	MCString errstring = "Error writing stack (disk full?)";
	
	// MW-2012-03-04: [[ StackFile5500 ]] Work out what header to emit, and the size.
	const char *t_header;
	uint32_t t_header_size;
	if (MCstackfileversion >= 5500)
		t_header = newheader5500, t_header_size = 8;
	else if (MCstackfileversion >= 2700)
		t_header = newheader, t_header_size = 8;
	else
		t_header = header, t_header_size = HEADERSIZE;
	
	if (IO_write(t_header, sizeof(char), t_header_size, stream) != IO_NORMAL
	        || IO_write_uint1(CHARSET, stream) != IO_NORMAL)
	{
		MCresult->sets(errstring);
		cleanup(stream, linkname, backup);
		return IO_ERROR;
	}

	if (IO_write_uint1(OT_NOTHOME, stream) != IO_NORMAL
	        || IO_write_string(NULL, stream) != IO_NORMAL)
	{ // was stackfiles
		MCresult->sets(errstring);
		cleanup(stream, linkname, backup);
		return IO_ERROR;
	}
	
	// MW-2012-02-22; [[ NoScrollSave ]] Adjust the rect by the current group offset.
	MCgroupedobjectoffset . x = 0;
	MCgroupedobjectoffset . y = 0;
	
	MCresult -> clear();
	if (sptr->save(stream, 0, false) != IO_NORMAL
	        || IO_write_uint1(OT_END, stream) != IO_NORMAL)
	{
		if (MCresult -> isclear())
			MCresult->sets(errstring);
		cleanup(stream, linkname, backup);
		return IO_ERROR;
	}
	MCS_close(stream);
	uint2 oldmask = MCS_umask(0);
	uint2 newmask = ~oldmask & 00777;
	if (oldmask & 00400)
		newmask &= ~00100;
	if (oldmask & 00040)
		newmask &= ~00010;
	if (oldmask & 00004)
		newmask &= ~00001;
	MCS_umask(oldmask);
	MCS_chmod(linkname, newmask);
	if (sptr->getfilename() != NULL && !strequal(linkname, sptr->getfilename()))
		MCS_copyresourcefork(sptr->getfilename(), linkname);
	else if (sptr -> getfilename() != NULL)
		MCS_copyresourcefork(backup, linkname);
	sptr->setfilename(linkname);
	if (backup != NULL)
	{
		MCS_unlink(backup);
		delete backup;
	}
	return IO_NORMAL;
}
示例#10
0
static int MCA_do_file_dialog(MCStringRef p_title, MCStringRef p_prompt, MCStringRef p_filter, MCStringRef p_initial, unsigned int p_options, MCStringRef &r_value, MCStringRef &r_result)
{
	int t_result = 0;

	MCAutoStringRef t_initial_file;
	MCAutoStringRef t_initial_folder;
    MCAutoStringRef t_initial_native_folder;

	if (p_initial != nil && !MCStringIsEmpty(p_initial))
	{
		MCAutoStringRef t_fixed_path;
		/* UNCHECKED */ MCU_fix_path(p_initial, &t_fixed_path);

		if (MCS_exists(*t_fixed_path, False))
			t_initial_folder = *t_fixed_path;
		else if ((p_options & MCA_OPTION_SAVE_DIALOG) != 0)
		{
			uindex_t t_last_slash;
			if (!MCStringLastIndexOfChar(*t_fixed_path, '/', UINDEX_MAX, kMCStringOptionCompareExact, t_last_slash))
			{
				if (MCStringGetLength(*t_fixed_path) != 0)
					t_initial_file = *t_fixed_path;
			}
			else
			{
				if (t_last_slash < MCStringGetLength(*t_fixed_path) - 1)
					/* UNCHECKED */ MCStringCopySubstring(*t_fixed_path, MCRangeMake(t_last_slash + 1, MCStringGetLength(*t_fixed_path) - (t_last_slash + 1)), &t_initial_file);

				MCAutoStringRef t_folder_split;
				/* UNCHECKED */ MCStringCopySubstring(*t_fixed_path, MCRangeMake(0, t_last_slash - 1), &t_folder_split);
				if (MCS_exists(*t_folder_split, False))
					t_initial_folder = *t_folder_split;
			}
		}
		else
		{
			uindex_t t_last_slash;

			if (MCStringLastIndexOfChar(*t_fixed_path, '/', UINDEX_MAX, kMCStringOptionCompareExact, t_last_slash))
			{
				MCAutoStringRef t_folder_split;
				/* UNCHECKED */ MCStringCopySubstring(*t_fixed_path, MCRangeMake(0, t_last_slash - 1), &t_folder_split);
				
				if (MCS_exists(*t_folder_split, False))
					t_initial_folder = *t_folder_split;
			}
		}
        
        MCAutoStringRef t_resolved_folder;
        /* UNCHECKED */ MCS_resolvepath(*t_initial_folder != nil ? *t_initial_folder : kMCEmptyString, &t_resolved_folder);
        /* UNCHECKED */ MCS_pathtonative(*t_resolved_folder, &t_initial_native_folder);
	}
    
	if (!MCModeMakeLocalWindows())
	{
		MCAutoStringRefArray t_filters;

		if (p_filter != NULL)
		{
			/* UNCHECKED */ MCStringsSplit(p_filter, '\0', t_filters.PtrRef(), t_filters.CountRef());
		}

		MCRemoteFileDialog(p_title, p_prompt, *t_filters, t_filters.Count(), *t_initial_native_folder, *t_initial_file, (p_options & MCA_OPTION_SAVE_DIALOG) != 0, (p_options & MCA_OPTION_PLURAL) != 0, r_value);

		return 0;
	}

	Window t_window;
	t_window = MCModeGetParentWindow();

	MCAutoStringRef t_value;
	bool t_succeeded;
	int t_filter_index;

	if (MCmajorosversion >= 0x0600)
	{
		static SHCreateItemFromParsingNamePtr  s_shcreateitemfromparsingname = NULL;
		if (s_shcreateitemfromparsingname == NULL)
		{
			static HMODULE s_shell32_module = NULL;
			s_shell32_module = LoadLibraryW(L"shell32.dll");
			s_shcreateitemfromparsingname = (SHCreateItemFromParsingNamePtr)GetProcAddress(s_shell32_module, "SHCreateItemFromParsingName");
		}

		IFileSaveDialog *t_file_save_dialog;
		IFileOpenDialog *t_file_open_dialog;
		IFileDialog *t_file_dialog;

		t_file_dialog = NULL;

		HRESULT t_hresult;

		if ((p_options & MCA_OPTION_SAVE_DIALOG) == 0)
		{
			t_hresult = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, __uuidof(IFileOpenDialog), (LPVOID *)&t_file_open_dialog);
			t_succeeded = SUCCEEDED(t_hresult);

			t_file_dialog = t_file_open_dialog;
		}
		else
		{
			t_hresult = CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, __uuidof(IFileSaveDialog), (LPVOID *)&t_file_save_dialog);
			t_succeeded = SUCCEEDED(t_hresult);

			t_file_dialog = t_file_save_dialog;
		}

		if (t_succeeded)
		{
			DWORD t_options;

			t_options = FOS_FORCEFILESYSTEM | FOS_NOCHANGEDIR | FOS_PATHMUSTEXIST;
			if (p_options & MCA_OPTION_PLURAL)
				t_options |= FOS_ALLOWMULTISELECT;
			if (p_options & MCA_OPTION_SAVE_DIALOG)
				t_options |= FOS_OVERWRITEPROMPT;
			if (p_options & MCA_OPTION_FOLDER_DIALOG)
				t_options |= FOS_PICKFOLDERS;
			else
				t_options |= FOS_FILEMUSTEXIST;

			t_hresult = t_file_dialog -> SetOptions(t_options);
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded && *t_initial_native_folder != NULL)
		{
			IShellItem *t_initial_folder_shellitem;
			t_initial_folder_shellitem = NULL;

			MCAutoStringRefAsWString t_initial_folder_wstr;
			/* UNCHECKED */ t_initial_folder_wstr.Lock(*t_initial_native_folder);

			t_hresult = s_shcreateitemfromparsingname(*t_initial_folder_wstr, NULL, __uuidof(IShellItem), (LPVOID *)&t_initial_folder_shellitem);
			if (SUCCEEDED(t_hresult))
				t_file_dialog -> SetFolder(t_initial_folder_shellitem);
			if (t_initial_folder_shellitem != NULL)
				t_initial_folder_shellitem -> Release();
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded && *t_initial_file != NULL)
		{
			MCAutoStringRefAsWString t_initial_file_wstr;
			/* UNCHECKED */ t_initial_file_wstr.Lock(*t_initial_file);
			
			t_hresult = t_file_dialog -> SetFileName(*t_initial_file_wstr);
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded && p_filter != NULL && (p_options & MCA_OPTION_FOLDER_DIALOG) == 0)
		{
			uint4 t_filter_length, t_filter_count;
			measure_filter(p_filter, t_filter_length, t_filter_count);

			MCAutoStringRefAsWString t_filter_wstr;
			/* UNCHECKED */ t_filter_wstr.Lock(p_filter);

			COMDLG_FILTERSPEC *t_filter_spec;

			filter_to_spec(*t_filter_wstr, t_filter_count, t_filter_spec);

			t_hresult = t_file_dialog -> SetFileTypes(t_filter_count, t_filter_spec);
			t_succeeded = SUCCEEDED(t_hresult);

			delete t_filter_spec;
		}

		if (t_succeeded && p_filter != NULL && (p_options & MCA_OPTION_FOLDER_DIALOG) == 0)
		{
			t_hresult = t_file_dialog -> SetFileTypeIndex(1);
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if (t_succeeded)
		{
			MCAutoStringRefAsWString t_prompt_wstr;
			/* UNCHECKED */ t_prompt_wstr.Lock(p_prompt);
			t_hresult = t_file_dialog -> SetTitle(*t_prompt_wstr);
		}

		if (t_succeeded)
		{
			t_hresult = t_file_dialog -> Show(t_window != NULL ? (HWND)t_window -> handle . window : NULL);
			t_succeeded = SUCCEEDED(t_hresult);
		}

		if ((p_options & MCA_OPTION_SAVE_DIALOG) == 0)
		{
			IShellItemArray *t_file_items;
			t_file_items = NULL;
			if (t_succeeded)
			{
				t_hresult = t_file_open_dialog -> GetResults(&t_file_items);
				t_succeeded = SUCCEEDED(t_hresult);
			}

			DWORD t_file_item_count;
			if (t_succeeded)
			{
				t_hresult = t_file_items -> GetCount(&t_file_item_count);
				t_succeeded = SUCCEEDED(t_hresult);
			}

			if (t_succeeded)
			{
				for(uint4 t_index = 0; t_index < t_file_item_count && t_succeeded; ++t_index)
				{
					IShellItem *t_file_item;
					t_file_item = NULL;
					if (t_succeeded)
					{
						t_hresult = t_file_items -> GetItemAt(t_index, &t_file_item);
						t_succeeded = SUCCEEDED(t_hresult);
					}

					if (t_succeeded)
					{
						t_hresult = append_shellitem_path_and_release(t_file_item, t_index == 0, &t_value);
						t_succeeded = SUCCEEDED(t_hresult);
					}
				}
			}

			if (t_file_items != NULL)
				t_file_items -> Release();
		}
		else
		{
			IShellItem *t_file_item;
			t_file_item = NULL;
			if (t_succeeded)
			{
				t_hresult = t_file_dialog -> GetResult(&t_file_item);
				t_succeeded = SUCCEEDED(t_hresult);
			}

			if (t_succeeded)
			{
				t_hresult = append_shellitem_path_and_release(t_file_item, true, &t_value);
				t_succeeded = SUCCEEDED(t_hresult);
			}
		}

		t_filter_index = 0;
		if (t_succeeded && (p_options & MCA_OPTION_FOLDER_DIALOG) == 0)
		{
			UINT t_index;
			t_hresult = t_file_dialog -> GetFileTypeIndex(&t_index);
			t_succeeded = SUCCEEDED(t_hresult);
			if (t_succeeded)
				t_filter_index = (int)t_index;
		}

		if (t_file_dialog != NULL)
			t_file_dialog -> Release();

		if (!t_succeeded)
			t_result = t_hresult;
		else
			t_result = 0;
	}
	else
	{
		OPENFILENAMEW t_open_dialog;
		memset(&t_open_dialog, 0, sizeof(OPENFILENAMEW));
		t_open_dialog . lStructSize = sizeof(OPENFILENAMEW);

		MCAutoStringRefAsWString t_initial_folder_wstr;
		MCAutoStringRefAsWString t_prompt_wstr;
		MCAutoStringRefAsWString t_filter_wstr;
		/* UNCHECKED */ t_filter_wstr.Lock(p_filter);
		/* UNCHECKED */ t_initial_folder_wstr.Lock(*t_initial_native_folder);
		/* UNCHECKED */ t_prompt_wstr.Lock(p_prompt);

		MCAutoArray<unichar_t> t_buffer;
		/* UNCHECKED */ t_buffer.New(MAX_PATH);

		if (!MCStringIsEmpty(*t_initial_file))
			/* UNCHECKED */ MCStringGetChars(*t_initial_file, MCRangeMake(0, t_buffer.Size()), t_buffer.Ptr());
		else
			t_buffer[0] = '\0';

		t_open_dialog . lpstrFilter = *t_filter_wstr;
		t_open_dialog . nFilterIndex = 1;
		t_open_dialog . lpstrFile = t_buffer.Ptr();
		t_open_dialog . nMaxFile = t_buffer.Size();
		t_open_dialog . lpstrInitialDir = *t_initial_folder_wstr;
		t_open_dialog . lpstrTitle = *t_prompt_wstr;
		t_open_dialog . Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR |
														OFN_LONGNAMES | OFN_PATHMUSTEXIST | OFN_EXPLORER |
														OFN_ENABLEHOOK | OFN_ENABLESIZING;

		if (p_options & MCA_OPTION_PLURAL)
			t_open_dialog . Flags |= OFN_ALLOWMULTISELECT;

		if (p_options & MCA_OPTION_SAVE_DIALOG)
			t_open_dialog . Flags |= OFN_OVERWRITEPROMPT;

		t_open_dialog . lpstrFilter = *t_filter_wstr;
		t_open_dialog . lpfnHook = open_dialog_hook;
		t_open_dialog . hwndOwner = t_window != NULL ? (HWND)t_window -> handle . window : NULL;

		if (p_options & MCA_OPTION_SAVE_DIALOG)
			t_succeeded = GetSaveFileNameW(&t_open_dialog) == TRUE;
		else
		{
			*t_open_dialog . lpstrFile = '\0';
			t_succeeded = GetOpenFileNameW(&t_open_dialog) == TRUE;
		}

		if (!t_succeeded)
			t_result = CommDlgExtendedError();

		// MW-2005-07-26: Try again without the specified filename if it was invalid
		if (t_result == FNERR_INVALIDFILENAME)
		{
			*t_open_dialog . lpstrFile = '\0';
			if (p_options & MCA_OPTION_SAVE_DIALOG)
				t_succeeded = GetSaveFileNameW(&t_open_dialog) == TRUE;
			else
				t_succeeded = GetOpenFileNameW(&t_open_dialog) == TRUE;

			if (!t_succeeded)
				t_result = CommDlgExtendedError();	
		}

		if (t_result == FNERR_BUFFERTOOSMALL)
			t_succeeded = true;

		if (t_succeeded)
		{
			build_paths(&t_value);
			t_filter_index = t_open_dialog . nFilterIndex;
		}
	}

	if (t_succeeded)
	{
		if (p_options & MCA_OPTION_RETURN_FILTER)
		{
			// The filter string has the following format:
			// "<description0>\0<extensions0>\0<description1>\0...\0<extensionsN>\0"
			// so the n'th filter comes after the 2(n - 1)'th null character
			uindex_t t_index = 2 * (t_filter_index - 1);
			uindex_t t_offset = 0;
			while (t_index--)
			{
				/* UNCHECKED */ MCStringFirstIndexOfChar(p_filter, '\0', t_offset, kMCStringOptionCompareExact, t_offset);
				t_offset++;
			}
			
			uindex_t t_end;
			t_end = UINDEX_MAX;
			/* UNCHECKED */ MCStringFirstIndexOfChar(p_filter, '\0', t_offset, kMCStringOptionCompareExact, t_end);
            
			/* UNCHECKED */ MCStringCopySubstring(p_filter, MCRangeMake(t_offset, t_end-t_offset), r_result);
		}

		t_result = 0;
		r_value = MCValueRetain(*t_value);
	}
	else
		r_result = MCValueRetain(MCNameGetString(MCN_cancel));

	waitonbutton();

	return t_result;
}
示例#11
0
文件: srvcgi.cpp 项目: bduck/livecode
static bool cgi_multipart_header_callback(void *p_context, MCMultiPartHeader *p_header)
{
	bool t_success = true;
	cgi_multipart_context_t *t_context = (cgi_multipart_context_t*)p_context;
	
	if (p_header != NULL)
	{
		if (MCCStringEqualCaseless(p_header->name, "Content-Disposition"))
		{
			if (MCCStringEqualCaseless(p_header->value, "form-data"))
				t_context->disposition = kMCDispositionFormData;
			else if (MCCStringEqualCaseless(p_header->value, "file"))
				t_context->disposition = kMCDispositionFile;
			else
				t_context->disposition = kMCDispositionUnknown;
			
			for (uint32_t i = 0; i < p_header->param_count; i++)
			{
				if (MCCStringEqualCaseless(p_header->param_name[i], "name"))
					grab_string(t_context->name, p_header->param_value[i]);
				else if (MCCStringEqualCaseless(p_header->param_name[i], "filename"))
					grab_string(t_context->file_name, p_header->param_value[i]);
			}
		}
		else if (MCCStringEqualCaseless(p_header->name, "Content-Type"))
		{
			grab_string(t_context->type, p_header->value);
			
			for (uint32_t i = 0; i < p_header->param_count; i++)
			{
				if (MCCStringEqualCaseless(p_header->param_name[i], "boundary"))
					grab_string(t_context->boundary, p_header->param_value[i]);
			}
		}
	}
	else
	{
		if (cgi_context_is_form_data(t_context))
		{
			MCExecPoint ep;
			t_success = t_context->name != NULL;
			if (t_success)
			{
				cgi_fetch_variable_value_for_key(s_cgi_post, t_context->name, MCCStringLength(t_context->name), ep, t_context->post_variable);
				t_context->post_variable->assign_empty();
				cgi_fetch_variable_value_for_key(s_cgi_post_binary, t_context->name, MCCStringLength(t_context->name), ep, t_context->post_binary_variable);
				t_context->post_binary_variable->assign_empty();
			}
		}
		else if (cgi_context_is_file(t_context))
		{
			const char *t_temp_dir = cgi_get_upload_temp_dir();
			const char *t_error = NULL;
			if (t_temp_dir == NULL || !MCS_exists(t_temp_dir, False))
			{
				t_context->file_status = kMCFileStatusNoUploadFolder;
			}
			else if (!MCMultiPartCreateTempFile(cgi_get_upload_temp_dir(), t_context->file_handle, t_context->temp_name))
			{
				t_context->file_status = kMCFileStatusIOError;
			}
		}
	}
	
	return t_success;
}