diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-09-16 14:34:41 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-09-16 15:14:22 -0700 |
commit | 6d526c90729d1b66da6c2546ad3ae794ff0f7751 (patch) | |
tree | d8e839a7aa03dad62a845fbf936e0d9d1491d09f | |
parent | 548998994665241967a43e583f5d0f00ee089289 (diff) |
Variable scope reductions as recommended by cppcheck
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | atom.c | 9 | ||||
-rw-r--r-- | bdfread.c | 31 | ||||
-rw-r--r-- | bdfutils.c | 14 | ||||
-rw-r--r-- | bitmap.c | 19 | ||||
-rw-r--r-- | bitmaputil.c | 27 | ||||
-rw-r--r-- | bufio.c | 21 | ||||
-rw-r--r-- | fontink.c | 9 | ||||
-rw-r--r-- | pcfwrite.c | 10 | ||||
-rw-r--r-- | private.c | 4 | ||||
-rw-r--r-- | utilbitmap.c | 14 |
10 files changed, 68 insertions, 90 deletions
@@ -72,10 +72,7 @@ ResizeHashTable(void) int newHashSize; int newHashMask; AtomListPtr *newHashTable; - int i; - int h; int newRehash; - int r; if (hashSize == 0) newHashSize = 1024; @@ -90,11 +87,11 @@ ResizeHashTable(void) } newHashMask = newHashSize - 1; newRehash = (newHashMask - 2); - for (i = 0; i < hashSize; i++) { + for (int i = 0; i < hashSize; i++) { if (hashTable[i]) { - h = (hashTable[i]->hash) & newHashMask; + int h = (hashTable[i]->hash) & newHashMask; if (newHashTable[h]) { - r = hashTable[i]->hash % newRehash | 1; + int r = hashTable[i]->hash % newRehash | 1; do { h += r; if (h >= newHashSize) @@ -90,7 +90,7 @@ bdfReadBitmap(CharInfoPtr pCI, FontFilePtr file, int bit, int byte, { int widthBits, widthBytes, widthHexChars; int height, row; - int i, inLineLen, nextByte; + int i, nextByte; unsigned char *pInBits, *picture, *line = NULL; unsigned char lineBuf[BDFLINELEN]; @@ -120,6 +120,8 @@ bdfReadBitmap(CharInfoPtr pCI, FontFilePtr file, int bit, int byte, /* 0 width characters? */ for (row = 0; row < height; row++) { + int inLineLen; + line = bdfGetLine(file, lineBuf, BDFLINELEN); if (!line) break; @@ -222,21 +224,18 @@ bdfSkipBitmap(FontFilePtr file, int height) static void bdfFreeFontBits(FontPtr pFont) { - BitmapFontPtr bitmapFont; - BitmapExtraPtr bitmapExtra; - int i, nencoding; + BitmapFontPtr bitmapFont = (BitmapFontPtr) pFont->fontPrivate; + BitmapExtraPtr bitmapExtra = (BitmapExtraPtr) bitmapFont->bitmapExtra; - bitmapFont = (BitmapFontPtr) pFont->fontPrivate; - bitmapExtra = (BitmapExtraPtr) bitmapFont->bitmapExtra; free(bitmapFont->ink_metrics); if (bitmapFont->encoding) { - nencoding = (pFont->info.lastCol - pFont->info.firstCol + 1) * + int nencoding = (pFont->info.lastCol - pFont->info.firstCol + 1) * (pFont->info.lastRow - pFont->info.firstRow + 1); - for (i = 0; i < NUM_SEGMENTS(nencoding); i++) + for (int i = 0; i < NUM_SEGMENTS(nencoding); i++) free(bitmapFont->encoding[i]); } free(bitmapFont->encoding); - for (i = 0; i < bitmapFont->num_chars; i++) + for (int i = 0; i < bitmapFont->num_chars; i++) free(bitmapFont->metrics[i].bits); free(bitmapFont->metrics); if (bitmapExtra) { @@ -861,13 +860,13 @@ bdfReadFont(FontPtr pFont, FontFilePtr file, goto BAILOUT; if (state.haveDefaultCh) { - unsigned int r, c, cols; + unsigned int r, c; r = pFont->info.defaultCh >> 8; c = pFont->info.defaultCh & 0xFF; if (pFont->info.firstRow <= r && r <= pFont->info.lastRow && pFont->info.firstCol <= c && c <= pFont->info.lastCol) { - cols = pFont->info.lastCol - pFont->info.firstCol + 1; + unsigned int cols = pFont->info.lastCol - pFont->info.firstCol + 1; r = r - pFont->info.firstRow; c = c - pFont->info.firstCol; bitmapFont->pDefault = ACCESSENCODING(bitmapFont->encoding, @@ -950,10 +949,8 @@ bdfPadToTerminal(FontPtr pFont) { BitmapFontPtr bitmapFont; BitmapExtraPtr bitmapExtra; - int i; int new_size; CharInfoRec new; - int w, h; bitmapFont = (BitmapFontPtr) pFont->fontPrivate; @@ -965,7 +962,7 @@ bdfPadToTerminal(FontPtr pFont) new.metrics.characterWidth = new.metrics.rightSideBearing; new_size = BYTES_FOR_GLYPH(&new, pFont->glyph); - for (i = 0; i < bitmapFont->num_chars; i++) { + for (int i = 0; i < bitmapFont->num_chars; i++) { new.bits = malloc(new_size); if (!new.bits) { bdfError("Couldn't allocate bits (%d)\n", new_size); @@ -978,9 +975,9 @@ bdfPadToTerminal(FontPtr pFont) } bitmapExtra = bitmapFont->bitmapExtra; if (bitmapExtra) { - w = GLYPHWIDTHPIXELS(&new); - h = GLYPHHEIGHTPIXELS(&new); - for (i = 0; i < GLYPHPADOPTIONS; i++) + int w = GLYPHWIDTHPIXELS(&new); + int h = GLYPHHEIGHTPIXELS(&new); + for (int i = 0; i < GLYPHPADOPTIONS; i++) bitmapExtra->bitmapsSizes[i] = bitmapFont->num_chars * (BYTES_PER_ROW(w, 1 << i) * h); } @@ -99,11 +99,10 @@ bdfWarning(const char *message, ...) unsigned char * bdfGetLine(FontFilePtr file, unsigned char *buf, int len) { - int c; - unsigned char *b; - for (;;) { - b = buf; + int c; + unsigned char *b = buf; + while ((c = FontFileGetc(file)) != FontFileEOF) { if (c == '\r') continue; @@ -227,11 +226,10 @@ unsigned char bdfHexByte(unsigned char *s) { unsigned char b = 0; - register char c; - int i; - for (i = 2; i; i--) { - c = *s++; + for (int i = 2; i; i--) { + char c = *s++; + if ((c >= '0') && (c <= '9')) b = (b << 4) + (c - '0'); else if ((c >= 'A') && (c <= 'F')) @@ -49,7 +49,6 @@ bitmapGetGlyphs(FontPtr pFont, unsigned long count, unsigned char *chars, CharInfoPtr *glyphsBase; register unsigned int c; register CharInfoPtr pci; - unsigned int r; CharInfoPtr **encoding; CharInfoPtr pDefault; @@ -115,6 +114,8 @@ bitmapGetGlyphs(FontPtr pFont, unsigned long count, unsigned char *chars, firstRow = pFont->info.firstRow; numRows = pFont->info.lastRow - firstRow + 1; while (count--) { + unsigned int r; + r = (*chars++) - firstRow; c = (*chars++) - firstCol; if (r < numRows && c < numCols && @@ -138,23 +139,19 @@ bitmapGetMetrics(FontPtr pFont, unsigned long count, unsigned char *chars, xCharInfo ** glyphs) /* RETURN */ { int ret; - xCharInfo *ink_metrics; - CharInfoPtr metrics; - BitmapFontPtr bitmapFont; - CharInfoPtr oldDefault; - int i; + BitmapFontPtr bitmapFont = (BitmapFontPtr) pFont->fontPrivate; + CharInfoPtr oldDefault = bitmapFont->pDefault; - bitmapFont = (BitmapFontPtr) pFont->fontPrivate; - oldDefault = bitmapFont->pDefault; bitmapFont->pDefault = &nonExistantChar; ret = bitmapGetGlyphs(pFont, count, chars, charEncoding, glyphCount, (CharInfoPtr *) glyphs); if (ret == Successful) { if (bitmapFont->ink_metrics) { - metrics = bitmapFont->metrics; - ink_metrics = bitmapFont->ink_metrics; - for (i = 0; i < *glyphCount; i++) { + CharInfoPtr metrics = bitmapFont->metrics; + xCharInfo *ink_metrics = bitmapFont->ink_metrics; + + for (int i = 0; i < *glyphCount; i++) { if (glyphs[i] != (xCharInfo *) & nonExistantChar) glyphs[i] = ink_metrics + (((CharInfoPtr) glyphs[i]) - metrics); diff --git a/bitmaputil.c b/bitmaputil.c index c74964a..324bd8f 100644 --- a/bitmaputil.c +++ b/bitmaputil.c @@ -73,7 +73,6 @@ bitmapComputeFontBounds(FontPtr pFont) { BitmapFontPtr bitmapFont = (BitmapFontPtr) pFont->fontPrivate; int nchars; - int r, c; CharInfoPtr ci; int maxOverlap; int overlap; @@ -117,8 +116,8 @@ bitmapComputeFontBounds(FontPtr pFont) *maxbounds = initMaxMetrics; i = 0; maxOverlap = MINSHORT; - for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) { - for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) { + for (int r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) { + for (int c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) { ci = ACCESSENCODING(bitmapFont->encoding, i); if (ci) { COMPUTE_MINMAX(&ci->metrics); @@ -148,13 +147,6 @@ void bitmapComputeFontInkBounds(FontPtr pFont) { BitmapFontPtr bitmapFont = (BitmapFontPtr) pFont->fontPrivate; - int nchars; - int r, c; - CharInfoPtr cit; - xCharInfo *ci; - int offset; - xCharInfo *minbounds, *maxbounds; - int i; if (!bitmapFont->ink_metrics) { if (bitmapFont->bitmapExtra) { @@ -167,6 +159,9 @@ bitmapComputeFontInkBounds(FontPtr pFont) pFont->info.ink_maxbounds = pFont->info.maxbounds; } else { + xCharInfo *minbounds, *maxbounds, *ci; + int nchars, i; + if (bitmapFont->bitmapExtra) { minbounds = &bitmapFont->bitmapExtra->info.ink_minbounds; maxbounds = &bitmapFont->bitmapExtra->info.ink_maxbounds; @@ -189,11 +184,12 @@ bitmapComputeFontInkBounds(FontPtr pFont) *minbounds = initMinMetrics; *maxbounds = initMaxMetrics; i = 0; - for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) { - for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) { - cit = ACCESSENCODING(bitmapFont->encoding, i); + for (int r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) { + for (int c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) + { + CharInfoPtr cit = ACCESSENCODING(bitmapFont->encoding, i); if (cit) { - offset = cit - bitmapFont->metrics; + int offset = cit - bitmapFont->metrics; ci = &bitmapFont->ink_metrics[offset]; COMPUTE_MINMAX(ci); minbounds->attributes &= ci->attributes; @@ -210,7 +206,6 @@ Bool bitmapAddInkMetrics(FontPtr pFont) { BitmapFontPtr bitmapFont; - int i; bitmapFont = (BitmapFontPtr) pFont->fontPrivate; bitmapFont->ink_metrics = malloc(bitmapFont->num_chars * sizeof(xCharInfo)); @@ -219,7 +214,7 @@ bitmapAddInkMetrics(FontPtr pFont) bitmapFont->num_chars, (unsigned long) sizeof(xCharInfo)); return FALSE; } - for (i = 0; i < bitmapFont->num_chars; i++) + for (int i = 0; i < bitmapFont->num_chars; i++) FontCharInkMetrics(pFont, &bitmapFont->metrics[i], &bitmapFont->ink_metrics[i]); pFont->info.inkMetrics = TRUE; @@ -82,18 +82,16 @@ BufFileRawFill(BufFilePtr f) static int BufFileRawSkip(BufFilePtr f, int count) { - int curoff; - int fileoff; - int todo; + int curoff = f->bufp - f->buffer; + int fileoff = curoff + f->left; - curoff = f->bufp - f->buffer; - fileoff = curoff + f->left; if (curoff + count <= fileoff) { f->bufp += count; f->left -= count; } else { - todo = count - (fileoff - curoff); + int todo = count - (fileoff - curoff); + if (lseek(FileDes(f), todo, 1) == -1) { if (errno != ESPIPE) return BUFFILEEOF; @@ -174,11 +172,10 @@ BufFileOpenWrite(int fd) int BufFileRead(BufFilePtr f, char *b, int n) { - int c, cnt; + int cnt = n; - cnt = n; while (cnt--) { - c = BufFileGet(f); + int c = BufFileGet(f); if (c == BUFFILEEOF) break; *b++ = c; @@ -189,9 +186,8 @@ BufFileRead(BufFilePtr f, char *b, int n) int BufFileWrite(BufFilePtr f, char *b, int n) { - int cnt; + int cnt = n; - cnt = n; while (cnt--) { if (BufFilePut(*b++, f) == BUFFILEEOF) return BUFFILEEOF; @@ -202,9 +198,8 @@ BufFileWrite(BufFilePtr f, char *b, int n) int BufFileClose(BufFilePtr f, int doClose) { - int ret; + int ret = (*f->close) (f, doClose); - ret = (*f->close) (f, doClose); free(f); return ret; } @@ -155,7 +155,6 @@ FontCharInkMetrics(FontPtr pFont, CharInfoPtr pCI, xCharInfo * pInk) void FontCharReshape(FontPtr pFont, CharInfoPtr pSrc, CharInfoPtr pDst) { - int x, y; unsigned char *in_line, *out_line; unsigned char *oldglyph, *newglyph; int inwidth; @@ -182,8 +181,8 @@ FontCharReshape(FontPtr pFont, CharInfoPtr pSrc, CharInfoPtr pDst) in_line += (y_min + pSrc->metrics.ascent) * in_bytes; out_line += (y_min + pDst->metrics.ascent) * out_bytes; if (pFont->bit == MSBFirst) { - for (y = y_min; y < y_max; y++) { - for (x = x_min; x < x_max; x++) { + for (int y = y_min; y < y_max; y++) { + for (int x = x_min; x < x_max; x++) { if (ISBITONMSB(x - pSrc->metrics.leftSideBearing, in_line)) SETBITMSB(x - pDst->metrics.leftSideBearing, out_line); } @@ -192,8 +191,8 @@ FontCharReshape(FontPtr pFont, CharInfoPtr pSrc, CharInfoPtr pDst) } } else { - for (y = y_min; y < y_max; y++) { - for (x = x_min; x < x_max; x++) { + for (int y = y_min; y < y_max; y++) { + for (int x = x_min; x < x_max; x++) { if (ISBITONLSB(x - pSrc->metrics.leftSideBearing, in_line)) SETBITLSB(x - pDst->metrics.leftSideBearing, out_line); } @@ -117,13 +117,11 @@ pcfPutINT8(FontFilePtr file, CARD32 format, int c) static void pcfWriteTOC(FontFilePtr file, PCFTablePtr table, int count) { - CARD32 version; - int i; + CARD32 version = PCF_FILE_VERSION; - version = PCF_FILE_VERSION; pcfPutLSB32(file, version); pcfPutLSB32(file, count); - for (i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { pcfPutLSB32(file, table->type); pcfPutLSB32(file, table->format); pcfPutLSB32(file, table->size); @@ -216,7 +214,7 @@ int pcfWriteFont(FontPtr pFont, FontFilePtr file) { PCFTableRec tables[32], *table; - CARD32 mask, bit; + CARD32 mask; int ntables; int size; CARD32 format; @@ -272,7 +270,7 @@ pcfWriteFont(FontPtr pFont, FontFilePtr file) ntables = 0; table = tables; while (mask) { - bit = lowbit(mask); + CARD32 bit = lowbit(mask); mask &= ~bit; table->type = bit; switch (bit) { @@ -79,9 +79,9 @@ ResetFontPrivateIndex(void) Bool _FontSetNewPrivate(FontPtr pFont, int n, pointer ptr) { - pointer *new; - if (n > pFont->maxPrivate) { + pointer *new; + if (pFont->devPrivates && pFont->devPrivates != (pointer) (&pFont[1])) { new = realloc(pFont->devPrivates, (n + 1) * sizeof(pointer)); if (!new) diff --git a/utilbitmap.c b/utilbitmap.c index 5d06124..b0c0249 100644 --- a/utilbitmap.c +++ b/utilbitmap.c @@ -88,9 +88,9 @@ BitOrderInvert(unsigned char *buf, int nbytes) void TwoByteSwap(unsigned char *buf, int nbytes) { - unsigned char c; - for (; nbytes > 0; nbytes -= 2, buf += 2) { + unsigned char c; + c = buf[0]; buf[0] = buf[1]; buf[1] = c; @@ -103,12 +103,13 @@ TwoByteSwap(unsigned char *buf, int nbytes) void FourByteSwap(unsigned char *buf, int nbytes) { - unsigned char c; - for (; nbytes > 0; nbytes -= 4, buf += 4) { + unsigned char c; + c = buf[0]; buf[0] = buf[3]; buf[3] = c; + c = buf[1]; buf[1] = buf[2]; buf[2] = c; @@ -124,7 +125,6 @@ RepadBitmap(char *pSrc, char *pDst, unsigned int srcPad, unsigned int dstPad, int width, int height) { int srcWidthBytes, dstWidthBytes; - int row, col; char *pTmpSrc, *pTmpDst; switch (srcPad) { @@ -165,7 +165,9 @@ RepadBitmap(char *pSrc, char *pDst, width = dstWidthBytes; pTmpSrc = pSrc; pTmpDst = pDst; - for (row = 0; row < height; row++) { + for (int row = 0; row < height; row++) { + int col; + for (col = 0; col < width; col++) *pTmpDst++ = *pTmpSrc++; while (col < dstWidthBytes) { |