scm.cpp revision 1828a998092cf8316a49329fdbcb1449b511b66a
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* $Id$ */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** @file
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * IPRT Testcase / Tool - Source Code Massager.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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 *
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 *
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 *
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
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*******************************************************************************
a299266cddc1ae14d76d725a660bb278816bc151vboxsync* Header Files *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync*******************************************************************************/
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/assert.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/ctype.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/dir.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/file.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/err.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/getopt.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/initterm.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/mem.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/message.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/param.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/path.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/stream.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#include <iprt/string.h>
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*******************************************************************************
a299266cddc1ae14d76d725a660bb278816bc151vboxsync* Internal Functions *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync*******************************************************************************/
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** End of line marker type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef enum SCMEOL
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL_NONE = 0,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL_LF = 1,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL_CRLF = 2
a299266cddc1ae14d76d725a660bb278816bc151vboxsync} SCMEOL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to an end of line marker type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef SCMEOL *PSCMEOL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Line record.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef struct SCMSTREAMLINE
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The offset of the line. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t off;
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 size_t cch;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The end of line marker type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync} SCMSTREAMLINE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a line record. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef SCMSTREAMLINE *PSCMSTREAMLINE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Source code massager stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef struct SCMSTREAM
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Pointer to the file memory. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync char *pch;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current stream position. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current stream size. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The size of the memory pb points to. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cbAllocated;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Line records. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PSCMSTREAMLINE paLines;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current line. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The current stream size given in lines. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cLines;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** The sizeof the the memory backing paLines. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cLinesAllocated;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Set if write-only, clear if read-only. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fWriteOrRead;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Set if the memory pb points to is from RTFileReadAll. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fFileMemory;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Set if fully broken into lines. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fFullyLineated;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Stream status code (IPRT). */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync} SCMSTREAM;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a SCM stream. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef SCMSTREAM *PSCMSTREAM;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a const SCM stream. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef SCMSTREAM const *PCSCMSTREAM;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * A rewriter.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * This works like a stream editor, reading @a pIn, modifying it and writing it
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * to @a pOut.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if any changes were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef bool (*PFNSCMREWRITER)(PSCMSTREAM pIn, PSCMSTREAM pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Configuration entry.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef struct SCMCFGENTRY
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Number of rewriters. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cRewriters;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Pointer to an array of rewriters. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PFNSCMREWRITER const *papfnRewriter;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** File pattern (simple). */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pszFilePattern;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync} SCMCFGENTRY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef SCMCFGENTRY *PSCMCFGENTRY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef SCMCFGENTRY const *PCSCMCFGENTRY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Diff state.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef struct SCMDIFFSTATE
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cDiffs;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pszFilename;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PSCMSTREAM pLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PSCMSTREAM pRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to ignore end of line markers when diffing. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fIgnoreEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to ignore trailing whitespace. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fIgnoreTrailingWhite;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to ignore leading whitespace. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fIgnoreLeadingWhite;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Whether to print special characters in human readable form or not. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fSpecialChars;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /** Where to push the diff. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PRTSTREAM pDiff;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync} SCMDIFFSTATE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/** Pointer to a diff state. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsynctypedef SCMDIFFSTATE *PSCMDIFFSTATE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
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
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/*******************************************************************************
a299266cddc1ae14d76d725a660bb278816bc151vboxsync* Global Variables *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync*******************************************************************************/
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char g_szProgName[] = "scm";
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *g_pszChangedSuff = "";
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 unsigned g_cchTab = 8;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char g_szTabSpaces[16+1] = " ";
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 int g_iVerbosity = 2;//99; //0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *g_pszFileFilter = "*";
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *g_pszFileFilterOut = "*.exe|*.com|20*-*-*.log";
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *g_pszDirFilter = NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *g_pszDirFilterOut = \
a299266cddc1ae14d76d725a660bb278816bc151vboxsync // generic
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ".svn|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "CVS|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync // root
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "out|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "tools|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "webtools|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "kBuild|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "debian|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "SlickEdit|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync // src
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/libs/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/apps/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Frontends/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Additions/x11/x11include/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Additions/WINNT/Graphics/Wine/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Additions/common/crOpenGL/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/HostServices/SharedOpenGL/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/GuestHost/OpenGL/*/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Devices/PC/Etherboot-src/*/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Devices/Network/lwip/.|"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "*/src/VBox/Devices/Storage/VBoxHDDFormats/StorageCraft/*/."
a299266cddc1ae14d76d725a660bb278816bc151vboxsync;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_Makefile_kup
a299266cddc1ae14d76d725a660bb278816bc151vboxsync};
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_ForceNativeEol,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_StripTrailingBlanks,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_AdjustTrailingLines,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_Makefile_kmk
a299266cddc1ae14d76d725a660bb278816bc151vboxsync};
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_ForceNativeEol,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_ExpandTabs,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_StripTrailingBlanks,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_AdjustTrailingLines,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_C_and_CPP
a299266cddc1ae14d76d725a660bb278816bc151vboxsync};
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_ForceLF,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_ExpandTabs,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_StripTrailingBlanks
a299266cddc1ae14d76d725a660bb278816bc151vboxsync};
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_ForceLF,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_ExpandTabs,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rewrite_StripTrailingBlanks
a299266cddc1ae14d76d725a660bb278816bc151vboxsync};
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic SCMCFGENTRY const g_aConfigs[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
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};
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- memory streams -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Initializes the stream structure.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream structure.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param fWriteOrRead The value of the fWriteOrRead stream member.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic void scmStreamInitInternal(PSCMSTREAM pStream, bool fWriteOrRead)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch = NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cb = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cbAllocated = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines = NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLines = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLinesAllocated = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fWriteOrRead = fWriteOrRead;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fFileMemory = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fFullyLineated = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->rc = VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Initialize an input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream to initialize.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFilename The file to take the stream content from.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamInitForReading(PSCMSTREAM pStream, const char *pszFilename)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmStreamInitInternal(pStream, false /*fWriteOrRead*/);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync void *pvFile;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cbFile;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = pStream->rc = RTFileReadAll(pszFilename, &pvFile, &cbFile);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_SUCCESS(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch = (char *)pvFile;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cb = cbFile;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cbAllocated = cbFile;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fFileMemory = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Initialize an output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream to initialize.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pRelatedStream Pointer to a related stream. NULL is fine.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamInitForWriting(PSCMSTREAM pStream, PCSCMSTREAM pRelatedStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmStreamInitInternal(pStream, true /*fWriteOrRead*/);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* allocate stuff */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cbEstimate = pRelatedStream
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ? pRelatedStream->cb + pRelatedStream->cb / 10
a299266cddc1ae14d76d725a660bb278816bc151vboxsync : _64K;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cbEstimate = RT_ALIGN(cbEstimate, _4K);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch = (char *)RTMemAlloc(cbEstimate);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pStream->pch)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cLinesEstimate = pRelatedStream && pRelatedStream->fFullyLineated
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ? pRelatedStream->cLines + pRelatedStream->cLines / 10
a299266cddc1ae14d76d725a660bb278816bc151vboxsync : cbEstimate / 24;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLinesEstimate = RT_ALIGN(cLinesEstimate, 512);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines = (PSCMSTREAMLINE)RTMemAlloc(cLinesEstimate * sizeof(SCMSTREAMLINE));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pStream->paLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[0].off = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[0].cch = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[0].enmEol = SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cbAllocated = cbEstimate;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLinesAllocated = cLinesEstimate;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMemFree(pStream->pch);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch = NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc = VERR_NO_MEMORY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Frees the resources associated with the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Nothing is happens to whatever the stream was initialized from or dumped to.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream to delete.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncvoid ScmStreamDelete(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pStream->pch)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pStream->fFileMemory)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMemFree(pStream->pch);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch = NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cbAllocated = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pStream->paLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMemFree(pStream->paLines);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines = NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLinesAllocated = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the stream status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamGetStatus(PCSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Grows the buffer of a write stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * with.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmStreamGrowBuffer(PSCMSTREAM pStream, size_t cbAppending)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cbAllocated = pStream->cbAllocated;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cbAllocated += RT_MAX(0x1000 + cbAppending, cbAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cbAllocated = RT_ALIGN(cbAllocated, 0x1000);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync void *pvNew;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pStream->fFileMemory)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pvNew = RTMemRealloc(pStream->pch, cbAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pvNew)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc = VERR_NO_MEMORY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pvNew = RTMemDupEx(pStream->pch, pStream->off, cbAllocated - pStream->off);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pvNew)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc = VERR_NO_MEMORY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fFileMemory = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch = (char *)pvNew;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cbAllocated = cbAllocated;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Grows the line array of a stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iMinLine Minimum line number.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmStreamGrowLines(PSCMSTREAM pStream, size_t iMinLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cLinesAllocated = pStream->cLinesAllocated;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLinesAllocated += RT_MAX(512 + iMinLine, cLinesAllocated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLinesAllocated = RT_ALIGN(cLinesAllocated, 512);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync void *pvNew = RTMemRealloc(pStream->paLines, cLinesAllocated * sizeof(SCMSTREAMLINE));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pvNew)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc = VERR_NO_MEMORY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines = (PSCMSTREAMLINE)pvNew;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLinesAllocated = cLinesAllocated;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewinds the stream and sets the mode to read.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncvoid ScmStreamRewindForReading(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fWriteOrRead = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->rc = VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewinds the stream and sets the mode to write.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncvoid ScmStreamRewindForWriting(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLines = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fWriteOrRead = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fFullyLineated = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->rc = VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Checks if it's a text stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Not 100% proof.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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 */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncbool ScmStreamIsText(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (memchr(pStream->pch, '\0', pStream->cb))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pStream->cb)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Performs an integrity check of the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamCheckItegrity(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Perform sanity checks.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const cbFile = pStream->cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iLine = 0; iLine < pStream->cLines; iLine++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
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 switch (pStream->paLines[iLine].enmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMEOL_LF:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->pch[offEol] == '\n', VERR_INTERNAL_ERROR_3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMEOL_CRLF:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->pch[offEol] == '\r', VERR_INTERNAL_ERROR_3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->pch[offEol + 1] == '\n', VERR_INTERNAL_ERROR_3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMEOL_NONE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_4);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync default:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_5);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Writes the stream to a file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFilenameFmt The filename format string.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param ... Format arguments.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamWriteToFile(PSCMSTREAM pStream, const char *pszFilenameFmt, ...)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#ifdef RT_STRICT
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Check that what we're going to write makes sense first.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamCheckItegrity(pStream);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#endif
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Do the actual writing.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTFILE hFile;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync va_list va;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync va_start(va, pszFilenameFmt);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTFileOpenV(&hFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE, pszFilenameFmt, va);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_SUCCESS(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTFileWrite(hFile, pStream->pch, pStream->cb, NULL);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTFileClose(hFile);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Worker for ScmStreamGetLine that builds the line number index while parsing
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *scmStreamGetLineInternal(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(!pStream->fWriteOrRead, NULL);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pStream->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t off = pStream->off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cb = pStream->cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(off >= cb))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->fFullyLineated = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t iLine = pStream->iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(iLine >= pStream->cLinesAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowLines(pStream, iLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].off = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cb -= off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRet = &pStream->pch[off];
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pch = (const char *)memchr(pchRet, '\n', cb);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_LIKELY(pch))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cb = pch - pchRet;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = off + cb + 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( cb < 1
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || pch[-1] != '\r')
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_LF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_CRLF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cb--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = off + cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *pcchLine = cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLines = pStream->iLine = ++iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pchRet;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Internal worker that lineates a stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream. Caller must check that it is in
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * read mode.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmStreamLineate(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Save the stream position. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const offSaved = pStream->off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const iLineSaved = pStream->iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Get each line. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (scmStreamGetLineInternal(pStream, &cchLine, &enmEol))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* nothing */;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync Assert(RT_FAILURE(pStream->rc) || pStream->fFullyLineated);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Restore the position */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = offSaved;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = iLineSaved;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the current stream position as a line number.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns The current line (0-based).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncsize_t ScmStreamTellLine(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Gets the number of lines in the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns The number of lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncsize_t ScmStreamCountLines(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pStream->fFullyLineated)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmStreamLineate(pStream);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->cLines;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Seeks to a given line in the tream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamSeekByLine(PSCMSTREAM pStream, size_t iLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
1828a998092cf8316a49329fdbcb1449b511b66avboxsync AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pStream->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Must be fully lineated of course. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(!pStream->fFullyLineated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamLineate(pStream);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Ok, do the job. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (iLine < pStream->cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = pStream->paLines[iLine].off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = pStream->cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = pStream->cLines;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get a numbered line from the stream (changes the position).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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 *
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 * occured.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(!pStream->fWriteOrRead, NULL);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pStream->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Make sure it's fully lineated so we can use the index. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(!pStream->fFullyLineated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamLineate(pStream);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* End of stream? */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(iLine >= pStream->cLines))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = pStream->cb;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = pStream->cLines;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return NULL;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Get the data. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRet = &pStream->pch[pStream->paLines[iLine].off];
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *pcchLine = pStream->paLines[iLine].cch;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *penmEol = pStream->paLines[iLine].enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* update the stream position. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = iLine + 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pchRet;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get a line from the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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 *
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 * occured.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic const char *ScmStreamGetLine(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pStream->fFullyLineated)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmStreamGetLineInternal(pStream, pcchLine, penmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return ScmStreamGetLineByNo(pStream, pStream->iLine, pcchLine, penmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Checks if the given line is empty or full of white space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool ScmStreamIsWhiteLine(PSCMSTREAM pStream, size_t iLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchLine && RT_C_IS_SPACE(*pchLine))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLine++, cchLine--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return cchLine == 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Try figure out the end of line style of the give stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns Most likely end of line style.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pStream The stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncSCMEOL ScmStreamGetEol(PSCMSTREAM pStream)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pStream->cLines > 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = pStream->paLines[0].enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (pStream->cb == 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLF = (const char *)memchr(pStream->pch, '\n', pStream->cb);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pchLF && pchLF != pStream->pch && pchLF[-1] == '\r')
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = SCMEOL_CRLF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = SCMEOL_LF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (enmEol == SCMEOL_NONE)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = SCMEOL_CRLF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = SCMEOL_LF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#endif
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the end of line indicator type for a line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncSCMEOL ScmStreamGetEolByLine(PSCMSTREAM pStream, size_t iLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (iLine < pStream->cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = pStream->paLines[iLine].enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = SCMEOL_CRLF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = SCMEOL_LF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#endif
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Appends a line to the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamPutLine(PSCMSTREAM pStream, const char *pchLine, size_t cchLine, SCMEOL enmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pStream->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Make sure the previous line has a new-line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t off = pStream->off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t iLine = pStream->iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY( iLine != 0
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
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 {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol + enmEol2);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (enmEol2 == SCMEOL_LF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch[off++] = '\n';
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch[off++] = '\r';
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch[off++] = '\n';
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine - 1].enmEol = enmEol2;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].off = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cb = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Ensure we've got sufficient buffer space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(off + cchLine + enmEol > pStream->cbAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Add a line record.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowLines(pStream, iLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
1828a998092cf8316a49329fdbcb1449b511b66avboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off + cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iLine++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLines = iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copy the line
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync memcpy(&pStream->pch[off], pchLine, cchLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync off += cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (enmEol == SCMEOL_LF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch[off++] = '\n';
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (enmEol == SCMEOL_CRLF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch[off++] = '\r';
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch[off++] = '\n';
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cb = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Start a new line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].off = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Writes to the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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 */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamWrite(PSCMSTREAM pStream, const char *pchBuf, size_t cchBuf)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pStream->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Ensure we've got sufficient buffer space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t off = pStream->off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(off + cchBuf > pStream->cbAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowBuffer(pStream, cchBuf);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t iLine = pStream->iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY( iLine > 0
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iLine--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLines = iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Deal with lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLF = (const char *)memchr(pchBuf, '\n', cchBuf);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchLF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch += cchBuf;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = pchBuf;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (;;)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = scmStreamGrowLines(pStream, iLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iLine = pStream->iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1828a998092cf8316a49329fdbcb1449b511b66avboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine = pchLF - pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( cchLine
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ? pchLF[-1] != '\r'
a299266cddc1ae14d76d725a660bb278816bc151vboxsync : !pStream->paLines[iLine].cch
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || pStream->pch[pStream->paLines[iLine].off + pStream->paLines[iLine].cch - 1] != '\r')
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = SCMEOL_LF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = SCMEOL_CRLF;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cchLine--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch += cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iLine++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t offBuf = pchLF + 1 - pchBuf;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].off = off + offBuf;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].enmEol = SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLeft = cchBuf - offBuf;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLF = (const char *)memchr(pchLF + 1, '\n', cchLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchLF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[iLine].cch = cchLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->iLine = iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cLines = iLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copy the data and update position and size.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync memcpy(&pStream->pch[off], pchBuf, cchBuf);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync off += cchBuf;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->cb = off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Write a character to the stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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 */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamPutCh(PSCMSTREAM pStream, char ch)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pStream->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pStream->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Only deal with the simple cases here, use ScmStreamWrite for the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * annyoing stuff.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t off = pStream->off;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( ch == '\n'
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || RT_UNLIKELY(off + 1 > pStream->cbAllocated))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return ScmStreamWrite(pStream, &ch, 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Just append it.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->pch[off] = ch;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->off = off + 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pStream->paLines[pStream->iLine].cch++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * The stream positions will be used and changed in both streams.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint ScmStreamCopyLines(PSCMSTREAM pDst, PSCMSTREAM pSrc, size_t cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(pDst->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pDst->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pDst->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReturn(!pSrc->fWriteOrRead, VERR_ACCESS_DENIED);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(pSrc->rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pSrc->rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cLines-- > 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLine(pSrc, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pDst->rc = (RT_FAILURE(pSrc->rc) ? pSrc->rc : VERR_EOF);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = ScmStreamPutLine(pDst, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- diff -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Prints a range of lines with a prefix.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic void scmDiffPrintLines(PSCMDIFFSTATE pState, char chPrefix, PSCMSTREAM pStream, size_t iLine, size_t cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cLines-- > 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPutCh(pState->pDiff, chPrefix);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pchLine && cchLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmWrite(pState->pDiff, pchLine, cchLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pState->fSpecialChars)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPutCh(pState->pDiff, '\n');
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (enmEol == SCMEOL_LF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPutStr(pState->pDiff, "[LF]\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (enmEol == SCMEOL_CRLF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPutStr(pState->pDiff, "[CRLF]\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPutStr(pState->pDiff, "[NONE]\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iLine++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Reports a difference and propells the streams to the lines following the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * resync.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic size_t scmDiffReport(PSCMDIFFSTATE pState, size_t cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t iLeft, size_t cLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t iRight, size_t cRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Adjust the input.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cLeft == ~(size_t)0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t c = ScmStreamCountLines(pState->pLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (c >= iLeft)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLeft = c - iLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iLeft = c;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLeft = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cRight == ~(size_t)0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t c = ScmStreamCountLines(pState->pRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (c >= iRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cRight = c - iRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iRight = c;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cRight = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Print header if it's the first difference
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pState->cDiffs)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "diff %s %s\n", pState->pszFilename, pState->pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Emit the change description.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync char ch = cLeft == 0
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ? 'a'
a299266cddc1ae14d76d725a660bb278816bc151vboxsync : cRight == 0
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ? 'd'
a299266cddc1ae14d76d725a660bb278816bc151vboxsync : 'c';
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cLeft > 1 && cRight > 1)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu,%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1, iRight + cRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (cLeft > 1)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (cRight > 1)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu%c%zu,%zu\n", iLeft + 1, ch, iRight + 1, iRight + cRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "%zu%c%zu\n", iLeft + 1, ch, iRight + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * And the lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cLeft)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffPrintLines(pState, '<', pState->pLeft, iLeft, cLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cLeft && cRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmPrintf(pState->pDiff, "---\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffPrintLines(pState, '>', pState->pRight, iRight, cRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Reposition the streams (safely ignores return value).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamSeekByLine(pState->pLeft, iLeft + cLeft + cMatches);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamSeekByLine(pState->pRight, iRight + cRight + cMatches);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pState->cDiffs++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return pState->cDiffs;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Helper for scmDiffCompare that takes care of trailing spaces and stuff
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * like that.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool scmDiffCompareSlow(PSCMDIFFSTATE pState,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pState->fIgnoreTrailingWhite)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchLeft > 0 && RT_C_IS_SPACE(pchLeft[cchLeft - 1]))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cchLeft--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchRight > 0 && RT_C_IS_SPACE(pchRight[cchRight - 1]))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cchRight--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pState->fIgnoreLeadingWhite)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchLeft > 0 && RT_C_IS_SPACE(*pchLeft))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLeft++, cchLeft--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchRight > 0 && RT_C_IS_SPACE(*pchRight))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchRight++, cchRight--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( cchLeft != cchRight
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || memcmp(pchLeft, pchRight, cchLeft))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compare two lines.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if the are equal, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncDECLINLINE(bool) scmDiffCompare(PSCMDIFFSTATE pState,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( cchLeft != cchRight
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || memcmp(pchLeft, pchRight, cchLeft))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( pState->fIgnoreTrailingWhite
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || pState->fIgnoreTrailingWhite)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmDiffCompareSlow(pState,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLeft, cchLeft, enmEolLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchRight, cchRight, enmEolRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compares two sets of lines from the two files.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool scmDiffCompareLines(PSCMDIFFSTATE pState, size_t iLeft, size_t iRight, size_t cLines)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iLine = 0; iLine < cLines; iLine++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEolLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iLeft + iLine, &cchLeft, &enmEolLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEolRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iRight + iLine, &cchRight, &enmEolRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Resynchronize the two streams and reports the difference.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Upon return, the streams will be positioned after the block of @a cMatches
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * lines where it resynchronized them.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic size_t scmDiffSynchronize(PSCMDIFFSTATE pState, size_t cMatches)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const iStartLeft = ScmStreamTellLine(pState->pLeft) - 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const iStartRight = ScmStreamTellLine(pState->pRight) - 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync Assert(cMatches > 0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compare each new line from each of the streams will all the preceding
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * ones, including iStartLeft/Right.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iRange = 1; ; iRange++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the next line in the left stream and compare it against all the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * preceding lines on the right side.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iRange, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iRight = cMatches - 1; iRight < iRange; iRight++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEolRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRight,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync &cchRight, &enmEolRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( scmDiffCompare(pState, pchLine, cchLine, enmEol, pchRight, cchRight, enmEolRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && scmDiffCompareLines(pState,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartLeft + iRange + 1 - cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartRight + iRight + 1 - cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cMatches - 1)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync )
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmDiffReport(pState, cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartLeft, iRange + 1 - cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartRight, iRight + 1 - cMatches);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get the next line in the right stream and compare it against all the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * lines on the right side.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLine = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRange, &cchLine, &enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchLine)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iLeft = cMatches - 1; iLeft <= iRange; iLeft++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEolLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iLeft,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync &cchLeft, &enmEolLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchLine, cchLine, enmEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && scmDiffCompareLines(pState,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartLeft + iLeft + 1 - cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartRight + iRange + 1 - cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cMatches - 1)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync )
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmDiffReport(pState, cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartLeft, iLeft + 1 - cMatches,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync iStartRight, iRange + 1 - cMatches);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Creates a diff of the changes between the streams @a pLeft and @a pRight.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * This currently only implements the simplest diff format, so no contexts.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Also, note that we won't detect differences in the final newline of the
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * streams.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncsize_t ScmDiffStreams(const char *pszFilename, PSCMSTREAM pLeft, PSCMSTREAM pRight, bool fIgnoreEol,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fIgnoreLeadingWhite, bool fIgnoreTrailingWhite, bool fSpecialChars, PRTSTREAM pDiff)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#ifdef RT_STRICT
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamCheckItegrity(pLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamCheckItegrity(pRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#endif
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Set up the diff state.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMDIFFSTATE State;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.cDiffs = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.pszFilename = pszFilename;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.pLeft = pLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.pRight = pRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.fIgnoreEol = fIgnoreEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.fIgnoreLeadingWhite = fIgnoreLeadingWhite;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.fIgnoreTrailingWhite = fIgnoreTrailingWhite;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.fSpecialChars = fSpecialChars;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync State.pDiff = pDiff;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Compare them line by line.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamRewindForReading(pLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamRewindForReading(pRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (;;)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEolLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLeft;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchLeft = ScmStreamGetLine(pLeft, &cchLeft, &enmEolLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEolRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchRight;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchRight = ScmStreamGetLine(pRight, &cchRight, &enmEolRight);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchLeft || !pchRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!scmDiffCompare(&State, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffSynchronize(&State, 3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Deal with any remaining differences.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pchLeft)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffReport(&State, 0, ScmStreamTellLine(pLeft) - 1, ~(size_t)0, ScmStreamTellLine(pRight), 0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (pchRight)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync scmDiffReport(&State, 0, ScmStreamTellLine(pLeft), 0, ScmStreamTellLine(pRight) - 1, ~(size_t)0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Report any errors.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(ScmStreamGetStatus(pLeft)))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Left diff stream error: %Rrc\n", ScmStreamGetStatus(pLeft));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(ScmStreamGetStatus(pRight)))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Right diff stream error: %Rrc\n", ScmStreamGetStatus(pRight));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return State.cDiffs;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- misc -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Prints a verbose message if the level is high enough.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param iLevel The required verbosity level.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszFormat The message format string.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param ... Format arguments.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic void ScmVerbose(int iLevel, const char *pszFormat, ...)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (iLevel <= g_iVerbosity)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTPrintf("%s: info: ", g_szProgName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync va_list va;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync va_start(va, pszFormat);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTPrintfV(pszFormat, va);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync va_end(va);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- rewriters -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Strip trailing blanks (space & tab).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns True if modified, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_StripTrailingBlanks(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!g_fStripTrailingBlanks)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( cchLine == 0
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || !RT_C_IS_BLANK(pchLine[cchLine - 1]) )
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cchLine--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cchLine > 0 && RT_C_IS_BLANK(pchLine[cchLine - 1]))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cchLine--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync fModified = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (fModified)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, " * Stripped trailing blanks\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return fModified;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Expand tabs.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns True if modified, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ExpandTabs(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!g_fConvertTabs)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchTab)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t offTab = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchChunk = pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (;;)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchChunk = pchTab - pchChunk;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync offTab += cchChunk;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamWrite(pOut, pchChunk, cchChunk);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchToTab = g_cchTab - offTab % g_cchTab;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamWrite(pOut, g_szTabSpaces, cchToTab);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync offTab += cchToTab;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchChunk = pchTab + 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLeft = cchLine - (pchChunk - pchLine);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pchTab = (const char *)memchr(pchChunk, '\t', cchLeft);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pchTab)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamPutLine(pOut, pchChunk, cchLeft, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync fModified = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (fModified)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, " * Expanded tabs\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return fModified;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Worker for rewrite_ForceNativeEol, rewrite_ForceLF and rewrite_ForceCRLF.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceEol(PSCMSTREAM pIn, PSCMSTREAM pOut, SCMEOL enmDesiredEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!g_fConvertEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMEOL enmEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pchLine;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( enmEol != enmDesiredEol
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && enmEol != SCMEOL_NONE)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync fModified = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmEol = enmDesiredEol;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (fModified)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, " * Converted EOL markers\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return fModified;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Force native end of line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceNativeEol(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rewrite_ForceEol(pIn, pOut, SCMEOL_CRLF);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rewrite_ForceEol(pIn, pOut, SCMEOL_LF);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync#endif
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Force the stream to use LF as the end of line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceLF(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rewrite_ForceEol(pIn, pOut, SCMEOL_LF);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Force the stream to use CRLF as the end of line indicator.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_ForceCRLF(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rewrite_ForceEol(pIn, pOut, SCMEOL_CRLF);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Strip trailing blank lines and/or make sure there is exactly one blank line
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * at the end of the file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @remarks ASSUMES trailing white space has been removed already.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_AdjustTrailingLines(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!g_fStripTrailingLines && !g_fForceTrailingLine && !g_fForceFinalEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t const cLines = ScmStreamCountLines(pIn);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Empty files remains empty. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cLines <= 1)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Figure out if we need to adjust the number of lines or not. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cLinesNew = cLines;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( g_fStripTrailingLines
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ( cLinesNew > 1
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && ScmStreamIsWhiteLine(pIn, cLinesNew - 2))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLinesNew--;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( g_fForceTrailingLine
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && !ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cLinesNew++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fFixMissingEol = g_fForceFinalEol
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && ScmStreamGetEolByLine(pIn, cLinesNew - 1) == SCMEOL_NONE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( !fFixMissingEol
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && cLines == cLinesNew)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Copy the number of lines we've arrived at. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamRewindForReading(pIn);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cCopied = RT_MIN(cLinesNew, cLines);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamCopyLines(pOut, pIn, cCopied);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (cCopied != cLinesNew)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while (cCopied++ < cLinesNew)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamPutLine(pOut, "", 0, ScmStreamGetEol(pIn));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (fFixMissingEol)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (ScmStreamGetEol(pIn) == SCMEOL_LF)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamWrite(pOut, "\n", 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamWrite(pOut, "\r\n", 2);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, " * Adjusted trailing blank lines\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Makefile.kup are empty files, enforce this.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_Makefile_kup(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* These files should be zero bytes. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pIn->cb == 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, " * Truncated file to zero bytes\n");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewrite a kBuild makefile.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @todo
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Ideas for Makefile.kmk and Config.kmk:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - sort if1of/ifn1of sets.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * - line continuation slashes should only be preceeded by one space.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_Makefile_kmk(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Rewrite a C/C++ source or header file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns true if modifications were made, false if not.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pIn The input stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pOut The output stream.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @todo
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic bool rewrite_C_and_CPP(PSCMSTREAM pIn, PSCMSTREAM pOut)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Processes a file.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Do the file level filtering.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( g_pszFileFilter
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && !RTStrSimplePatternMultiMatch(g_pszFileFilter, RTSTR_MAX, pszBasename, cchBasename, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(4, "file filter mismatch: \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( g_pszFileFilterOut
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && RTStrSimplePatternMultiMatch(g_pszFileFilterOut, RTSTR_MAX, pszBasename, cchBasename, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(4, "file filter out: \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Try find a matching rewrite config for this filename.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PCSCMCFGENTRY pCfg = NULL;
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 {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pCfg = &g_aConfigs[iCfg];
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pCfg)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(3, "No rewriters configured for \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(3, "\"%s\" matched \"%s\"\n", pszFilename, pCfg->pszFilePattern);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Create an input stream from the file and check that it's text.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMSTREAM Stream1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = ScmStreamInitForReading(&Stream1, pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (ScmStreamIsText(&Stream1))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(1, "Processing '%s'...\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
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 */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMSTREAM Stream2;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamInitForWriting(&Stream2, &Stream1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_SUCCESS(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMSTREAM Stream3;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamInitForWriting(&Stream3, &Stream1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_SUCCESS(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fModified = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PSCMSTREAM pIn = &Stream1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PSCMSTREAM pOut = &Stream2;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync bool fRc = pCfg->papfnRewriter[iRw](pIn, pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (fRc)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PSCMSTREAM pTmp = pOut;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pOut = pIn == &Stream1 ? &Stream3 : pIn;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pIn = pTmp;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync fModified = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamRewindForReading(pIn);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamRewindForWriting(pOut);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * If rewritten, write it back to disk.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (fModified)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!g_fDryRun)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(1, "Writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
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 }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(2, "Unchanged \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamDelete(&Stream3);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamDelete(&Stream2);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmVerbose(3, "not text file: \"%s\"\n", pszFilename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ScmStreamDelete(&Stream1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Tries to correct RTDIRENTRY_UNKNOWN.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns Corrected type.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszPath The path to the object in question.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTFSOBJINFO Info;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return RTDIRENTRYTYPE_UNKNOWN;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return RTDIRENTRYTYPE_DIRECTORY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RTFS_IS_FILE(Info.Attr.fMode))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return RTDIRENTRYTYPE_FILE;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return RTDIRENTRYTYPE_UNKNOWN;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Recurse into a sub-directory and process all the files and directories.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
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.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry, unsigned iRecursion)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Make sure we stop somewhere.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (iRecursion > 128)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("recursion too deep: %d\n", iRecursion);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS; /* ignore */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Try open and read the directory.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync PRTDIR pDir;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (;;)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Read the next entry. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTDirRead(pDir, pEntry, NULL);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc) && rc != VERR_NO_MORE_FILES)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("RTDirRead -> %Rrc\n", rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Skip '.' and '..'. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( pEntry->szName[0] == '.'
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && ( pEntry->cbName == 1
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || ( pEntry->cbName == 2
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && pEntry->szName[1] == '.')))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync continue;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Enter it into the buffer so we've got a full name to work
a299266cddc1ae14d76d725a660bb278816bc151vboxsync with when needed. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pEntry->cbName + cchDir >= RTPATH_MAX)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Skipping too long entry: %s", pEntry->szName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync continue;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Figure the type. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTDIRENTRYTYPE enmType = pEntry->enmType;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (enmType == RTDIRENTRYTYPE_UNKNOWN)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enmType = scmFigureUnknownType(pszBuf);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Process the file or directory, skip the rest. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (enmType == RTDIRENTRYTYPE_FILE)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /* Append the dot for the benefit of the pattern matching. */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync continue;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( ( g_pszDirFilter == NULL
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || RTStrSimplePatternMultiMatch(g_pszDirFilter, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pEntry->szName, pEntry->cbName, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && ( g_pszDirFilterOut == NULL
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || ( !RTStrSimplePatternMultiMatch(g_pszDirFilterOut, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pEntry->szName, pEntry->cbName, NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && !RTStrSimplePatternMultiMatch(g_pszDirFilterOut, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pszBuf, cchSubDir, NULL)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync )
a299266cddc1ae14d76d725a660bb278816bc151vboxsync )
a299266cddc1ae14d76d725a660bb278816bc151vboxsync )
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, iRecursion + 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTDirClose(pDir);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return RT_SUCCESS(rc) ? 0 : 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Process a directory tree.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszDir The directory to start with.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmProcessDirTree(const char *pszDir)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Setup the recursion.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync char szBuf[RTPATH_MAX];
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = RTPathAbs(pszDir, szBuf, sizeof(szBuf));
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_SUCCESS(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTPathAppend(szBuf, sizeof(szBuf), ".");
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("RTPathAbs/Append: %Rrc\n", rc);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Get going.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTDIRENTRY Entry;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmProcessDirTreeRecursion(szBuf, strlen(szBuf), &Entry, 0);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync/**
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Processes a file or directory specified as an command line argument.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync *
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @returns IPRT status code
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * @param pszSomething What we found in the commad line arguments.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncstatic int scmProcessSomething(const char *pszSomething)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RTFileExists(pszSomething))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync const char *pszBasename = RTPathFilename(pszSomething);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!pszBasename)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VERR_IS_A_DIRECTORY;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cchBasename = strlen(pszBasename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( ( g_pszFileFilter
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || RTStrSimplePatternMultiMatch(g_pszFileFilter, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pszBasename, cchBasename, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync && ( g_pszFileFilterOut == NULL
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || !RTStrSimplePatternMultiMatch(g_pszFileFilterOut, RTSTR_MAX,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync pszBasename, cchBasename, NULL))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync )
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmProcessFile(pszSomething, pszBasename, cchBasename);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return VINF_SUCCESS;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * If it's not a file, try treat it as a directory tree.
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return scmProcessDirTree(pszSomething);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsyncint main(int argc, char **argv)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync{
a299266cddc1ae14d76d725a660bb278816bc151vboxsync int rc = RTR3Init();
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync /*
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * Parse arguments and process input in order (because this is the only
a299266cddc1ae14d76d725a660bb278816bc151vboxsync * thing that works at the moment).
a299266cddc1ae14d76d725a660bb278816bc151vboxsync */
a299266cddc1ae14d76d725a660bb278816bc151vboxsync enum SCMOPT
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_IGNORE_EOL = 10000,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_NO_IGNORE_EOL,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_IGNORE_SPACE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_NO_IGNORE_SPACE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_IGNORE_LEADING_SPACE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_SPECIAL_CHARS,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_DIFF_NO_SPECIAL_CHARS,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_STRIP_TRAILING_LINES,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_NO_STRIP_TRAILING_LINES,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_FORCE_FINAL_EOL,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_NO_FORCE_FINAL_EOL,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_FORCE_TRAILING_LINE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_NO_FORCE_TRAILING_LINE,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync SCMOPT_LAST
a299266cddc1ae14d76d725a660bb278816bc151vboxsync };
a299266cddc1ae14d76d725a660bb278816bc151vboxsync static const RTGETOPTDEF s_aOpts[] =
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--strip-trailing-blanks", 'b', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--no-strip-trailing-blanks", 'B', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--convert-tabs", 'c', 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 { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--real-run", 'D', 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 { "--convert-eol", 'e', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--no-convert-eol", 'E', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--file-filter", 'f', RTGETOPT_REQ_STRING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--help", 'h', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--tab-size", 't', RTGETOPT_REQ_UINT8 },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
a299266cddc1ae14d76d725a660bb278816bc151vboxsync };
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTGETOPTUNION ValueUnion;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTGETOPTSTATE GetOptState;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, 0 /*fFlags*/);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync AssertReleaseRCReturn(rc, 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync size_t cProcessed = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync switch (rc)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'b':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fStripTrailingBlanks = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'B':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fStripTrailingBlanks = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'c':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fConvertTabs = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'C':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fConvertTabs = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'd':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDryRun = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'D':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDryRun = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'e':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fConvertEol = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'E':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fConvertEol = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'f':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_pszFileFilter = ValueUnion.psz;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'h':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTPrintf("Source Code Massager\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "Usage: %s [options] <files & dirs>\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "Options:\n", g_szProgName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTPrintf(" %s\n", s_aOpts[i].pszLong);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTPrintf(" %s string\n", s_aOpts[i].pszLong);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync else
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTPrintf(" %s value\n", s_aOpts[i].pszLong);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return 1;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'q':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_iVerbosity = 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 't':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if ( ValueUnion.u8 < 1
a299266cddc1ae14d76d725a660bb278816bc151vboxsync || ValueUnion.u8 >= RT_ELEMENTS(g_szTabSpaces))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
a299266cddc1ae14d76d725a660bb278816bc151vboxsync ValueUnion.u8, RT_ELEMENTS(g_szTabSpaces) - 1);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return 2;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_cchTab = ValueUnion.u8;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case 'v':
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_iVerbosity++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_IGNORE_EOL:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreEol = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_NO_IGNORE_EOL:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreEol = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_IGNORE_SPACE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_NO_IGNORE_SPACE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreLeadingWS = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreLeadingWS = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreTrailingWS = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffIgnoreTrailingWS = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_SPECIAL_CHARS:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffSpecialChars = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_DIFF_NO_SPECIAL_CHARS:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fDiffSpecialChars = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_STRIP_TRAILING_LINES:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fStripTrailingLines = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_NO_STRIP_TRAILING_LINES:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fStripTrailingLines = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_FORCE_FINAL_EOL:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fForceFinalEol = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_NO_FORCE_FINAL_EOL:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fForceFinalEol = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_FORCE_TRAILING_LINE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fForceTrailingLine = true;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case SCMOPT_NO_FORCE_TRAILING_LINE:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_fForceTrailingLine = false;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync case VINF_GETOPT_NOT_OPTION:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!g_fDryRun)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (!cProcessed)
a299266cddc1ae14d76d725a660bb278816bc151vboxsync {
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:\n"
a299266cddc1ae14d76d725a660bb278816bc151vboxsync "%s: Press any key to continue...\n",
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
a299266cddc1ae14d76d725a660bb278816bc151vboxsync g_szProgName, g_szProgName);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync RTStrmGetCh(g_pStdIn);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync cProcessed++;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync rc = scmProcessSomething(ValueUnion.psz);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync if (RT_FAILURE(rc))
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return rc;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync break;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync default:
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return RTGetOptPrintError(rc, &ValueUnion);
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync }
a299266cddc1ae14d76d725a660bb278816bc151vboxsync
a299266cddc1ae14d76d725a660bb278816bc151vboxsync return 0;
a299266cddc1ae14d76d725a660bb278816bc151vboxsync}
a299266cddc1ae14d76d725a660bb278816bc151vboxsync