scm.cpp revision a299266cddc1ae14d76d725a660bb278816bc151
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * IPRT Testcase / Tool - Source Code Massager.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * available from http://www.virtualbox.org. This file is free software;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * General Public License (GPL) as published by the Free Software
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * The contents of this file may alternatively be used under the terms
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * of the Common Development and Distribution License Version 1.0
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * VirtualBox OSE distribution, in which case the provisions of the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * CDDL are applicable instead of those of the GPL.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * You may elect to license modified versions of this file under the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * terms and conditions of either the GPL or the CDDL or both.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * additional information or have any questions.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*******************************************************************************
a299266cddc1ae14d76d725a660bb278816bc151vboxsync* Header Files *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync*******************************************************************************/
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*******************************************************************************
a299266cddc1ae14d76d725a660bb278816bc151vboxsync* Internal Functions *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync*******************************************************************************/
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** End of line marker type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to an end of line marker type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Line record.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The offset of the line. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The line length, excluding the LF character.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @todo This could be derived from the offset of the next line if that wasn't
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * so tedious. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The end of line marker type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a line record. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Source code massager stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef struct SCMSTREAM
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Pointer to the file memory. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current stream position. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current stream size. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The size of the memory pb points to. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Line records. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current line. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current stream size given in lines. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The sizeof the the memory backing paLines. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Set if write-only, clear if read-only. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Set if the memory pb points to is from RTFileReadAll. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Set if fully broken into lines. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Stream status code (IPRT). */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a SCM stream. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a const SCM stream. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * A rewriter.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * This works like a stream editor, reading @a pIn, modifying it and writing it
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * to @a pOut.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if any changes were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef bool (*PFNSCMREWRITER)(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Configuration entry.
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef struct SCMCFGENTRY
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Number of rewriters. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Pointer to an array of rewriters. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** File pattern (simple). */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Diff state.
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef struct SCMDIFFSTATE
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to ignore end of line markers when diffing. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to ignore trailing whitespace. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to ignore leading whitespace. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to print special characters in human readable form or not. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Where to push the diff. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a diff state. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*******************************************************************************
a299266cddc1ae14d76d725a660bb278816bc151vboxsync* Internal Functions *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync*******************************************************************************/
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_StripTrailingBlanks(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ExpandTabs(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceNativeEol(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceLF(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceCRLF(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_AdjustTrailingLines(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_Makefile_kup(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_Makefile_kmk(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_C_and_CPP(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*******************************************************************************
a299266cddc1ae14d76d725a660bb278816bc151vboxsync* Global Variables *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync*******************************************************************************/
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fDryRun = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fStripTrailingBlanks = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fStripTrailingLines = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fForceFinalEol = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fForceTrailingLine = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fConvertTabs = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fConvertEol = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fDiffSpecialChars = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fDiffIgnoreEol = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fDiffIgnoreLeadingWS = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool g_fDiffIgnoreTrailingWS = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *g_pszFileFilterOut = "*.exe|*.com|20*-*-*.log";
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *g_pszDirFilterOut = \
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "webtools|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "SlickEdit|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Devices/Storage/VBoxHDDFormats/StorageCraft/*/."
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { RT_ELEMENTS(g_aRewritersFor_Makefile_kup), &g_aRewritersFor_Makefile_kup[0], "Makefile.kup" },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { RT_ELEMENTS(g_aRewritersFor_Makefile_kmk), &g_aRewritersFor_Makefile_kmk[0], "Makefile.kmk" },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { RT_ELEMENTS(g_aRewritersFor_C_and_CPP), &g_aRewritersFor_C_and_CPP[0], "*.c|*.h|*.cpp|*.hpp|*.C|*.CPP|*.cxx|*.cc" },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { RT_ELEMENTS(g_aRewritersFor_ShellScripts), &g_aRewritersFor_ShellScripts[0], "*.sh|configure" },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { RT_ELEMENTS(g_aRewritersFor_BatchFiles), &g_aRewritersFor_BatchFiles[0], "*.bat|*.cmd|*.btm|*.vbs|*.ps1" },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- memory streams -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Initializes the stream structure.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream structure.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param fWriteOrRead The value of the fWriteOrRead stream member.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic void scmStreamInitInternal(PSCMSTREAM pStream, bool fWriteOrRead)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Initialize an input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream to initialize.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFilename The file to take the stream content from.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamInitForReading(PSCMSTREAM pStream, const char *pszFilename)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmStreamInitInternal(pStream, false /*fWriteOrRead*/);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = pStream->rc = RTFileReadAll(pszFilename, &pvFile, &cbFile);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Initialize an output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream to initialize.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pRelatedStream Pointer to a related stream. NULL is fine.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamInitForWriting(PSCMSTREAM pStream, PCSCMSTREAM pRelatedStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmStreamInitInternal(pStream, true /*fWriteOrRead*/);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* allocate stuff */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cLinesEstimate = pRelatedStream && pRelatedStream->fFullyLineated
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ? pRelatedStream->cLines + pRelatedStream->cLines / 10
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines = (PSCMSTREAMLINE)RTMemAlloc(cLinesEstimate * sizeof(SCMSTREAMLINE));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Frees the resources associated with the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Nothing is happens to whatever the stream was initialized from or dumped to.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream to delete.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the stream status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Grows the buffer of a write stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in write mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cbAppending The minimum number of bytes to grow the buffer
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmStreamGrowBuffer(PSCMSTREAM pStream, size_t cbAppending)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cbAllocated += RT_MAX(0x1000 + cbAppending, cbAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pvNew = RTMemDupEx(pStream->pch, pStream->off, cbAllocated - pStream->off);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Grows the line array of a stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iMinLine Minimum line number.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmStreamGrowLines(PSCMSTREAM pStream, size_t iMinLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLinesAllocated += RT_MAX(512 + iMinLine, cLinesAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync void *pvNew = RTMemRealloc(pStream->paLines, cLinesAllocated * sizeof(SCMSTREAMLINE));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewinds the stream and sets the mode to read.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewinds the stream and sets the mode to write.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Checks if it's a text stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Not 100% proof.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if it probably is a text file, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Write or read, doesn't matter.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Performs an integrity check of the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Perform sanity checks.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iLine = 0; iLine < pStream->cLines; iLine++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t offEol = pStream->paLines[iLine].off + pStream->paLines[iLine].cch;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(offEol + pStream->paLines[iLine].enmEol <= cbFile, VERR_INTERNAL_ERROR_2);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->pch[offEol] == '\n', VERR_INTERNAL_ERROR_3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->pch[offEol] == '\r', VERR_INTERNAL_ERROR_3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->pch[offEol + 1] == '\n', VERR_INTERNAL_ERROR_3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_4);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_5);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Writes the stream to a file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFilenameFmt The filename format string.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param ... Format arguments.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamWriteToFile(PSCMSTREAM pStream, const char *pszFilenameFmt, ...)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Check that what we're going to write makes sense first.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Do the actual writing.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTFileOpenV(&hFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE, pszFilenameFmt, va);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTFileWrite(hFile, pStream->pch, pStream->cb, NULL);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Worker for ScmStreamGetLine that builds the line number index while parsing
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns Same as SCMStreamGetLine.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pcchLine Where to return the line length.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param penmEol Where to return the kind of end of line marker.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *scmStreamGetLineInternal(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(iLine >= pStream->cLinesAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pch = (const char *)memchr(pchRet, '\n', cb);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_LF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_CRLF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Internal worker that lineates a stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Caller must check that it is in
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Save the stream position. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Get each line. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (scmStreamGetLineInternal(pStream, &cchLine, &enmEol))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* nothing */;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync Assert(RT_FAILURE(pStream->rc) || pStream->fFullyLineated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Restore the position */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the current stream position as a line number.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns The current line (0-based).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Gets the number of lines in the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns The number of lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Seeks to a given line in the tream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLine The line to seek to. If this is beyond the end
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * of the stream, the position is set to the end.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamSeekByLine(PSCMSTREAM pStream, size_t iLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Must be fully lineated of course. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Ok, do the job. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get a numbered line from the stream (changes the position).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * A line is always delimited by a LF character or the end of the stream. The
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * delimiter is not included in returned line length, but instead returned via
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * the @a penmEol indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns Pointer to the first character in the line, not NULL terminated.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * NULL if the end of the stream has been reached or some problem
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLine The line to get (0-based).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pcchLine The length.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param penmEol Where to return the end of line type indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Make sure it's fully lineated so we can use the index. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* End of stream? */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Get the data. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRet = &pStream->pch[pStream->paLines[iLine].off];
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* update the stream position. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get a line from the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * A line is always delimited by a LF character or the end of the stream. The
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * delimiter is not included in returned line length, but instead returned via
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * the @a penmEol indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns Pointer to the first character in the line, not NULL terminated.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * NULL if the end of the stream has been reached or some problem
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pcchLine The length.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param penmEol Where to return the end of line type indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *ScmStreamGetLine(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmStreamGetLineInternal(pStream, pcchLine, penmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return ScmStreamGetLineByNo(pStream, pStream->iLine, pcchLine, penmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Checks if the given line is empty or full of white space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if white space only, false if not (or if non-existant).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLine The line in question.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool ScmStreamIsWhiteLine(PSCMSTREAM pStream, size_t iLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return cchLine == 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Try figure out the end of line style of the give stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns Most likely end of line style.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLF = (const char *)memchr(pStream->pch, '\n', pStream->cb);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pchLF && pchLF != pStream->pch && pchLF[-1] == '\r')
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the end of line indicator type for a line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns The EOL indicator. If the line isn't found, the default EOL
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * indicator is return.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLine The line (0-base).
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncSCMEOL ScmStreamGetEolByLine(PSCMSTREAM pStream, size_t iLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Appends a line to the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in write mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pchLine Pointer to the line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cchLine Line length.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param enmEol Which end of line indicator to use.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamPutLine(PSCMSTREAM pStream, const char *pchLine, size_t cchLine, SCMEOL enmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Make sure the previous line has a new-line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->paLines[iLine].cch == 0, VERR_INTERNAL_ERROR_3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol2 = enmEol != SCMEOL_NONE ? enmEol : ScmStreamGetEol(pStream);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(off + cchLine + enmEol + enmEol2 > pStream->cbAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol + enmEol2);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Ensure we've got sufficient buffer space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(off + cchLine + enmEol > pStream->cbAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Add a line record.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off + cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copy the line
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Start a new line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Writes to the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in write mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pchBuf What to write.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cchBuf How much to write.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamWrite(PSCMSTREAM pStream, const char *pchBuf, size_t cchBuf)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Ensure we've got sufficient buffer space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(off + cchBuf > pStream->cbAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Deal with lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLF = (const char *)memchr(pchBuf, '\n', cchBuf);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || pStream->pch[pStream->paLines[iLine].off + pStream->paLines[iLine].cch - 1] != '\r')
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLF = (const char *)memchr(pchLF + 1, '\n', cchLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copy the data and update position and size.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Write a character to the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Must be in write mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pchBuf What to write.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cchBuf How much to write.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Only deal with the simple cases here, use ScmStreamWrite for the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * annyoing stuff.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Just append it.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * The stream positions will be used and changed in both streams.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pDst The destionation stream. Must be in write mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cLines The number of lines. (0 is accepted.)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pSrc The source stream. Must be in read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamCopyLines(PSCMSTREAM pDst, PSCMSTREAM pSrc, size_t cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pDst->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(!pSrc->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cLines-- > 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLine(pSrc, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pDst->rc = (RT_FAILURE(pSrc->rc) ? pSrc->rc : VERR_EOF);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = ScmStreamPutLine(pDst, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- diff -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Prints a range of lines with a prefix.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pState The diff state.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param chPrefix The prefix.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream to get the lines from.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLine The first line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cLines The number of lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic void scmDiffPrintLines(PSCMDIFFSTATE pState, char chPrefix, PSCMSTREAM pStream, size_t iLine, size_t cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cLines-- > 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Reports a difference and propells the streams to the lines following the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns New pState->cDiff value (just to return something).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pState The diff state. The cDiffs member will be
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * incremented.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cMatches The resync length.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLeft Where the difference starts on the left side.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cLeft How long it is on this side. ~(size_t)0 is used
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * to indicate that it goes all the way to the end.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iRight Where the difference starts on the right side.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cRight How long it is.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic size_t scmDiffReport(PSCMDIFFSTATE pState, size_t cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Adjust the input.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Print header if it's the first difference
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "diff %s %s\n", pState->pszFilename, pState->pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Emit the change description.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu,%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1, iRight + cRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu%c%zu,%zu\n", iLeft + 1, ch, iRight + 1, iRight + cRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu%c%zu\n", iLeft + 1, ch, iRight + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * And the lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffPrintLines(pState, '<', pState->pLeft, iLeft, cLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffPrintLines(pState, '>', pState->pRight, iRight, cRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Reposition the streams (safely ignores return value).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamSeekByLine(pState->pLeft, iLeft + cLeft + cMatches);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamSeekByLine(pState->pRight, iRight + cRight + cMatches);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Helper for scmDiffCompare that takes care of trailing spaces and stuff
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * like that.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool scmDiffCompareSlow(PSCMDIFFSTATE pState,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchLeft > 0 && RT_C_IS_SPACE(pchLeft[cchLeft - 1]))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchRight > 0 && RT_C_IS_SPACE(pchRight[cchRight - 1]))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compare two lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if the are equal, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncDECLINLINE(bool) scmDiffCompare(PSCMDIFFSTATE pState,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compares two sets of lines from the two files.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if they matches, false if they don't.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pState The diff state.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLeft Where to start in the left stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iRight Where to start in the right stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cLines How many lines to compare.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool scmDiffCompareLines(PSCMDIFFSTATE pState, size_t iLeft, size_t iRight, size_t cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iLeft + iLine, &cchLeft, &enmEolLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iRight + iLine, &cchRight, &enmEolRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Resynchronize the two streams and reports the difference.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Upon return, the streams will be positioned after the block of @a cMatches
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * lines where it resynchronized them.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns pState->cDiffs (just so we can use it in a return statement).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pState The state.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cMatches The number of lines that needs to match for the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * stream to be considered synchronized again.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic size_t scmDiffSynchronize(PSCMDIFFSTATE pState, size_t cMatches)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const iStartLeft = ScmStreamTellLine(pState->pLeft) - 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const iStartRight = ScmStreamTellLine(pState->pRight) - 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compare each new line from each of the streams will all the preceding
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * ones, including iStartLeft/Right.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the next line in the left stream and compare it against all the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * preceding lines on the right side.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iRange, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iRight = cMatches - 1; iRight < iRange; iRight++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRight,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( scmDiffCompare(pState, pchLine, cchLine, enmEol, pchRight, cchRight, enmEolRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the next line in the right stream and compare it against all the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * lines on the right side.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLine = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRange, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iLeft = cMatches - 1; iLeft <= iRange; iLeft++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchLine, cchLine, enmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Creates a diff of the changes between the streams @a pLeft and @a pRight.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * This currently only implements the simplest diff format, so no contexts.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Also, note that we won't detect differences in the final newline of the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns The number of differences.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFilename The filename.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pLeft The left side stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pRight The right side stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param fIgnoreEol Whether to ignore end of line markers.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param fIgnoreLeadingWhite Set if leading white space should be ignored.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param fIgnoreTrailingWhite Set if trailing white space should be ignored.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param fSpecialChars Whether to print special chars in a human
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * readable form or not. Currently only applicable
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * to the EOL marker.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pDiff Where to write the diff.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncsize_t ScmDiffStreams(const char *pszFilename, PSCMSTREAM pLeft, PSCMSTREAM pRight, bool fIgnoreEol,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fIgnoreLeadingWhite, bool fIgnoreTrailingWhite, bool fSpecialChars, PRTSTREAM pDiff)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Set up the diff state.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compare them line by line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLeft = ScmStreamGetLine(pLeft, &cchLeft, &enmEolLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchRight = ScmStreamGetLine(pRight, &cchRight, &enmEolRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!scmDiffCompare(&State, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Deal with any remaining differences.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffReport(&State, 0, ScmStreamTellLine(pLeft) - 1, ~(size_t)0, ScmStreamTellLine(pRight), 0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffReport(&State, 0, ScmStreamTellLine(pLeft), 0, ScmStreamTellLine(pRight) - 1, ~(size_t)0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Report any errors.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Left diff stream error: %Rrc\n", ScmStreamGetStatus(pLeft));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Right diff stream error: %Rrc\n", ScmStreamGetStatus(pRight));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- misc -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Prints a verbose message if the level is high enough.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLevel The required verbosity level.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFormat The message format string.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param ... Format arguments.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic void ScmVerbose(int iLevel, const char *pszFormat, ...)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- rewriters -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Strip trailing blanks (space & tab).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns True if modified, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_StripTrailingBlanks(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchLine > 0 && RT_C_IS_BLANK(pchLine[cchLine - 1]))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Expand tabs.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns True if modified, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ExpandTabs(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchTab = (const char *)memchr(pchChunk, '\t', cchLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchChunk, cchLeft, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Worker for rewrite_ForceNativeEol, rewrite_ForceLF and rewrite_ForceCRLF.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param enmDesiredEol The desired end of line indicator type.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceEol(PSCMSTREAM pIn, PSCMSTREAM pOut, SCMEOL enmDesiredEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Force native end of line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceNativeEol(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Force the stream to use LF as the end of line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceLF(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Force the stream to use CRLF as the end of line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceCRLF(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Strip trailing blank lines and/or make sure there is exactly one blank line
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * at the end of the file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @remarks ASSUMES trailing white space has been removed already.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_AdjustTrailingLines(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!g_fStripTrailingLines && !g_fForceTrailingLine && !g_fForceFinalEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Empty files remains empty. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Figure out if we need to adjust the number of lines or not. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && ScmStreamGetEolByLine(pIn, cLinesNew - 1) == SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Copy the number of lines we've arrived at. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamPutLine(pOut, "", 0, ScmStreamGetEol(pIn));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, " * Adjusted trailing blank lines\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Makefile.kup are empty files, enforce this.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_Makefile_kup(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* These files should be zero bytes. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, " * Truncated file to zero bytes\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewrite a kBuild makefile.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Ideas for Makefile.kmk and Config.kmk:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - sort if1of/ifn1of sets.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - line continuation slashes should only be preceeded by one space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_Makefile_kmk(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewrite a C/C++ source or header file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Ideas for C/C++:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - space after if, while, for, switch
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - spaces in for (i=0;i<x;i++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - complex conditional, bird style.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - remove unnecessary parentheses.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - sort defined RT_OS_*|| and RT_ARCH
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - sizeof without parenthesis.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - defined without parenthesis.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - trailing spaces.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - parameter indentation.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - space after comma.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - while (x--); -> multi line + comment.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - else statement;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - space between function and left parenthesis.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - TODO, XXX, @todo cleanup.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - Space before/after '*'.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - ensure new line at end of file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - Indentation of precompiler statements (#ifdef, #defines).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - space between functions.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_C_and_CPP(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Processes a file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFilename The file name.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszBasename The base name (pointer within @a pszFilename).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cchBasename The length of the base name. (For passing to
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * RTStrSimplePatternMultiMatch.)
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Do the file level filtering.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && !RTStrSimplePatternMultiMatch(g_pszFileFilter, RTSTR_MAX, pszBasename, cchBasename, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(4, "file filter mismatch: \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && RTStrSimplePatternMultiMatch(g_pszFileFilterOut, RTSTR_MAX, pszBasename, cchBasename, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(4, "file filter out: \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Try find a matching rewrite config for this filename.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(3, "No rewriters configured for \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(3, "\"%s\" matched \"%s\"\n", pszFilename, pCfg->pszFilePattern);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Create an input stream from the file and check that it's text.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = ScmStreamInitForReading(&Stream1, pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(1, "Processing '%s'...\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Create two more streams for output and push the text thru all the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * rewriters, switching the two streams around when something is
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * actually rewritten. Stream1 remains unchanged.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * If rewritten, write it back to disk.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(1, "Writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(1, "Would have modified file \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol, g_fDiffIgnoreLeadingWS,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars, g_pStdOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(3, "not text file: \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Tries to correct RTDIRENTRY_UNKNOWN.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns Corrected type.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszPath The path to the object in question.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Recurse into a sub-directory and process all the files and directories.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszBuf Path buffer containing the directory path on
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * entry. This ends with a dot. This is passed
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * along when recusing in order to save stack space
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * and avoid needless copying.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param cchDir Length of our path in pszbuf.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pEntry Directory entry buffer. This is also passed
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * along when recursing to save stack space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iRecursion The recursion depth. This is used to restrict
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * the recursions.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry, unsigned iRecursion)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Make sure we stop somewhere.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("recursion too deep: %d\n", iRecursion);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Try open and read the directory.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Read the next entry. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Skip '.' and '..'. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Enter it into the buffer so we've got a full name to work
a299266cddc1ae14d76d725a660bb278816bc151vboxsync with when needed. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Skipping too long entry: %s", pEntry->szName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Figure the type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Process the file or directory, skip the rest. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Append the dot for the benefit of the pattern matching. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || RTStrSimplePatternMultiMatch(g_pszDirFilter, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || ( !RTStrSimplePatternMultiMatch(g_pszDirFilterOut, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && !RTStrSimplePatternMultiMatch(g_pszDirFilterOut, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, iRecursion + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Process a directory tree.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszDir The directory to start with.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Setup the recursion.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get going.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmProcessDirTreeRecursion(szBuf, strlen(szBuf), &Entry, 0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Processes a file or directory specified as an command line argument.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszSomething What we found in the commad line arguments.
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmProcessSomething(const char *pszSomething)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pszBasename = RTPathFilename(pszSomething);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || RTStrSimplePatternMultiMatch(g_pszFileFilter, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || !RTStrSimplePatternMultiMatch(g_pszFileFilterOut, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmProcessFile(pszSomething, pszBasename, cchBasename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * If it's not a file, try treat it as a directory tree.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Parse arguments and process input in order (because this is the only
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * thing that works at the moment).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--strip-trailing-blanks", 'b', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--no-strip-trailing-blanks", 'B', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--no-convert-tabs", 'C', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, 0 /*fFlags*/);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "Usage: %s [options] <files & dirs>\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTPrintf("%s: Warning! This program will make changes to your source files and\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "%s: there is a slight risk that bugs or a full disk may cause\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "%s: LOSS OF DATA. So, please make sure you have checked in\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "%s: all your changes already. If you didn't, then don't blam\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "%s: anyone for not warning you!\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "%s: Press any key to continue...\n",