Exemplo n.º 1
0
bool MCValueCopyDescription(MCValueRef p_value, MCStringRef& r_desc)
{
	__MCValue *self = (__MCValue *)p_value;

	switch(__MCValueGetTypeCode(self))
	{
	case kMCValueTypeCodeNull:
		return MCStringFormat(r_desc, "<null>");
	case kMCValueTypeCodeBoolean:
		return MCStringFormat(r_desc, "<%s>", p_value == kMCTrue ? "true" : "false");
	case kMCValueTypeCodeNumber:
		return __MCNumberCopyDescription((__MCNumber *)p_value, r_desc);
	case kMCValueTypeCodeString:
		return __MCStringCopyDescription((__MCString *)p_value, r_desc);
	case kMCValueTypeCodeName:
		return __MCNameCopyDescription((__MCName *)p_value, r_desc);
	case kMCValueTypeCodeArray:
		return __MCArrayCopyDescription((__MCArray *)p_value, r_desc);
	case kMCValueTypeCodeList:
		return __MCListCopyDescription((__MCList *)p_value, r_desc);
	case kMCValueTypeCodeSet:
		return __MCSetCopyDescription((__MCSet *)p_value, r_desc);
    case kMCValueTypeCodeData:
        return __MCDataCopyDescription((__MCData*)p_value, r_desc);
	case kMCValueTypeCodeCustom:
		return ((__MCCustomValue *)self) -> callbacks -> describe(p_value, r_desc);
	default:
		break;
	}
	return false;
}
Exemplo n.º 2
0
static void MCInterfaceMarginsFormat(MCExecContext& ctxt, const MCInterfaceMargins& p_input, MCStringRef& r_output)
{
    bool t_success;
    
    if (p_input . type == kMCInterfaceMarginsTypeSingle)
        t_success = MCStringFormat(r_output, "%d", p_input . margin);
    else
        t_success = MCStringFormat(r_output, "%d,%d,%d,%d", p_input . margins[0], p_input . margins[1], p_input . margins[2], p_input . margins[3]);

    if (t_success)
        return;
    
    ctxt . Throw();
}
Exemplo n.º 3
0
bool MCArraysCopyExtents(MCArrayRef self, MCListRef& r_list)
{
	MCAutoArray<array_extent_t> t_extents;
	if (!MCArraysCopyExtents(self, t_extents.PtrRef(), t_extents.SizeRef()))
		return false;

	uindex_t t_dimensions = t_extents.Size();

	if (t_dimensions == 0)
	{
		r_list = MCValueRetain(kMCEmptyList);
		return true;
	}

	MCAutoListRef t_list;
	if (!MCListCreateMutable('\n', &t_list))
		return false;

	for (uindex_t i = 0; i < t_dimensions; i++)
	{
		MCAutoStringRef t_string;
		if (!MCStringFormat(&t_string, "%d,%d", t_extents[i].min, t_extents[i].max))
			return false;
		if (!MCListAppend(*t_list, *t_string))
			return false;
	}

	return MCListCopy(*t_list, r_list);
}
Exemplo n.º 4
0
static void MCInterfaceButtonIconFormat(MCExecContext& ctxt, const MCInterfaceButtonIcon& p_input, MCStringRef& r_output)
{
    if (MCStringFormat(r_output, "%d", p_input . id))
        return;
    
    ctxt . Throw();
}
Exemplo n.º 5
0
size_t
regerror(int errcode, const regex_t *preg, MCStringRef &errbuf)
{
	const char *message, *addmessage;
	size_t length, addlength;

	message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))
	          ? "unknown error code" : pstring[errcode];
	length = strlen(message) + 1;

	addmessage = " at offset ";
	addlength = (preg != NULL && (int)preg->re_erroffset != -1)
	            ? strlen(addmessage) + 6 : 0;
	
	
    if (addlength > 0)
    {
        MCAutoStringRef t_error_string;
        MCStringFormat(&t_error_string, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset);
        MCValueAssign(errbuf, *t_error_string);

    }
    else
    {
		if (errbuf != nil)
			MCValueRelease(errbuf);
        /* UNCHECKED */ MCStringCreateWithNativeChars((const char_t *) message, strlen(message), errbuf);
    }
	
	return length + addlength;
}
Exemplo n.º 6
0
bool MCAndroidSystem::GetAddress(MCStringRef& r_address)
{
	extern MCStringRef MCcmd;
    MCAutoStringRef t_address;
    bool t_success;
    t_success = MCStringFormat(&t_address, "android:%@", MCcmd);
    if (t_success)
        r_address = MCValueRetain(*t_address);
    
	return t_success;
}
Exemplo n.º 7
0
bool MCArraysCopyTransposed(MCArrayRef self, MCArrayRef& r_transposed)
{
	MCAutoArray<array_extent_t> t_extents;
	if (!MCArraysCopyExtents(self, t_extents.PtrRef(), t_extents.SizeRef()) ||
		t_extents.Size() != 2)
		return false;

	integer_t t_rows = extent_size(t_extents[0]);
	integer_t t_cols = extent_size(t_extents[1]);

	integer_t t_row_end = t_extents[0].min + t_rows;
	integer_t t_col_end = t_extents[1].min + t_cols;

	if (MCArrayGetCount(self) != t_rows * t_cols)
		return false;

	MCAutoArrayRef t_transposed;
	if (!MCArrayCreateMutable(&t_transposed))
		return false;

	for (integer_t r = t_extents[0].min; r < t_row_end; r++)
	{
		for (integer_t c = t_extents[1].min; c < t_col_end; c++)
		{
			MCAutoStringRef t_src_string, t_dst_string;
			MCNewAutoNameRef t_src_name, t_dst_name;
			MCValueRef t_value;
			if (!MCStringFormat(&t_src_string, "%d,%d", r, c) ||
				!MCStringFormat(&t_dst_string, "%d,%d", c, r))
				return false;
			if (!MCNameCreate(*t_src_string, &t_src_name) ||
				!MCNameCreate(*t_dst_string, &t_dst_name))
				return false;
			if (!MCArrayFetchValue(self, true, *t_src_name, t_value) ||
				!MCArrayStoreValue(*t_transposed, true, *t_dst_name, t_value))
				return false;
		}
	}

	return MCArrayCopy(*t_transposed, r_transposed);
}
Exemplo n.º 8
0
bool MCSessionOpenSession(MCSessionIndexRef p_index, MCSession *p_session)
{
	bool t_success = true;
	
	MCAutoStringRef t_path_string;
    t_success = MCStringFormat(&t_path_string, "%@/%s", p_index->save_path, p_session->filename);
	
	if (t_success)
		t_success = NULL != (p_session->filehandle = MCsystem->OpenFile(*t_path_string, kMCOpenFileModeUpdate, false));
	
	if (t_success)
		t_success = MCSystemLockFile(p_session->filehandle, false, true);
	
	if (t_success && p_session->filehandle->GetFileSize() > 0 && p_session->expires > MCS_time())
		t_success = MCSessionReadSession(p_session);
	
	return t_success;
}
Exemplo n.º 9
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;
}
Exemplo n.º 10
0
void MCButton::GetAcceleratorKey(MCExecContext& ctxt, MCStringRef& r_key)
{
	if (accelkey & 0xFF00)
	{
		const char *t_keyname = MCLookupAcceleratorName(accelkey);
		if (t_keyname == NULL || MCStringCreateWithCString(t_keyname, r_key))
			return;
	}
    else if (accelkey)
    {
        char t_accel_key = (char)accelkey;
        if (MCStringFormat(r_key, "%c", t_accel_key))
            return;
    }
    else
        return;

	ctxt . Throw();
}
Exemplo n.º 11
0
void MCPlatformHandleViewFocusSwitched(MCPlatformWindowRef p_window, uint32_t p_view_id)
{
	MCStack *t_stack;
	t_stack = MCdispatcher -> findstackd(p_window);
	if (t_stack == nil)
		return;
	
	if (p_view_id == 0)
		t_stack -> getcard() -> kfocus();
	else
	{
		MCControl *t_control;
        MCAutoStringRef t_id;
        /* UNCHECKED */ MCStringFormat(&t_id, "%d", p_view_id);
        
		t_control = t_stack -> getcard() -> getchild(CT_ID, *t_id, CT_LAYER, CT_UNDEFINED);
		if (t_control != nil)
			t_stack -> kfocusset(t_control);
		else
			t_stack -> getcard() -> kunfocus();
	}
}
Exemplo n.º 12
0
bool MCArraysCopyMatrix(MCExecContext& ctxt, MCArrayRef self, matrix_t*& r_matrix)
{
	MCAutoArray<array_extent_t> t_extents;
	if (!MCArraysCopyExtents(self, t_extents.PtrRef(), t_extents.SizeRef()) ||
		t_extents.Size() != 2)
		return false;

	integer_t t_rows = extent_size(t_extents[0]);
	integer_t t_cols = extent_size(t_extents[1]);

	integer_t t_row_offset = t_extents[0].min;
	integer_t t_col_offset = t_extents[1].min;

	if (MCArrayGetCount(self) != t_rows * t_cols)
		return false;

	MCAutoPointer<matrix_t> t_matrix;
	if (!MCMatrixNew(t_rows, t_cols, t_row_offset, t_col_offset, &t_matrix))
		return false;

	for (integer_t row = 0; row < t_rows; row++)
	{
		for (integer_t col = 0; col < t_cols; col++)
		{
			MCAutoStringRef t_string;
			MCNewAutoNameRef t_name;
			MCValueRef t_value;
			if (!MCStringFormat(&t_string, "%d,%d", row + t_row_offset, col + t_col_offset) ||
				!MCNameCreate(*t_string, &t_name) ||
				!MCArrayFetchValue(self, true, *t_name, t_value) ||
				!ctxt.ConvertToReal(t_value, MCMatrixEntry(*t_matrix, row, col)))
				return false;
		}
	}

	t_matrix.Take(r_matrix);
	return true;
}
Exemplo n.º 13
0
bool MCSessionOpenIndex(MCSessionIndexRef &r_index)
{
	bool t_success = true;
	
	MCSessionIndexRef t_index = nil;

	t_success = MCMemoryNew(t_index);
	
	MCAutoStringRef t_save_path;
	if (t_success)
		t_success = MCS_get_session_save_path(&t_save_path);
	
	if (t_success)
        t_success = MCStringCopy(*t_save_path, t_index->save_path);
	
	MCAutoStringRef t_path_string;
	if (t_success)
        t_success = MCStringFormat(&t_path_string, "%@/lcsessions.idx", t_index->save_path);
	
	// open file
	if (t_success)
		t_success = NULL != (t_index->file = MCsystem->OpenFile(*t_path_string, kMCOpenFileModeUpdate, false));

	// lock file
	if (t_success)
		t_success = MCSystemLockFile(t_index->file, false, true);
	
	// read index
	if (t_success && t_index->file->GetFileSize() > 0)
		t_success = MCSessionReadIndex(t_index);
	
	if (t_success)
		r_index = t_index;
	else
		MCSessionCloseIndex(t_index, false);
	
	return t_success;
}
Exemplo n.º 14
0
bool MCArraysCreateWithMatrix(matrix_t *p_matrix, MCArrayRef& r_array)
{
	MCAutoArrayRef t_array;
	if (!MCArrayCreateMutable(&t_array))
		return false;

	for (integer_t r = 0; r < p_matrix->rows; r++)
	{
		for (integer_t c = 0; c < p_matrix->columns; c++)
		{
			MCAutoStringRef t_string;
			MCNewAutoNameRef t_key;
			MCAutoNumberRef t_value;
			if (!MCStringFormat(&t_string, "%d,%d", r + p_matrix->row_offset, c + p_matrix->column_offset) ||
				!MCNameCreate(*t_string, &t_key) ||
				!MCNumberCreateWithReal(MCMatrixEntry(p_matrix, r, c), &t_value) ||
				!MCArrayStoreValue(*t_array, true, *t_key, *t_value))
				return false;
		}
	}

	return MCArrayCopy(*t_array, r_array);
}
Exemplo n.º 15
0
void MCDebuggingGetExecutionContexts(MCExecContext& ctxt, MCStringRef& r_value)
{
	int i;
	bool added = false;
	if (MCnexecutioncontexts < MAX_CONTEXTS)
	{
		MCexecutioncontexts[MCnexecutioncontexts++] = &ctxt;
		added = true;
	}

	bool t_success;
	t_success = true;

	MCAutoListRef t_context_list;
	if (t_success)
		t_success = MCListCreateMutable('\n', &t_context_list);

	if (t_success)
	{
		for (i = 0 ; i < MCnexecutioncontexts ; i++)
		{
			MCAutoListRef t_context;
			t_success = MCListCreateMutable(',', &t_context);
			
			if (t_success)
			{
				MCAutoValueRef t_context_id;
				t_success = MCexecutioncontexts[i]->GetObject()->names(P_LONG_ID, &t_context_id) &&
							MCListAppend(*t_context, *t_context_id);
			}
			
            // PM-2014-04-14: [[Bug 12125]] Do this check to avoid a crash in LC server
            if (t_success && MCexecutioncontexts[i]->GetHandler() != NULL)
				t_success = MCListAppend(*t_context, MCexecutioncontexts[i]->GetHandler()->getname());
			
			if (t_success)
			{
				MCAutoStringRef t_line;
                t_success = MCStringFormat(&t_line, "%d", MCexecutioncontexts[i] -> GetLine()) &&
							MCListAppend(*t_context, *t_line);
			}
			
			if (t_success && MCexecutioncontexts[i] -> GetParentScript() != NULL)
			{
				MCAutoValueRef t_parent;
				t_success = MCexecutioncontexts[i] -> GetParentScript() -> GetParent() -> GetObject() -> names(P_LONG_ID, &t_parent) &&
							MCListAppend(*t_context, *t_parent);
			}

			if (t_success)
				t_success = MCListAppend(*t_context_list, *t_context);
		}
	}
	if (added)
		MCnexecutioncontexts--;
	
	if (t_success)
		t_success = MCListCopyAsString(*t_context_list, r_value);

	if (!t_success)
		r_value = MCValueRetain(kMCEmptyString);

}
Exemplo n.º 16
0
MCExternal *MCExternal::Load(MCStringRef p_filename)
{
	bool t_success;
	t_success = true;
	
	// Load the referenced module.
	MCSAutoLibraryRef t_module;
	if (t_success)
	{
        &t_module = MCU_library_load(p_filename);
        if (!t_module.IsSet())
        {
            // try a relative path
            MCAutoStringRef t_relative_filename;
            if (MCStringFormat(&t_relative_filename, "./%@", p_filename))
            {
                &t_module = MCU_library_load(*t_relative_filename);
            }
        }
        
        if (!t_module.IsSet())
            t_success = false;
	}

	// Now loop through the loaded externals to see if we are already loaded.
	MCExternal *t_external;
	t_external = nil;
	if (t_success)
		for(t_external = s_externals; t_external != nil; t_external = t_external -> m_next)
			if (MCValueIsEqualTo(*t_module,
                                 *(t_external->m_module)))
				break;

	// If we failed to find the external, then we must try and prepare it.
	if (t_success && t_external == nil)
	{
		// First try and load it as a new style external.
        
		if (MCU_library_lookup(*t_module, MCSTR("MCExternalDescribe")) != nil)
			t_external = MCExternalCreateV1();
		else if (MCU_library_lookup(*t_module, MCSTR("getXtable")) != nil)
			t_external = MCExternalCreateV0();
		
		if (t_external != nil)
		{
			t_external -> m_next = s_externals;
			s_externals = t_external;

			t_external -> m_references = 0;
			t_external -> m_module = t_module.Take();
			t_external -> m_name = nil;
			
			t_success = t_external -> Prepare();
		}
		else
			t_success = false;
	}

	// Now we attempt to initialize the external - if it isn't already initialized.
	// (i.e. if the reference count > 0).
	if (t_success && t_external -> m_references == 0)
		t_success = t_external -> Initialize();

	// Finally, increment the reference count and we are done.
	if (t_success)
		t_external -> m_references += 1;

	return t_success ? t_external : nil;
}
Exemplo n.º 17
0
void MCPickExecPickOptionByIndex(MCExecContext &ctxt, int p_chunk_type, MCStringRef *p_option_lists, uindex_t p_option_list_count, uindex_t *p_initial_indices, uindex_t p_indices_count, bool p_use_hilite_type, bool p_use_picker, bool p_use_cancel, bool p_use_done, MCRectangle p_button_rect)
{
    
    MCAutoArray<MCPickList> t_pick_lists;
    
    char_t t_delimiter;
    switch ((MCChunkType)p_chunk_type)
    {
        // No access to the line/item delimiter set in the handler from the mobile-specific functions/commands
        // so following the old engine default values for them
        case kMCChunkTypeItem:
            t_delimiter = ',';
            break;
        case kMCChunkTypeWord:
        case kMCChunkTypeLine:
            t_delimiter = '\n';
            break;
        default:
            MCUnreachable();
    }
    uindex_t t_old_offset = 0;
    uindex_t t_new_offset = 0;
    
    bool t_success;
    t_success = true;
    
    for (uindex_t i = 0; i < p_option_list_count; i++)
    {
        MCStringRef t_option;
        MCPickList t_pick_list;
        MCAutoArray<MCStringRef> t_options;
        t_old_offset = 0;
        
        while (t_success && MCStringFirstIndexOfChar(p_option_lists[i], t_delimiter, t_old_offset, kMCCompareCaseless, t_new_offset))
        {
            t_success = MCStringCopySubstring(p_option_lists[i], MCRangeMakeMinMax(t_old_offset, t_new_offset), t_option);
            if (t_success)
                t_options . Push(t_option);
            
            t_old_offset = t_new_offset + 1;
        }
        // Append the remaining part of the options
        t_success = MCStringCopySubstring(p_option_lists[i], MCRangeMakeMinMax(t_old_offset, MCStringGetLength(p_option_lists[i])), t_option);
        if (t_success)
            t_options . Push(t_option);
        
        t_options . Take(t_pick_list . options, t_pick_list . option_count);
        t_pick_list . initial = p_initial_indices[i];
        t_pick_lists . Push(t_pick_list);
    }
    
    bool t_cancelled;
    
    uindex_t *t_result;
    uindex_t t_result_count = 0;
    
    // Open the picker and allow the user to select the options
    if (t_success)
        t_success = MCSystemPickOption(t_pick_lists . Ptr(), t_pick_lists . Size(), t_result, t_result_count, p_use_hilite_type, p_use_picker, p_use_cancel, p_use_done, t_cancelled, p_button_rect);
    
    ctxt.SetTheResultToEmpty();
    
    if (t_success)
    {
        if (t_cancelled)
        {
            // HC-2012-02-15 [[ BUG 9999 ]] Picker should return 0 if cancel was selected.
            ctxt . SetTheResultToNumber(0);
        }
        else
        {
            MCAutoListRef t_indices;
            t_success = MCListCreateMutable(',', &t_indices);
            for (uindex_t i = 0; i < t_result_count && t_success; i++)
            {
                MCAutoStringRef t_index;
                t_success = MCStringFormat(&t_index, "%u", t_result[i]);
                if (t_success)
                    t_success = MCListAppend(*t_indices, *t_index);
            }
            MCAutoStringRef t_string;
			/* UNCHECKED */ MCListCopyAsString(*t_indices, &t_string);
            ctxt . SetTheResultToValue(*t_string);
        }
    }
    
    // Free memory
    for (uindex_t i = 0; i < t_pick_lists . Size(); i++)
        for (uindex_t j = 0; j < t_pick_lists[i] . option_count; j++)
            MCValueRelease(t_pick_lists[i] . options[j]);
}
Exemplo n.º 18
0
static MCStringRef string_prepend(MCStringRef p_string, unichar_t p_prefix)
{
	MCStringRef t_new;
	MCStringFormat(t_new, "%lc%@", p_prefix, p_string);
	return t_new;
}
Exemplo n.º 19
0
// This call is primarily designed to be used by the plugin installer. As this
// is generally installed 'per-user', we simply scan '/proc' for processes owned
// by the calling user.
bool MCSystemListProcesses(MCSystemListProcessesCallback p_callback, void* p_context)
{
	bool t_success;
	t_success = true;

	DIR *t_dir;
	t_dir = nil;
	if (t_success)
	{
		t_dir = opendir("/proc");
		if (t_dir == nil)
			t_success = false;
	}

	if (t_success)
	{
		dirent *t_entry;
		while(t_success)
		{
			// Fetch the next entry
			t_entry = readdir(t_dir);
			if (t_entry == nil)
				break;

			// Work out if the entry is a process id
			int32_t t_pid;
            MCAutoStringRef t_dname;
            /* UNCHECKED */ MCStringCreateWithCString(t_entry -> d_name, &t_dname);
			if (!MCU_strtol(*t_dname, t_pid))
				continue;

			// Work out the full path ("/proc/<int>") and stat so we can
			// check ownership.
			char t_path[6 + I4L + 1];
			struct stat64 t_stat;
			sprintf(t_path, "/proc/%u", t_pid);
			stat64(t_path, &t_stat);
			if (t_stat . st_uid != getuid())
				continue;

			// We have a viable process to report. First fetch its path
            MCAutoStringRef t_exe_link, t_exe_path;
            /* UNCHECKED */ MCStringFormat(&t_exe_link, "/proc/%u/exe", t_pid);
			if (!MCS_resolvepath(*t_exe_link, &t_exe_path))
			{
				t_success = false;
				break;
			}

			// Next fetch its 'description' from the first line of the status
			// file.
			char t_status_file[6 + I4L + 7 + 1];
			char t_status[256];
			FILE *t_stream;
			sprintf(t_status_file, "/proc/%u/status", t_pid);
			t_stream = fopen(t_status_file, "r");
			if (t_stream != nil)
			{
				if (fgets(t_status, 256, t_stream) != nil)
				{
					char *t_tab;
					t_tab = strchr(t_status, '\t');
					if (t_tab != nil)
						MCMemoryMove(t_status, t_tab + 1, MCCStringLength(t_tab + 1));
				}
				else
					t_status[0] = '\0';
				fclose(t_stream);
			}
			else
				t_status[0] = '\0';

            MCAutoStringRef t_status_str;
            /* UNCHECKED */ MCStringCreateWithSysString(t_status, &t_status_str);
			t_success = p_callback(p_context, t_pid, *t_exe_path, *t_status_str);
		}
	}

	if (t_dir != nil)
		closedir(t_dir);

	return t_success;
}