Skip to content

Commit 6eaf5fb

Browse files
authored
Merge pull request #107 from ronnychevalier/rc/c-yet-more-cleanups
C cleanups
2 parents 621e625 + 06a48c6 commit 6eaf5fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+593
-1163
lines changed

build.rs

-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ fn main() {
251251
.define("HAVE_LIBPNG", Some("1"))
252252
.define("HAVE_MKSTEMP", Some("1"))
253253
.define("HAVE_STDINT_H", Some("1"))
254-
.define("HAVE_SYS_TYPES_H", Some("1"))
255-
.define("HAVE_SYS_WAIT_H", Some("1"))
256254
.define("HAVE_TM_GMTOFF", Some("1"))
257255
.define("HAVE_ZLIB", Some("1"))
258256
.define("HAVE_ZLIB_COMPRESS2", Some("1"))

tectonic/XeTeX_ext.c

+44-46
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ linebreak_start(int f, integer localeStrNum, uint16_t* text, integer textLength)
7474
UErrorCode status = U_ZERO_ERROR;
7575
char* locale = (char*)gettexstring(localeStrNum);
7676

77-
if (font_area[f] == OTGR_FONT_FLAG && strcmp(locale, "G") == 0) {
77+
if (font_area[f] == OTGR_FONT_FLAG && streq_ptr(locale, "G")) {
7878
XeTeXLayoutEngine engine = (XeTeXLayoutEngine) font_layout_engine[f];
7979
if (initGraphiteBreaking(engine, text, textLength))
8080
/* user asked for Graphite line breaking and the font supports it */
@@ -186,7 +186,7 @@ static void*
186186
load_mapping_file(const char* s, const char* e, char byteMapping)
187187
{
188188
TECkit_Converter cnv = 0;
189-
char* buffer = (char*) xmalloc(e - s + 5);
189+
char* buffer = xmalloc(e - s + 5);
190190
rust_input_handle_t map;
191191

192192
strncpy(buffer, s, e - s);
@@ -196,7 +196,7 @@ load_mapping_file(const char* s, const char* e, char byteMapping)
196196
map = ttstub_input_open (buffer, kpse_miscfonts_format, 0);
197197
if (map) {
198198
size_t mappingSize = ttstub_input_get_size (map);
199-
Byte *mapping = (Byte*) xmalloc(mappingSize);
199+
Byte *mapping = xmalloc(mappingSize);
200200

201201
if (ttstub_input_read(map, (char *) mapping, mappingSize) != mappingSize)
202202
_tt_abort("could not read mapping file \"%s\"", buffer);
@@ -233,8 +233,7 @@ check_for_tfm_font_mapping(void)
233233
{
234234
char* cp = strstr((char*)name_of_file + 1, ":mapping=");
235235
if (saved_mapping_name != NULL) {
236-
free(saved_mapping_name);
237-
saved_mapping_name = NULL;
236+
saved_mapping_name = mfree(saved_mapping_name);
238237
}
239238
if (cp != NULL) {
240239
*cp = 0;
@@ -253,8 +252,7 @@ load_tfm_font_mapping(void)
253252
if (saved_mapping_name != NULL) {
254253
rval = load_mapping_file(saved_mapping_name,
255254
saved_mapping_name + strlen(saved_mapping_name), 1);
256-
free(saved_mapping_name);
257-
saved_mapping_name = NULL;
255+
saved_mapping_name = mfree(saved_mapping_name);
258256
}
259257
return rval;
260258
}
@@ -380,53 +378,53 @@ readCommonFeatures(const char* feat, const char* end, float* extend, float* slan
380378
// returns 1 to go to next_option, -1 for bad_option, 0 to continue
381379
{
382380
const char* sep;
383-
if (strncmp(feat, "mapping", 7) == 0) {
384-
sep = feat + 7;
381+
sep = strstartswith(feat, "mapping");
382+
if (sep) {
385383
if (*sep != '=')
386384
return -1;
387385
loaded_font_mapping = load_mapping_file(sep + 1, end, 0);
388386
return 1;
389387
}
390388

391-
if (strncmp(feat, "extend", 6) == 0) {
392-
sep = feat + 6;
389+
sep = strstartswith(feat, "extend");
390+
if (sep) {
393391
if (*sep != '=')
394392
return -1;
395393
++sep;
396394
*extend = read_double(&sep);
397395
return 1;
398396
}
399397

400-
if (strncmp(feat, "slant", 5) == 0) {
401-
sep = feat + 5;
398+
sep = strstartswith(feat, "slant");
399+
if (sep) {
402400
if (*sep != '=')
403401
return -1;
404402
++sep;
405403
*slant = read_double(&sep);
406404
return 1;
407405
}
408406

409-
if (strncmp(feat, "embolden", 8) == 0) {
410-
sep = feat + 8;
407+
sep = strstartswith(feat, "embolden");
408+
if (sep) {
411409
if (*sep != '=')
412410
return -1;
413411
++sep;
414412
*embolden = read_double(&sep);
415413
return 1;
416414
}
417415

418-
if (strncmp(feat, "letterspace", 11) == 0) {
419-
sep = feat + 11;
416+
sep = strstartswith(feat, "letterspace");
417+
if (sep) {
420418
if (*sep != '=')
421419
return -1;
422420
++sep;
423421
*letterspace = read_double(&sep);
424422
return 1;
425423
}
426424

427-
if (strncmp(feat, "color", 5) == 0) {
425+
sep = strstartswith(feat, "color");
426+
if (sep) {
428427
const char* s;
429-
sep = feat + 5;
430428
if (*sep != '=')
431429
return -1;
432430
++sep;
@@ -533,28 +531,28 @@ loadOTfont(PlatformFontRef fontRef, XeTeXFont font, Fixed scaled_size, char* cp1
533531
while (*cp2 && (*cp2 != ':') && (*cp2 != ';') && (*cp2 != ','))
534532
++cp2;
535533

536-
if (strncmp(cp1, "script", 6) == 0) {
537-
cp3 = cp1 + 6;
534+
cp3 = strstartswith(cp1, "script");
535+
if (cp3) {
538536
if (*cp3 != '=')
539537
goto bad_option;
540538
++cp3;
541539
script = hb_tag_from_string(cp3, cp2 - cp3);
542540
goto next_option;
543541
}
544542

545-
if (strncmp(cp1, "language", 8) == 0) {
546-
cp3 = cp1 + 8;
543+
cp3 = strstartswith(cp1, "language");
544+
if (cp3) {
547545
if (*cp3 != '=')
548546
goto bad_option;
549547
++cp3;
550-
language = (char*)xmalloc(cp2 - cp3 + 1);
548+
language = xmalloc(cp2 - cp3 + 1);
551549
language[cp2 - cp3] = '\0';
552550
memcpy(language, cp3, cp2 - cp3);
553551
goto next_option;
554552
}
555553

556-
if (strncmp(cp1, "shaper", 6) == 0) {
557-
cp3 = cp1 + 6;
554+
cp3 = strstartswith(cp1, "shaper");
555+
if (cp3) {
558556
if (*cp3 != '=')
559557
goto bad_option;
560558
++cp3;
@@ -614,7 +612,7 @@ loadOTfont(PlatformFontRef fontRef, XeTeXFont font, Fixed scaled_size, char* cp1
614612
goto next_option;
615613
}
616614

617-
if (strncmp(cp1, "vertical", 8) == 0) {
615+
if (strstartswith(cp1, "vertical")) {
618616
cp3 = cp2;
619617
if (*cp3 == ';' || *cp3 == ':' || *cp3 == ',')
620618
--cp3;
@@ -656,7 +654,7 @@ loadOTfont(PlatformFontRef fontRef, XeTeXFont font, Fixed scaled_size, char* cp1
656654
engine = createLayoutEngine(fontRef, font, script, language,
657655
features, nFeatures, shapers, rgbValue, extend, slant, embolden);
658656

659-
if (engine == 0) {
657+
if (!engine) {
660658
// only free these if creation failed, otherwise the engine now owns them
661659
free(features);
662660
free(shapers);
@@ -731,18 +729,18 @@ find_native_font(unsigned char* uname, integer scaled_size)
731729
loaded_font_letter_space = 0;
732730

733731
splitFontName(name, &var, &feat, &end, &index);
734-
nameString = (char*) xmalloc(var - name + 1);
732+
nameString = xmalloc(var - name + 1);
735733
strncpy(nameString, name, var - name);
736734
nameString[var - name] = 0;
737735

738736
if (feat > var) {
739-
varString = (char*) xmalloc(feat - var);
737+
varString = xmalloc(feat - var);
740738
strncpy(varString, var + 1, feat - var - 1);
741739
varString[feat - var - 1] = 0;
742740
}
743741

744742
if (end > feat) {
745-
featString = (char*) xmalloc(end - feat);
743+
featString = xmalloc(end - feat);
746744
strncpy(featString, feat + 1, end - feat - 1);
747745
featString[end - feat - 1] = 0;
748746
}
@@ -767,11 +765,11 @@ find_native_font(unsigned char* uname, integer scaled_size)
767765
/* This is duplicated in XeTeXFontMgr::findFont! */
768766
setReqEngine(0);
769767
if (varString) {
770-
if (strncmp(varString, "/AAT", 4) == 0)
768+
if (strstartswith(varString, "/AAT"))
771769
setReqEngine('A');
772-
else if ((strncmp(varString, "/OT", 3) == 0) || (strncmp(varString, "/ICU", 4) == 0))
770+
else if ((strstartswith(varString, "/OT")) || (strstartswith(varString, "/ICU")))
773771
setReqEngine('O');
774-
else if (strncmp(varString, "/GR", 3) == 0)
772+
else if (strstartswith(varString, "/GR"))
775773
setReqEngine('G');
776774
}
777775

@@ -789,7 +787,7 @@ find_native_font(unsigned char* uname, integer scaled_size)
789787
} else {
790788
fontRef = findFontByName(nameString, varString, Fix2D(scaled_size));
791789

792-
if (fontRef != 0) {
790+
if (fontRef) {
793791
/* update name_of_file to the full name of the font, for error messages during font loading */
794792
const char* fullName = getFullName(fontRef);
795793
name_length = strlen(fullName);
@@ -1098,7 +1096,7 @@ makeXDVGlyphArrayData(void* pNode)
10981096
if (xdv_buffer != NULL)
10991097
free(xdv_buffer);
11001098
xdvBufSize = ((i / 1024) + 1) * 1024;
1101-
xdv_buffer = (char*) xmalloc(xdvBufSize);
1099+
xdv_buffer = xmalloc(xdvBufSize);
11021100
}
11031101

11041102
glyph_info = native_glyph_info_ptr(p);
@@ -1249,7 +1247,7 @@ make_font_def(integer f)
12491247
if (xdv_buffer != NULL)
12501248
free(xdv_buffer);
12511249
xdvBufSize = ((fontDefLength / 1024) + 1) * 1024;
1252-
xdv_buffer = (char*) xmalloc(xdvBufSize);
1250+
xdv_buffer = xmalloc(xdvBufSize);
12531251
}
12541252
cp = xdv_buffer;
12551253

@@ -1303,7 +1301,7 @@ apply_mapping(void* pCnv, uint16_t* txtPtr, int txtLen)
13031301

13041302
/* allocate outBuffer if not big enough */
13051303
if (outLength < txtLen * sizeof(UniChar) + 32) {
1306-
if (mapped_text != 0)
1304+
if (mapped_text != NULL)
13071305
free(mapped_text);
13081306
outLength = txtLen * sizeof(UniChar) + 32;
13091307
mapped_text = xmalloc(outLength);
@@ -1597,7 +1595,7 @@ measure_native_node(void* pNode, int use_glyph_metrics)
15971595
glyph_info = xcalloc(totalGlyphCount, native_glyph_info_size);
15981596
locations = (FixedPoint*)glyph_info;
15991597
glyphIDs = (uint16_t*)(locations + totalGlyphCount);
1600-
glyphAdvances = (Fixed*) xcalloc(totalGlyphCount, sizeof(Fixed));
1598+
glyphAdvances = xcalloc(totalGlyphCount, sizeof(Fixed));
16011599
totalGlyphCount = 0;
16021600

16031601
x = y = 0.0;
@@ -1607,9 +1605,9 @@ measure_native_node(void* pNode, int use_glyph_metrics)
16071605
nGlyphs = layoutChars(engine, txtPtr, logicalStart, length, txtLen,
16081606
(dir == UBIDI_RTL));
16091607

1610-
glyphs = (uint32_t*) xcalloc(nGlyphs, sizeof(uint32_t));
1611-
positions = (FloatPoint*) xcalloc(nGlyphs + 1, sizeof(FloatPoint));
1612-
advances = (float*) xcalloc(nGlyphs, sizeof(float));
1608+
glyphs = xcalloc(nGlyphs, sizeof(uint32_t));
1609+
positions = xcalloc(nGlyphs + 1, sizeof(FloatPoint));
1610+
advances = xcalloc(nGlyphs, sizeof(float));
16131611

16141612
getGlyphs(engine, glyphs);
16151613
getGlyphAdvances(engine, advances);
@@ -1639,9 +1637,9 @@ measure_native_node(void* pNode, int use_glyph_metrics)
16391637
double width = 0;
16401638
totalGlyphCount = layoutChars(engine, txtPtr, 0, txtLen, txtLen, (dir == UBIDI_RTL));
16411639

1642-
glyphs = (uint32_t*) xcalloc(totalGlyphCount, sizeof(uint32_t));
1643-
positions = (FloatPoint*) xcalloc(totalGlyphCount + 1, sizeof(FloatPoint));
1644-
advances = (float*) xcalloc(totalGlyphCount, sizeof(float));
1640+
glyphs = xcalloc(totalGlyphCount, sizeof(uint32_t));
1641+
positions = xcalloc(totalGlyphCount + 1, sizeof(FloatPoint));
1642+
advances = xcalloc(totalGlyphCount, sizeof(float));
16451643

16461644
getGlyphs(engine, glyphs);
16471645
getGlyphAdvances(engine, advances);
@@ -1652,7 +1650,7 @@ measure_native_node(void* pNode, int use_glyph_metrics)
16521650
glyph_info = xcalloc(totalGlyphCount, native_glyph_info_size);
16531651
locations = (FixedPoint*)glyph_info;
16541652
glyphIDs = (uint16_t*)(locations + totalGlyphCount);
1655-
glyphAdvances = (Fixed*) xcalloc(totalGlyphCount, sizeof(Fixed));
1653+
glyphAdvances = xcalloc(totalGlyphCount, sizeof(Fixed));
16561654
for (i = 0; i < totalGlyphCount; ++i) {
16571655
glyphIDs[i] = glyphs[i];
16581656
glyphAdvances[i] = D2Fix(advances[i]);

tectonic/XeTeX_mac.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ DoAATLayout(void* p, int justify)
143143
CFDictionaryRef runAttributes = CTRunGetAttributes(run);
144144
CFBooleanRef vertical = CFDictionaryGetValue(runAttributes, kCTVerticalFormsAttributeName);
145145
// TODO(jjgod): Avoid unnecessary allocation with CTRunGetFoosPtr().
146-
CGGlyph* glyphs = (CGGlyph*) xmalloc(count * sizeof(CGGlyph));
147-
CGPoint* positions = (CGPoint*) xmalloc(count * sizeof(CGPoint));
148-
CGSize* advances = (CGSize*) xmalloc(count * sizeof(CGSize));
146+
CGGlyph* glyphs = xmalloc(count * sizeof(CGGlyph));
147+
CGPoint* positions = xmalloc(count * sizeof(CGPoint));
148+
CGSize* advances = xmalloc(count * sizeof(CGSize));
149149
CGFloat runWidth = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), NULL, NULL, NULL);
150150
CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs);
151151
CTRunGetPositions(run, CFRangeMake(0, 0), positions);
@@ -443,7 +443,7 @@ getFileNameFromCTFont(CTFontRef ctFontRef, uint32_t *index)
443443
error = FT_New_Face (gFreeTypeLibrary, (char *) pathname, i, &face);
444444
if (!error) {
445445
const char *ps_name2 = FT_Get_Postscript_Name(face);
446-
if (strcmp(ps_name1, ps_name2) == 0) {
446+
if (streq_ptr(ps_name1, ps_name2)) {
447447
*index = i;
448448
break;
449449
}
@@ -648,9 +648,9 @@ loadAATfont(CTFontDescriptorRef descriptor, integer scaled_size, const char* cp1
648648
else if (ret == -1)
649649
goto bad_option;
650650

651-
if (strncmp(cp1, "tracking", 8) == 0) {
651+
cp3 = strstartswith(cp1, "tracking");
652+
if (cp3) {
652653
CFNumberRef trackingNumber;
653-
cp3 = cp1 + 8;
654654
if (*cp3 != '=')
655655
goto bad_option;
656656
++cp3;
@@ -664,7 +664,7 @@ loadAATfont(CTFontDescriptorRef descriptor, integer scaled_size, const char* cp1
664664
bad_option:
665665
// not a name=value pair, or not recognized....
666666
// check for plain "vertical" before complaining
667-
if (strncmp(cp1, "vertical", 8) == 0) {
667+
if (strstartswith(cp1, "vertical")) {
668668
cp3 = cp2;
669669
if (*cp3 == ';' || *cp3 == ':')
670670
--cp3;

tectonic/bibtex.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <tectonic/internals.h>
22

33
/* (Re)Allocate N items of type T using xmalloc/xrealloc. */
4-
#define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t)))
4+
#define XTALLOC(n, t) (xmalloc ((n) * sizeof (t)))
55

66
#define BIB_XRETALLOC_NOSET(array_name, array_var, type, size_var, new_size) \
77
(array_var) = (type *) xrealloc((array_var), (new_size + 1) * sizeof(type))
@@ -16,7 +16,7 @@
1616

1717
/* duplicated from xetexd.h: */
1818

19-
#define xmalloc_array(type,size) ((type*)xmalloc((size+1)*sizeof(type)))
19+
#define xmalloc_array(type,size) (xmalloc((size+1)*sizeof(type)))
2020

2121

2222
/* Sigh, I'm worried about ungetc() and EOF semantics in Bibtex's I/O, so

0 commit comments

Comments
 (0)