Skip to content

Commit 5514fff

Browse files
author
Ali Lloyd
committed
[[ RefactorSyntax ]] Remove unneccessary conversions from StringRef to c string and back during startup
1 parent 1c533af commit 5514fff

File tree

3 files changed

+9
-24
lines changed

3 files changed

+9
-24
lines changed

engine/src/capsule.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ bool MCCapsuleFillNoCopy(MCCapsuleRef self, const void *p_data, uint32_t p_data_
287287
return MCCapsuleFillCommon(self, p_data_length, 0, p_finished, true, p_data, nil);
288288
}
289289

290-
bool MCCapsuleFillFromFile(MCCapsuleRef self, const char *p_path, uint32_t p_offset, bool p_finished)
290+
bool MCCapsuleFillFromFile(MCCapsuleRef self, MCStringRef p_path, uint32_t p_offset, bool p_finished)
291291
{
292292
MCAssert(self != nil);
293293
MCAssert(p_path != nil);
@@ -299,9 +299,7 @@ bool MCCapsuleFillFromFile(MCCapsuleRef self, const char *p_path, uint32_t p_off
299299
t_stream = nil;
300300
if (t_success)
301301
{
302-
MCAutoStringRef p_path_string;
303-
/* UNCHECKED */ MCStringCreateWithCString(p_path, &p_path_string);
304-
t_stream = MCS_open(*p_path_string, kMCSOpenFileModeRead, True, False, 0);
302+
t_stream = MCS_open(p_path, kMCSOpenFileModeRead, True, False, 0);
305303
if (t_stream == nil)
306304
t_success = false;
307305
}

engine/src/capsule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ bool MCCapsuleFillNoCopy(MCCapsuleRef self, const void *data, uint32_t data_leng
200200
// will open the file at this point, but not read any data until required. It
201201
// will close the file as soon as it is finished with it. It will start reading
202202
// data at the given offset and for the given number of bytes.
203-
bool MCCapsuleFillFromFile(MCCapsuleRef self, const char *path, uint32_t offset, bool finished);
203+
bool MCCapsuleFillFromFile(MCCapsuleRef self, MCStringRef path, uint32_t offset, bool finished);
204204

205205
// The process method attempts to parse as many sections as it can, invoking the
206206
// callback for each one. Note that a section cannot be processed until the capsule

engine/src/mode_standalone.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,13 @@ extern void MCAndroidEngineRemoteCall(const char *, const char *, void *, ...);
312312

313313
IO_stat MCDispatch::startup(void)
314314
{
315-
char *t_mccmd;
316-
/* UNCHECKED */ MCStringConvertToCString(MCcmd, t_mccmd);
317315
startdir = MCS_getcurdir();
318-
enginedir = t_mccmd;
316+
/* UNCHECKED */ MCStringConvertToCString(MCcmd, enginedir);
319317
char *eptr = strrchr(enginedir, PATH_SEPARATOR);
320318
if (eptr != NULL)
321319
*eptr = '\0';
322320
else
323321
*enginedir = '\0';
324-
char *openpath = t_mccmd; //point to MCcmd string
325322

326323
// set up image cache before the first stack is opened
327324
MCCachedImageRep::init();
@@ -357,7 +354,6 @@ IO_stat MCDispatch::startup(void)
357354

358355
MCS_close(t_stream);
359356

360-
/* UNCHECKED */ MCStringCreateWithCString(openpath, MCcmd);
361357
MCdefaultstackptr = MCstaticdefaultstackptr = t_stack;
362358

363359
t_stack -> extraopen(false);
@@ -410,18 +406,15 @@ IO_stat MCDispatch::startup(void)
410406
// 0..2044 from project section
411407
// spill file
412408
// rest from project section
413-
char *t_spill;
414-
t_spill = (char *)malloc(strlen(openpath) + 5);
415-
sprintf(t_spill, "%s.dat", openpath);
409+
MCAutoStringRef t_spill;
410+
/* UNCHECKED */ MCStringFormat(&t_spill, "%@.dat", MCcmd);
416411
if (!MCCapsuleFillNoCopy(t_capsule, (const void *)&MCcapsule . data, 2044, false) ||
417-
!MCCapsuleFillFromFile(t_capsule, t_spill, 0, false) ||
412+
!MCCapsuleFillFromFile(t_capsule, *t_spill, 0, false) ||
418413
!MCCapsuleFillNoCopy(t_capsule, (const uint8_t *)&MCcapsule . data + 2044, 2048, true))
419414
{
420-
free(t_spill);
421415
MCCapsuleClose(t_capsule);
422416
return IO_ERROR;
423417
}
424-
free(t_spill);
425418
}
426419

427420
// Process the capsule
@@ -431,7 +424,6 @@ IO_stat MCDispatch::startup(void)
431424
return IO_ERROR;
432425
}
433426

434-
/* UNCHECKED */ MCStringCreateWithCString(openpath, MCcmd);
435427
MCdefaultstackptr = MCstaticdefaultstackptr = t_info . stack;
436428
MCCapsuleClose(t_capsule);
437429

@@ -495,7 +487,6 @@ IO_stat MCDispatch::startup(void)
495487
}
496488
MCS_close(t_stream);
497489

498-
/* UNCHECKED */ MCStringCreateWithCString(openpath, MCcmd);
499490
MCdefaultstackptr = MCstaticdefaultstackptr = t_stack;
500491

501492
t_stack -> extraopen(false);
@@ -536,16 +527,13 @@ IO_stat MCDispatch::startup(void)
536527
// 0..2044 from project section
537528
// spill file
538529
// rest from project section
539-
char *t_spill;
540-
t_spill = (char *)malloc(strlen(openpath) + 5);
541-
sprintf(t_spill, "%s.dat", openpath);
530+
MCAutoStringRef t_spill;
531+
/* UNCHECKED */ MCStringFormat(&t_spill, "%@.dat", MCcmd);
542532
if (!MCCapsuleFillFromFile(t_capsule, t_spill, 0, true))
543533
{
544-
free(t_spill);
545534
MCCapsuleClose(t_capsule);
546535
return IO_ERROR;
547536
}
548-
free(t_spill);
549537
}
550538

551539
// Process the capsule
@@ -555,7 +543,6 @@ IO_stat MCDispatch::startup(void)
555543
return IO_ERROR;
556544
}
557545

558-
/* UNCHECKED */ MCStringCreateWithCString(openpath, MCcmd);
559546
MCdefaultstackptr = MCstaticdefaultstackptr = t_info . stack;
560547
MCCapsuleClose(t_capsule);
561548

0 commit comments

Comments
 (0)