scm.cpp revision b2c35a7024c4c308b429c81b203b3bcbd44ee60c
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/* $Id$ */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @file
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * IPRT Testcase / Tool - Source Code Massager.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * available from http://www.virtualbox.org. This file is free software;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * you can redistribute it and/or modify it under the terms of the GNU
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * General Public License (GPL) as published by the Free Software
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * The contents of this file may alternatively be used under the terms
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * of the Common Development and Distribution License Version 1.0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * VirtualBox OSE distribution, in which case the provisions of the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * CDDL are applicable instead of those of the GPL.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * You may elect to license modified versions of this file under the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * terms and conditions of either the GPL or the CDDL or both.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * additional information or have any questions.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Header Files *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/assert.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/ctype.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/dir.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/file.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/err.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/getopt.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/initterm.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/mem.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/message.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/param.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/path.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/stream.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/string.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Defined Constants And Macros *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** The name of the settings files. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#define SCM_SETTINGS_FILENAME ".scm-settings"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Structures and Typedefs *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to const massager settings. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMSETTINGSBASE const *PCSCMSETTINGSBASE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** End of line marker type. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef enum SCMEOL
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL_NONE = 0,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL_LF = 1,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL_CRLF = 2
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMEOL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to an end of line marker type. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMEOL *PSCMEOL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Line record.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMSTREAMLINE
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The offset of the line. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The line length, excluding the LF character.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @todo This could be derived from the offset of the next line if that wasn't
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * so tedious. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cch;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The end of line marker type. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMSTREAMLINE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to a line record. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMSTREAMLINE *PSCMSTREAMLINE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Source code massager stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMSTREAM
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Pointer to the file memory. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pch;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The current stream position. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The current stream size. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The size of the memory pb points to. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cbAllocated;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Line records. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PSCMSTREAMLINE paLines;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The current line. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The current stream size given in lines. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cLines;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The sizeof the the memory backing paLines. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cLinesAllocated;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Set if write-only, clear if read-only. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fWriteOrRead;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Set if the memory pb points to is from RTFileReadAll. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fFileMemory;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Set if fully broken into lines. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fFullyLineated;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Stream status code (IPRT). */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMSTREAM;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to a SCM stream. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMSTREAM *PSCMSTREAM;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to a const SCM stream. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMSTREAM const *PCSCMSTREAM;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Rewriter state.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMRWSTATE
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The filename. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszFilename;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Set after the printing the first verbose message about a file under
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * rewrite. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fFirst;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMRWSTATE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to the rewriter state. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMRWSTATE *PSCMRWSTATE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * A rewriter.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * This works like a stream editor, reading @a pIn, modifying it and writing it
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * to @a pOut.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns true if any changes were made, false if not.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pIn The input stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pOut The output stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pSettings The settings.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef bool (*PFNSCMREWRITER)(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Configuration entry.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMCFGENTRY
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Number of rewriters. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cRewriters;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Pointer to an array of rewriters. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PFNSCMREWRITER const *papfnRewriter;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** File pattern (simple). */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszFilePattern;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMCFGENTRY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMCFGENTRY *PSCMCFGENTRY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMCFGENTRY const *PCSCMCFGENTRY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Diff state.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMDIFFSTATE
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cDiffs;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszFilename;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PSCMSTREAM pLeft;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PSCMSTREAM pRight;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Whether to ignore end of line markers when diffing. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fIgnoreEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Whether to ignore trailing whitespace. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fIgnoreTrailingWhite;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Whether to ignore leading whitespace. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fIgnoreLeadingWhite;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Whether to print special characters in human readable form or not. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fSpecialChars;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The tab size. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchTab;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Where to push the diff. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PRTSTREAM pDiff;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMDIFFSTATE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to a diff state. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMDIFFSTATE *PSCMDIFFSTATE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Source Code Massager Settings.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMSETTINGSBASE
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fConvertEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fConvertTabs;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fForceFinalEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fForceTrailingLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fStripTrailingBlanks;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fStripTrailingLines;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned cchTab;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Only consider files matcihng these patterns. This is only applied to the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * base names. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszFilterFiles;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Filter out files matching the following patterns. This is applied to base
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * names as well as the aboslute paths. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszFilterOutFiles;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Filter out directories matching the following patterns. This is applied
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * to base names as well as the aboslute paths. All absolute paths ends with a
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * slash and dot ("/."). */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszFilterOutDirs;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMSETTINGSBASE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to massager settings. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMSETTINGSBASE *PSCMSETTINGSBASE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Option identifiers.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @note The first chunk, down to SCMOPT_TAB_SIZE, are alternately set &
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * clear. So, the option setting a flag (boolean) will have an even
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * number and the one clearing it will have an odd number.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @note Down to SCMOPT_LAST_SETTINGS corresponds exactly to SCMSETTINGSBASE.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef enum SCMOPT
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_CONVERT_EOL = 10000,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_NO_CONVERT_EOL,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_CONVERT_TABS,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_NO_CONVERT_TABS,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_FORCE_FINAL_EOL,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_NO_FORCE_FINAL_EOL,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_FORCE_TRAILING_LINE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_NO_FORCE_TRAILING_LINE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_STRIP_TRAILING_BLANKS,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_NO_STRIP_TRAILING_BLANKS,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_STRIP_TRAILING_LINES,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_NO_STRIP_TRAILING_LINES,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_TAB_SIZE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_FILTER_OUT_DIRS,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_FILTER_FILES,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_FILTER_OUT_FILES,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_LAST_SETTINGS = SCMOPT_FILTER_OUT_FILES,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync //
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_IGNORE_EOL,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_NO_IGNORE_EOL,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_IGNORE_SPACE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_NO_IGNORE_SPACE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_IGNORE_LEADING_SPACE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_SPECIAL_CHARS,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_DIFF_NO_SPECIAL_CHARS,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMOPT_END
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMOPT;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * File/dir pattern + options.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMPATRNOPTPAIR
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszPattern;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszOptions;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMPATRNOPTPAIR;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to a pattern + option pair. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMPATRNOPTPAIR *PSCMPATRNOPTPAIR;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to a settings set. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMSETTINGS *PSCMSETTINGS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Settings set.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * This structure is constructed from the command line arguments or any
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * .scm-settings file found in a directory we recurse into. When recusing in
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * and out of a directory, we push and pop a settings set for it.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * The .scm-settings file has two kinds of setttings, first there are the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * unqualified base settings and then there are the settings which applies to a
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * set of files or directories. The former are lines with command line options.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * For the latter, the options are preceeded by a string pattern and a colon.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * The pattern specifies which files (and/or directories) the options applies
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * to.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * We parse the base options into the Base member and put the others into the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * paPairs array.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef struct SCMSETTINGS
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Pointer to the setting file below us in the stack. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PSCMSETTINGS pDown;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** Pointer to the setting file above us in the stack. */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync PSCMSETTINGS pUp;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync /** File/dir patterns and their options. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PSCMPATRNOPTPAIR paPairs;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The number of entires in paPairs. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint32_t cPairs;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** The base settings that was read out of the file. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMSETTINGSBASE Base;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync} SCMSETTINGS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Pointer to a const settings set. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsynctypedef SCMSETTINGS const *PCSCMSETTINGS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Internal Functions *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Global Variables *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic const char g_szProgName[] = "scm";
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic const char *g_pszChangedSuff = "";
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic const char g_szTabSpaces[16+1] = " ";
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool g_fDryRun = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool g_fDiffSpecialChars = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool g_fDiffIgnoreEol = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool g_fDiffIgnoreLeadingWS = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool g_fDiffIgnoreTrailingWS = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int g_iVerbosity = 2;//99; //0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** The global settings. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic SCMSETTINGSBASE const g_Defaults =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .fConvertEol = */ true,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .fConvertTabs = */ true,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .fForceFinalEol = */ true,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .fForceTrailingLine = */ false,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .fStripTrailingBlanks = */ true,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .fStripTrailingLines = */ true,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .cchTab = */ 8,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .pszFilterFiles = */ (char *)"",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .pszFilterOutFiles = */ (char *)"*.exe|*.com|20*-*-*.log",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* .pszFilterOutDirs = */ (char *)".svn|.hg|.git|CVS",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Option definitions for the base settings. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic RTGETOPTDEF g_aScmOpts[] =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--convert-eol", SCMOPT_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--no-convert-eol", SCMOPT_NO_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--convert-tabs", SCMOPT_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--no-convert-tabs", SCMOPT_NO_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--strip-trailing-blanks", SCMOPT_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--no-strip-trailing-blanks", SCMOPT_NO_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--tab-size", SCMOPT_TAB_SIZE, RTGETOPT_REQ_UINT8 },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--filter-out-dirs", SCMOPT_FILTER_OUT_DIRS, RTGETOPT_REQ_STRING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--filter-files", SCMOPT_FILTER_FILES, RTGETOPT_REQ_STRING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { "--filter-out-files", SCMOPT_FILTER_OUT_FILES, RTGETOPT_REQ_STRING },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** Consider files matching the following patterns (base names only). */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic const char *g_pszFileFilter = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_Makefile_kup
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_ForceNativeEol,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_StripTrailingBlanks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_AdjustTrailingLines,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_Makefile_kmk
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_ForceNativeEol,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_ExpandTabs,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_StripTrailingBlanks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_AdjustTrailingLines,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_C_and_CPP
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_ForceLF,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_ExpandTabs,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_StripTrailingBlanks
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_ForceCRLF,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_ExpandTabs,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rewrite_StripTrailingBlanks
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic SCMCFGENTRY const g_aConfigs[] =
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { RT_ELEMENTS(g_aRewritersFor_Makefile_kup), &g_aRewritersFor_Makefile_kup[0], "Makefile.kup" },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { RT_ELEMENTS(g_aRewritersFor_Makefile_kmk), &g_aRewritersFor_Makefile_kmk[0], "Makefile.kmk" },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { RT_ELEMENTS(g_aRewritersFor_C_and_CPP), &g_aRewritersFor_C_and_CPP[0], "*.c|*.h|*.cpp|*.hpp|*.C|*.CPP|*.cxx|*.cc" },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { RT_ELEMENTS(g_aRewritersFor_ShellScripts), &g_aRewritersFor_ShellScripts[0], "*.sh|configure" },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync { RT_ELEMENTS(g_aRewritersFor_BatchFiles), &g_aRewritersFor_BatchFiles[0], "*.bat|*.cmd|*.btm|*.vbs|*.ps1" },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/* -=-=-=-=-=- memory streams -=-=-=-=-=- */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Initializes the stream structure.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream structure.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param fWriteOrRead The value of the fWriteOrRead stream member.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void scmStreamInitInternal(PSCMSTREAM pStream, bool fWriteOrRead)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cb = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cbAllocated = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLines = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLinesAllocated = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fWriteOrRead = fWriteOrRead;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fFileMemory = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fFullyLineated = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Initialize an input stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream to initialize.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszFilename The file to take the stream content from.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamInitForReading(PSCMSTREAM pStream, const char *pszFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync scmStreamInitInternal(pStream, false /*fWriteOrRead*/);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync void *pvFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cbFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = pStream->rc = RTFileReadAll(pszFilename, &pvFile, &cbFile);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_SUCCESS(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch = (char *)pvFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cb = cbFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cbAllocated = cbFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fFileMemory = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Initialize an output stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream to initialize.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pRelatedStream Pointer to a related stream. NULL is fine.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamInitForWriting(PSCMSTREAM pStream, PCSCMSTREAM pRelatedStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync scmStreamInitInternal(pStream, true /*fWriteOrRead*/);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* allocate stuff */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cbEstimate = pRelatedStream
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? pRelatedStream->cb + pRelatedStream->cb / 10
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : _64K;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbEstimate = RT_ALIGN(cbEstimate, _4K);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch = (char *)RTMemAlloc(cbEstimate);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pStream->pch)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cLinesEstimate = pRelatedStream && pRelatedStream->fFullyLineated
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? pRelatedStream->cLines + pRelatedStream->cLines / 10
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : cbEstimate / 24;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cLinesEstimate = RT_ALIGN(cLinesEstimate, 512);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines = (PSCMSTREAMLINE)RTMemAlloc(cLinesEstimate * sizeof(SCMSTREAMLINE));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pStream->paLines)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[0].off = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[0].cch = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[0].enmEol = SCMEOL_NONE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cbAllocated = cbEstimate;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLinesAllocated = cLinesEstimate;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMemFree(pStream->pch);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Frees the resources associated with the stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Nothing is happens to whatever the stream was initialized from or dumped to.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream to delete.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncvoid ScmStreamDelete(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pStream->pch)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pStream->fFileMemory)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMemFree(pStream->pch);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cbAllocated = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pStream->paLines)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMemFree(pStream->paLines);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLinesAllocated = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Get the stream status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamGetStatus(PCSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Grows the buffer of a write stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in write mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cbAppending The minimum number of bytes to grow the buffer
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * with.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int scmStreamGrowBuffer(PSCMSTREAM pStream, size_t cbAppending)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cbAllocated = pStream->cbAllocated;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbAllocated += RT_MAX(0x1000 + cbAppending, cbAllocated);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbAllocated = RT_ALIGN(cbAllocated, 0x1000);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync void *pvNew;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pStream->fFileMemory)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pvNew = RTMemRealloc(pStream->pch, cbAllocated);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pvNew)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pvNew = RTMemDupEx(pStream->pch, pStream->off, cbAllocated - pStream->off);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pvNew)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fFileMemory = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch = (char *)pvNew;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cbAllocated = cbAllocated;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Grows the line array of a stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param iMinLine Minimum line number.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int scmStreamGrowLines(PSCMSTREAM pStream, size_t iMinLine)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cLinesAllocated = pStream->cLinesAllocated;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cLinesAllocated += RT_MAX(512 + iMinLine, cLinesAllocated);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cLinesAllocated = RT_ALIGN(cLinesAllocated, 512);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync void *pvNew = RTMemRealloc(pStream->paLines, cLinesAllocated * sizeof(SCMSTREAMLINE));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pvNew)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines = (PSCMSTREAMLINE)pvNew;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLinesAllocated = cLinesAllocated;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Rewinds the stream and sets the mode to read.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncvoid ScmStreamRewindForReading(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fWriteOrRead = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Rewinds the stream and sets the mode to write.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncvoid ScmStreamRewindForWriting(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLines = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fWriteOrRead = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fFullyLineated = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Checks if it's a text stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Not 100% proof.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns true if it probably is a text file, false if not.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Write or read, doesn't matter.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncbool ScmStreamIsText(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (memchr(pStream->pch, '\0', pStream->cb))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pStream->cb)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Performs an integrity check of the stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamCheckItegrity(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Perform sanity checks.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t const cbFile = pStream->cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (size_t iLine = 0; iLine < pStream->cLines; iLine++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t offEol = pStream->paLines[iLine].off + pStream->paLines[iLine].cch;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(offEol + pStream->paLines[iLine].enmEol <= cbFile, VERR_INTERNAL_ERROR_2);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync switch (pStream->paLines[iLine].enmEol)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case SCMEOL_LF:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pStream->pch[offEol] == '\n', VERR_INTERNAL_ERROR_3);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case SCMEOL_CRLF:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pStream->pch[offEol] == '\r', VERR_INTERNAL_ERROR_3);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pStream->pch[offEol + 1] == '\n', VERR_INTERNAL_ERROR_3);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case SCMEOL_NONE:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_4);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync default:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_5);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Writes the stream to a file.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pszFilenameFmt The filename format string.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param ... Format arguments.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamWriteToFile(PSCMSTREAM pStream, const char *pszFilenameFmt, ...)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef RT_STRICT
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Check that what we're going to write makes sense first.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = ScmStreamCheckItegrity(pStream);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Do the actual writing.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFILE hFile;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync va_list va;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync va_start(va, pszFilenameFmt);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileOpenV(&hFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE, pszFilenameFmt, va);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_SUCCESS(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWrite(hFile, pStream->pch, pStream->cb, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFileClose(hFile);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Worker for ScmStreamGetLine that builds the line number index while parsing
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * the stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns Same as SCMStreamGetLine.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in read mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pcchLine Where to return the line length.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param penmEol Where to return the kind of end of line marker.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic const char *scmStreamGetLineInternal(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(!pStream->fWriteOrRead, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(pStream->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t off = pStream->off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cb = pStream->cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(off >= cb))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->fFullyLineated = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t iLine = pStream->iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(iLine >= pStream->cLinesAllocated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamGrowLines(pStream, iLine);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].off = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cb -= off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchRet = &pStream->pch[off];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pch = (const char *)memchr(pchRet, '\n', cb);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_LIKELY(pch))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cb = pch - pchRet;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = off + cb + 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( cb < 1
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || pch[-1] != '\r')
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_LF;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_CRLF;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cb--;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = off + cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_NONE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *pcchLine = cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch = cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLines = pStream->iLine = ++iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pchRet;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal worker that lineates a stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Caller must check that it is in
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * read mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int scmStreamLineate(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Save the stream position. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t const offSaved = pStream->off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t const iLineSaved = pStream->iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Get each line. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync while (scmStreamGetLineInternal(pStream, &cchLine, &enmEol))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* nothing */;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(RT_FAILURE(pStream->rc) || pStream->fFullyLineated);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Restore the position */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = offSaved;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = iLineSaved;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Get the current stream position as a line number.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns The current line (0-based).
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncsize_t ScmStreamTellLine(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Gets the number of lines in the stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns The number of lines.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncsize_t ScmStreamCountLines(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pStream->fFullyLineated)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync scmStreamLineate(pStream);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->cLines;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Seeks to a given line in the tream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in read mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param iLine The line to seek to. If this is beyond the end
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * of the stream, the position is set to the end.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsyncint ScmStreamSeekByLine(PSCMSTREAM pStream, size_t iLine)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(pStream->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Must be fully lineated of course. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(!pStream->fFullyLineated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamLineate(pStream);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Ok, do the job. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (iLine < pStream->cLines)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = pStream->paLines[iLine].off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = pStream->cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = pStream->cLines;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Get a numbered line from the stream (changes the position).
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * A line is always delimited by a LF character or the end of the stream. The
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * delimiter is not included in returned line length, but instead returned via
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * the @a penmEol indicator.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns Pointer to the first character in the line, not NULL terminated.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * NULL if the end of the stream has been reached or some problem
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * occured.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in read mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param iLine The line to get (0-based).
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pcchLine The length.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param penmEol Where to return the end of line type indicator.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(!pStream->fWriteOrRead, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(pStream->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Make sure it's fully lineated so we can use the index. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(!pStream->fFullyLineated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamLineate(pStream);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* End of stream? */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(iLine >= pStream->cLines))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = pStream->cb;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = pStream->cLines;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Get the data. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchRet = &pStream->pch[pStream->paLines[iLine].off];
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *pcchLine = pStream->paLines[iLine].cch;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *penmEol = pStream->paLines[iLine].enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* update the stream position. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = iLine + 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pchRet;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Get a line from the stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * A line is always delimited by a LF character or the end of the stream. The
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * delimiter is not included in returned line length, but instead returned via
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * the @a penmEol indicator.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns Pointer to the first character in the line, not NULL terminated.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * NULL if the end of the stream has been reached or some problem
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * occured.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in read mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pcchLine The length.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param penmEol Where to return the end of line type indicator.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic const char *ScmStreamGetLine(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pStream->fFullyLineated)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return scmStreamGetLineInternal(pStream, pcchLine, penmEol);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return ScmStreamGetLineByNo(pStream, pStream->iLine, pcchLine, penmEol);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Checks if the given line is empty or full of white space.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns true if white space only, false if not (or if non-existant).
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in read mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param iLine The line in question.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic bool ScmStreamIsWhiteLine(PSCMSTREAM pStream, size_t iLine)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pchLine)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync while (cchLine && RT_C_IS_SPACE(*pchLine))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pchLine++, cchLine--;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return cchLine == 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Try figure out the end of line style of the give stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns Most likely end of line style.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncSCMEOL ScmStreamGetEol(PSCMSTREAM pStream)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pStream->cLines > 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync enmEol = pStream->paLines[0].enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (pStream->cb == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync enmEol = SCMEOL_NONE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchLF = (const char *)memchr(pStream->pch, '\n', pStream->cb);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pchLF && pchLF != pStream->pch && pchLF[-1] == '\r')
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync enmEol = SCMEOL_CRLF;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync enmEol = SCMEOL_LF;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (enmEol == SCMEOL_NONE)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync enmEol = SCMEOL_CRLF;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync#else
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync enmEol = SCMEOL_LF;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync#endif
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync return enmEol;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync}
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/**
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * Get the end of line indicator type for a line.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync *
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @returns The EOL indicator. If the line isn't found, the default EOL
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * indicator is return.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @param pStream The stream.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @param iLine The line (0-base).
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsyncSCMEOL ScmStreamGetEolByLine(PSCMSTREAM pStream, size_t iLine)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync{
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync SCMEOL enmEol;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (iLine < pStream->cLines)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync enmEol = pStream->paLines[iLine].enmEol;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync else
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync enmEol = SCMEOL_CRLF;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync#else
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync enmEol = SCMEOL_LF;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync#endif
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync return enmEol;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync}
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/**
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * Appends a line to the stream.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync *
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @returns IPRT status code.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @param pStream The stream. Must be in write mode.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @param pchLine Pointer to the line.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @param cchLine Line length.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * @param enmEol Which end of line indicator to use.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamPutLine(PSCMSTREAM pStream, const char *pchLine, size_t cchLine, SCMEOL enmEol)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(pStream->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Make sure the previous line has a new-line indicator.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t off = pStream->off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t iLine = pStream->iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY( iLine != 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pStream->paLines[iLine].cch == 0, VERR_INTERNAL_ERROR_3);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL enmEol2 = enmEol != SCMEOL_NONE ? enmEol : ScmStreamGetEol(pStream);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(off + cchLine + enmEol + enmEol2 > pStream->cbAllocated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol + enmEol2);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (enmEol2 == SCMEOL_LF)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch[off++] = '\n';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch[off++] = '\r';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch[off++] = '\n';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine - 1].enmEol = enmEol2;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].off = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cb = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Ensure we've got sufficient buffer space.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(off + cchLine + enmEol > pStream->cbAllocated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Add a line record.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamGrowLines(pStream, iLine);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off + cchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync iLine++;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLines = iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Copy the line
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(&pStream->pch[off], pchLine, cchLine);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync off += cchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (enmEol == SCMEOL_LF)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch[off++] = '\n';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (enmEol == SCMEOL_CRLF)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch[off++] = '\r';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch[off++] = '\n';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cb = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Start a new line.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].off = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = SCMEOL_NONE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Writes to the stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in write mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pchBuf What to write.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cchBuf How much to write.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamWrite(PSCMSTREAM pStream, const char *pchBuf, size_t cchBuf)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(pStream->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Ensure we've got sufficient buffer space.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t off = pStream->off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(off + cchBuf > pStream->cbAllocated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamGrowBuffer(pStream, cchBuf);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t iLine = pStream->iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY( iLine > 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync iLine--;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLines = iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Deal with lines.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchLF = (const char *)memchr(pchBuf, '\n', cchBuf);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pchLF)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch += cchBuf;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchLine = pchBuf;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (;;)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = scmStreamGrowLines(pStream, iLine);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync iLine = pStream->iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = SCMEOL_NONE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchLine = pchLF - pchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( cchLine
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? pchLF[-1] != '\r'
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : !pStream->paLines[iLine].cch
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || pStream->pch[pStream->paLines[iLine].off + pStream->paLines[iLine].cch - 1] != '\r')
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = SCMEOL_LF;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = SCMEOL_CRLF;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cchLine--;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch += cchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync iLine++;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t offBuf = pchLF + 1 - pchBuf;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].off = off + offBuf;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].enmEol = SCMEOL_NONE;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchLeft = cchBuf - offBuf;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pchLF = (const char *)memchr(pchLF + 1, '\n', cchLeft);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pchLF)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[iLine].cch = cchLeft;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->iLine = iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cLines = iLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Copy the data and update position and size.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync memcpy(&pStream->pch[off], pchBuf, cchBuf);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync off += cchBuf;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->cb = off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Write a character to the stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream. Must be in write mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pchBuf What to write.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cchBuf How much to write.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamPutCh(PSCMSTREAM pStream, char ch)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (RT_FAILURE(pStream->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pStream->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * Only deal with the simple cases here, use ScmStreamWrite for the
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync * annyoing stuff.
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync size_t off = pStream->off;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( ch == '\n'
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || RT_UNLIKELY(off + 1 > pStream->cbAllocated))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return ScmStreamWrite(pStream, &ch, 1);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Just append it.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->pch[off] = ch;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->off = off + 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pStream->paLines[pStream->iLine].cch++;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * The stream positions will be used and changed in both streams.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns IPRT status code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pDst The destionation stream. Must be in write mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cLines The number of lines. (0 is accepted.)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pSrc The source stream. Must be in read mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncint ScmStreamCopyLines(PSCMSTREAM pDst, PSCMSTREAM pSrc, size_t cLines)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(pDst->fWriteOrRead, VERR_ACCESS_DENIED);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(pDst->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pDst->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync AssertReturn(!pSrc->fWriteOrRead, VERR_ACCESS_DENIED);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(pSrc->rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pSrc->rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync while (cLines-- > 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchLine = ScmStreamGetLine(pSrc, &cchLine, &enmEol);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pchLine)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return pDst->rc = (RT_FAILURE(pSrc->rc) ? pSrc->rc : VERR_EOF);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = ScmStreamPutLine(pDst, pchLine, cchLine, enmEol);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RT_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/* -=-=-=-=-=- diff -=-=-=-=-=- */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Prints a range of lines with a prefix.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pState The diff state.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param chPrefix The prefix.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pStream The stream to get the lines from.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param iLine The first line.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cLines The number of lines.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void scmDiffPrintLines(PSCMDIFFSTATE pState, char chPrefix, PSCMSTREAM pStream, size_t iLine, size_t cLines)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync while (cLines-- > 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync SCMEOL enmEol;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPutCh(pState->pDiff, chPrefix);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pchLine && cchLine)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pState->fSpecialChars)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmWrite(pState->pDiff, pchLine, cchLine);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t offVir = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchStart = pchLine;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync while (pchTab)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmWrite(pState->pDiff, pchStart, pchTab - pchStart);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync offVir += pchTab - pchStart;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchTab = pState->cchTab - offVir % pState->cchTab;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync switch (cchTab)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 1: RTStrmPutStr(pState->pDiff, "."); break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 2: RTStrmPutStr(pState->pDiff, ".."); break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 3: RTStrmPutStr(pState->pDiff, "[T]"); break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 4: RTStrmPutStr(pState->pDiff, "[TA]"); break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 5: RTStrmPutStr(pState->pDiff, "[TAB]"); break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync default: RTStrmPrintf(pState->pDiff, "[TAB%.*s]", cchTab - 5, g_szTabSpaces); break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync offVir += cchTab;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* next */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pchStart = pchTab + 1;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pchTab = (const char *)memchr(pchStart, '\t', cchLine - (pchStart - pchLine));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchLeft = cchLine - (pchStart - pchLine);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (cchLeft)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmWrite(pState->pDiff, pchStart, cchLeft);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pState->fSpecialChars)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPutCh(pState->pDiff, '\n');
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (enmEol == SCMEOL_LF)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPutStr(pState->pDiff, "[LF]\n");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (enmEol == SCMEOL_CRLF)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPutStr(pState->pDiff, "[CRLF]\n");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPutStr(pState->pDiff, "[NONE]\n");
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync iLine++;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Reports a difference and propells the streams to the lines following the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * resync.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @returns New pState->cDiff value (just to return something).
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param pState The diff state. The cDiffs member will be
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * incremented.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cMatches The resync length.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param iLeft Where the difference starts on the left side.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cLeft How long it is on this side. ~(size_t)0 is used
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * to indicate that it goes all the way to the end.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param iRight Where the difference starts on the right side.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * @param cRight How long it is.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic size_t scmDiffReport(PSCMDIFFSTATE pState, size_t cMatches,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t iLeft, size_t cLeft,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t iRight, size_t cRight)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Adjust the input.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (cLeft == ~(size_t)0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t c = ScmStreamCountLines(pState->pLeft);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (c >= iLeft)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cLeft = c - iLeft;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync iLeft = c;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cLeft = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (cRight == ~(size_t)0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t c = ScmStreamCountLines(pState->pRight);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (c >= iRight)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cRight = c - iRight;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync iRight = c;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cRight = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Print header if it's the first difference
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pState->cDiffs)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPrintf(pState->pDiff, "diff %s %s\n", pState->pszFilename, pState->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Emit the change description.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char ch = cLeft == 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? 'a'
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : cRight == 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? 'd'
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : 'c';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (cLeft > 1 && cRight > 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu,%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1, iRight + cRight);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (cLeft > 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (cRight > 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPrintf(pState->pDiff, "%zu%c%zu,%zu\n", iLeft + 1, ch, iRight + 1, iRight + cRight);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTStrmPrintf(pState->pDiff, "%zu%c%zu\n", iLeft + 1, ch, iRight + 1);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * And the lines.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (cLeft)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync scmDiffPrintLines(pState, '<', pState->pLeft, iLeft, cLeft);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (cLeft && cRight)
RTStrmPrintf(pState->pDiff, "---\n");
if (cRight)
scmDiffPrintLines(pState, '>', pState->pRight, iRight, cRight);
/*
* Reposition the streams (safely ignores return value).
*/
ScmStreamSeekByLine(pState->pLeft, iLeft + cLeft + cMatches);
ScmStreamSeekByLine(pState->pRight, iRight + cRight + cMatches);
pState->cDiffs++;
return pState->cDiffs;
}
/**
* Helper for scmDiffCompare that takes care of trailing spaces and stuff
* like that.
*/
static bool scmDiffCompareSlow(PSCMDIFFSTATE pState,
const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
{
if (pState->fIgnoreTrailingWhite)
{
while (cchLeft > 0 && RT_C_IS_SPACE(pchLeft[cchLeft - 1]))
cchLeft--;
while (cchRight > 0 && RT_C_IS_SPACE(pchRight[cchRight - 1]))
cchRight--;
}
if (pState->fIgnoreLeadingWhite)
{
while (cchLeft > 0 && RT_C_IS_SPACE(*pchLeft))
pchLeft++, cchLeft--;
while (cchRight > 0 && RT_C_IS_SPACE(*pchRight))
pchRight++, cchRight--;
}
if ( cchLeft != cchRight
|| (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
|| memcmp(pchLeft, pchRight, cchLeft))
return false;
return true;
}
/**
* Compare two lines.
*
* @returns true if the are equal, false if not.
*/
DECLINLINE(bool) scmDiffCompare(PSCMDIFFSTATE pState,
const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
{
if ( cchLeft != cchRight
|| (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
|| memcmp(pchLeft, pchRight, cchLeft))
{
if ( pState->fIgnoreTrailingWhite
|| pState->fIgnoreTrailingWhite)
return scmDiffCompareSlow(pState,
pchLeft, cchLeft, enmEolLeft,
pchRight, cchRight, enmEolRight);
return false;
}
return true;
}
/**
* Compares two sets of lines from the two files.
*
* @returns true if they matches, false if they don't.
* @param pState The diff state.
* @param iLeft Where to start in the left stream.
* @param iRight Where to start in the right stream.
* @param cLines How many lines to compare.
*/
static bool scmDiffCompareLines(PSCMDIFFSTATE pState, size_t iLeft, size_t iRight, size_t cLines)
{
for (size_t iLine = 0; iLine < cLines; iLine++)
{
SCMEOL enmEolLeft;
size_t cchLeft;
const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iLeft + iLine, &cchLeft, &enmEolLeft);
SCMEOL enmEolRight;
size_t cchRight;
const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iRight + iLine, &cchRight, &enmEolRight);
if (!scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
return false;
}
return true;
}
/**
* Resynchronize the two streams and reports the difference.
*
* Upon return, the streams will be positioned after the block of @a cMatches
* lines where it resynchronized them.
*
* @returns pState->cDiffs (just so we can use it in a return statement).
* @param pState The state.
* @param cMatches The number of lines that needs to match for the
* stream to be considered synchronized again.
*/
static size_t scmDiffSynchronize(PSCMDIFFSTATE pState, size_t cMatches)
{
size_t const iStartLeft = ScmStreamTellLine(pState->pLeft) - 1;
size_t const iStartRight = ScmStreamTellLine(pState->pRight) - 1;
Assert(cMatches > 0);
/*
* Compare each new line from each of the streams will all the preceding
* ones, including iStartLeft/Right.
*/
for (size_t iRange = 1; ; iRange++)
{
/*
* Get the next line in the left stream and compare it against all the
* preceding lines on the right side.
*/
SCMEOL enmEol;
size_t cchLine;
const char *pchLine = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iRange, &cchLine, &enmEol);
if (!pchLine)
return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
for (size_t iRight = cMatches - 1; iRight < iRange; iRight++)
{
SCMEOL enmEolRight;
size_t cchRight;
const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRight,
&cchRight, &enmEolRight);
if ( scmDiffCompare(pState, pchLine, cchLine, enmEol, pchRight, cchRight, enmEolRight)
&& scmDiffCompareLines(pState,
iStartLeft + iRange + 1 - cMatches,
iStartRight + iRight + 1 - cMatches,
cMatches - 1)
)
return scmDiffReport(pState, cMatches,
iStartLeft, iRange + 1 - cMatches,
iStartRight, iRight + 1 - cMatches);
}
/*
* Get the next line in the right stream and compare it against all the
* lines on the right side.
*/
pchLine = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRange, &cchLine, &enmEol);
if (!pchLine)
return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
for (size_t iLeft = cMatches - 1; iLeft <= iRange; iLeft++)
{
SCMEOL enmEolLeft;
size_t cchLeft;
const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iLeft,
&cchLeft, &enmEolLeft);
if ( scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchLine, cchLine, enmEol)
&& scmDiffCompareLines(pState,
iStartLeft + iLeft + 1 - cMatches,
iStartRight + iRange + 1 - cMatches,
cMatches - 1)
)
return scmDiffReport(pState, cMatches,
iStartLeft, iLeft + 1 - cMatches,
iStartRight, iRange + 1 - cMatches);
}
}
}
/**
* Creates a diff of the changes between the streams @a pLeft and @a pRight.
*
* This currently only implements the simplest diff format, so no contexts.
*
* Also, note that we won't detect differences in the final newline of the
* streams.
*
* @returns The number of differences.
* @param pszFilename The filename.
* @param pLeft The left side stream.
* @param pRight The right side stream.
* @param fIgnoreEol Whether to ignore end of line markers.
* @param fIgnoreLeadingWhite Set if leading white space should be ignored.
* @param fIgnoreTrailingWhite Set if trailing white space should be ignored.
* @param fSpecialChars Whether to print special chars in a human
* readable form or not.
* @param cchTab The tab size.
* @param pDiff Where to write the diff.
*/
size_t ScmDiffStreams(const char *pszFilename, PSCMSTREAM pLeft, PSCMSTREAM pRight, bool fIgnoreEol,
bool fIgnoreLeadingWhite, bool fIgnoreTrailingWhite, bool fSpecialChars,
size_t cchTab, PRTSTREAM pDiff)
{
#ifdef RT_STRICT
ScmStreamCheckItegrity(pLeft);
ScmStreamCheckItegrity(pRight);
#endif
/*
* Set up the diff state.
*/
SCMDIFFSTATE State;
State.cDiffs = 0;
State.pszFilename = pszFilename;
State.pLeft = pLeft;
State.pRight = pRight;
State.fIgnoreEol = fIgnoreEol;
State.fIgnoreLeadingWhite = fIgnoreLeadingWhite;
State.fIgnoreTrailingWhite = fIgnoreTrailingWhite;
State.fSpecialChars = fSpecialChars;
State.cchTab = cchTab;
State.pDiff = pDiff;
/*
* Compare them line by line.
*/
ScmStreamRewindForReading(pLeft);
ScmStreamRewindForReading(pRight);
const char *pchLeft;
const char *pchRight;
for (;;)
{
SCMEOL enmEolLeft;
size_t cchLeft;
pchLeft = ScmStreamGetLine(pLeft, &cchLeft, &enmEolLeft);
SCMEOL enmEolRight;
size_t cchRight;
pchRight = ScmStreamGetLine(pRight, &cchRight, &enmEolRight);
if (!pchLeft || !pchRight)
break;
if (!scmDiffCompare(&State, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
scmDiffSynchronize(&State, 3);
}
/*
* Deal with any remaining differences.
*/
if (pchLeft)
scmDiffReport(&State, 0, ScmStreamTellLine(pLeft) - 1, ~(size_t)0, ScmStreamTellLine(pRight), 0);
else if (pchRight)
scmDiffReport(&State, 0, ScmStreamTellLine(pLeft), 0, ScmStreamTellLine(pRight) - 1, ~(size_t)0);
/*
* Report any errors.
*/
if (RT_FAILURE(ScmStreamGetStatus(pLeft)))
RTMsgError("Left diff stream error: %Rrc\n", ScmStreamGetStatus(pLeft));
if (RT_FAILURE(ScmStreamGetStatus(pRight)))
RTMsgError("Right diff stream error: %Rrc\n", ScmStreamGetStatus(pRight));
return State.cDiffs;
}
/* -=-=-=-=-=- settings -=-=-=-=-=- */
/**
* Init a settings structure with settings from @a pSrc.
*
* @returns IPRT status code
* @param pSettings The settings.
* @param pSrc The source settings.
*/
static int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
{
*pSettings = *pSrc;
int rc = RTStrDupEx(&pSettings->pszFilterFiles, pSrc->pszFilterFiles);
if (RT_SUCCESS(rc))
{
rc = RTStrDupEx(&pSettings->pszFilterOutFiles, pSrc->pszFilterOutFiles);
if (RT_SUCCESS(rc))
{
rc = RTStrDupEx(&pSettings->pszFilterOutDirs, pSrc->pszFilterOutDirs);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
RTStrFree(pSettings->pszFilterOutFiles);
}
RTStrFree(pSettings->pszFilterFiles);
}
pSettings->pszFilterFiles = NULL;
pSettings->pszFilterOutFiles = NULL;
pSettings->pszFilterOutDirs = NULL;
return rc;
}
/**
* Init a settings structure.
*
* @returns IPRT status code
* @param pSettings The settings.
*/
static int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
{
return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
}
/**
* Deletes the settings, i.e. free any dynamically allocated content.
*
* @param pSettings The settings.
*/
static void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
{
if (pSettings)
{
Assert(pSettings->cchTab != ~(unsigned)0);
pSettings->cchTab = ~(unsigned)0;
RTStrFree(pSettings->pszFilterFiles);
pSettings->pszFilterFiles = NULL;
RTStrFree(pSettings->pszFilterOutFiles);
pSettings->pszFilterOutFiles = NULL;
RTStrFree(pSettings->pszFilterOutDirs);
pSettings->pszFilterOutDirs = NULL;
}
}
/**
* Processes a RTGetOpt result.
*
* @retval VINF_SUCCESS if handled.
* @retval VERR_OUT_OF_RANGE if the option value was out of range.
* @retval VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
*
* @param pSettings The settings to change.
* @param rc The RTGetOpt return value.
* @param pValueUnion The RTGetOpt value union.
*/
static int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion)
{
switch (rc)
{
case SCMOPT_CONVERT_EOL:
pSettings->fConvertEol = true;
return VINF_SUCCESS;
case SCMOPT_NO_CONVERT_EOL:
pSettings->fConvertEol = false;
return VINF_SUCCESS;
case SCMOPT_CONVERT_TABS:
pSettings->fConvertTabs = true;
return VINF_SUCCESS;
case SCMOPT_NO_CONVERT_TABS:
pSettings->fConvertTabs = false;
return VINF_SUCCESS;
case SCMOPT_FORCE_FINAL_EOL:
pSettings->fForceFinalEol = true;
return VINF_SUCCESS;
case SCMOPT_NO_FORCE_FINAL_EOL:
pSettings->fForceFinalEol = false;
return VINF_SUCCESS;
case SCMOPT_FORCE_TRAILING_LINE:
pSettings->fForceTrailingLine = true;
return VINF_SUCCESS;
case SCMOPT_NO_FORCE_TRAILING_LINE:
pSettings->fForceTrailingLine = false;
return VINF_SUCCESS;
case SCMOPT_STRIP_TRAILING_BLANKS:
pSettings->fStripTrailingBlanks = true;
return VINF_SUCCESS;
case SCMOPT_NO_STRIP_TRAILING_BLANKS:
pSettings->fStripTrailingBlanks = false;
return VINF_SUCCESS;
case SCMOPT_STRIP_TRAILING_LINES:
pSettings->fStripTrailingLines = true;
return VINF_SUCCESS;
case SCMOPT_NO_STRIP_TRAILING_LINES:
pSettings->fStripTrailingLines = false;
return VINF_SUCCESS;
case SCMOPT_TAB_SIZE:
if ( pValueUnion->u8 < 1
|| pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
{
RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
return VERR_OUT_OF_RANGE;
}
pSettings->cchTab = pValueUnion->u8;
return VINF_SUCCESS;
case SCMOPT_FILTER_OUT_DIRS:
case SCMOPT_FILTER_FILES:
case SCMOPT_FILTER_OUT_FILES:
{
char **ppsz;
switch (rc)
{
case SCMOPT_FILTER_OUT_DIRS: ppsz = &pSettings->pszFilterOutDirs; break;
case SCMOPT_FILTER_FILES: ppsz = &pSettings->pszFilterFiles; break;
case SCMOPT_FILTER_OUT_FILES: ppsz = &pSettings->pszFilterOutFiles; break;
}
/*
* An empty string zaps the current list.
*/
if (!*pValueUnion->psz)
return RTStrATruncate(ppsz, 0);
/*
* Non-empty strings are appended to the pattern list.
*
* Strip leading and trailing pattern separators before attempting
* to append it. If it's just separators, don't do anything.
*/
const char *pszSrc = pValueUnion->psz;
while (*pszSrc == '|')
pszSrc++;
size_t cchSrc = strlen(pszSrc);
while (cchSrc > 0 && pszSrc[cchSrc - 1] == '|')
cchSrc--;
if (!cchSrc)
return VINF_SUCCESS;
return RTStrAAppendExN(ppsz, 2,
"|", *ppsz && **ppsz ? 1 : 0,
pszSrc, cchSrc);
}
default:
return VERR_GETOPT_UNKNOWN_OPTION;
}
}
/**
* Parses an option string.
*
* @returns IPRT status code.
* @param pBase The base settings structure to apply the options
* to.
* @param pszOptions The options to parse.
*/
static int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine)
{
int cArgs;
char **papszArgs;
int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, NULL);
if (RT_SUCCESS(rc))
{
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetOptState;
rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
if (RT_SUCCESS(rc))
{
while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
{
rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion);
if (RT_FAILURE(rc))
break;
}
}
RTGetOptArgvFree(papszArgs);
}
return rc;
}
/**
* Parses an unterminated option string.
*
* @returns IPRT status code.
* @param pBase The base settings structure to apply the options
* to.
* @param pchLine The line.
* @param cchLine The line length.
*/
static int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine)
{
char *pszLine = RTStrDupN(pchLine, cchLine);
if (!pszLine)
return VERR_NO_MEMORY;
int rc = scmSettingsBaseParseString(pBase, pszLine);
RTStrFree(pszLine);
return rc;
}
/**
* Verifies the options string.
*
* @returns IPRT status code.
* @param pszOptions The options to verify .
*/
static int scmSettingsBaseVerifyString(const char *pszOptions)
{
SCMSETTINGSBASE Base;
int rc = scmSettingsBaseInit(&Base);
if (RT_SUCCESS(rc))
{
rc = scmSettingsBaseParseString(&Base, pszOptions);
scmSettingsBaseDelete(&Base);
}
return rc;
}
/**
* Loads settings found in editor and SCM settings directives within the
* document (@a pStream).
*
* @returns IPRT status code.
* @param pBase The settings base to load settings into.
* @param pStream The stream to scan for settings directives.
*/
static int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
{
/** @todo Editor and SCM settings directives in documents. */
return VINF_SUCCESS;
}
/**
* Creates a new settings file struct, cloning @a pSettings.
*
* @returns IPRT status code.
* @param ppSettings Where to return the new struct.
* @param pSettingsBase The settings to inherit from.
*/
static int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
{
PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
if (!pSettings)
return VERR_NO_MEMORY;
int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
if (RT_SUCCESS(rc))
{
pSettings->pDown = NULL;
pSettings->pUp = NULL;
pSettings->paPairs = NULL;
pSettings->cPairs = 0;
*ppSettings = pSettings;
return VINF_SUCCESS;
}
RTMemFree(pSettings);
return rc;
}
/**
* Destroys a settings structure.
*
* @param pSettings The settgins structure to destroy. NULL is OK.
*/
static void scmSettingsDestroy(PSCMSETTINGS pSettings)
{
if (pSettings)
{
scmSettingsBaseDelete(&pSettings->Base);
for (size_t i = 0; i < pSettings->cPairs; i++)
{
RTStrFree(pSettings->paPairs[i].pszPattern);
RTStrFree(pSettings->paPairs[i].pszOptions);
pSettings->paPairs[i].pszPattern = NULL;
pSettings->paPairs[i].pszOptions = NULL;
}
RTMemFree(pSettings->paPairs);
pSettings->paPairs = NULL;
RTMemFree(pSettings);
}
}
/**
* Adds a pattern/options pair to the settings structure.
*
* @returns IPRT status code.
* @param pSettings The settings.
* @param pchLine The line containing the unparsed pair.
* @param cchLine The length of the line.
*/
static int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine)
{
/*
* Split the string.
*/
const char *pchOptions = (const char *)memchr(pchLine, ':', cchLine);
if (!pchOptions)
return VERR_INVALID_PARAMETER;
size_t cchPattern = pchOptions - pchLine - 1;
size_t cchOptions = cchLine - cchPattern - 1 - 1;
pchOptions++;
/* strip spaces everywhere */
while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
cchPattern--;
while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
cchPattern--, pchLine++;
while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
cchOptions--;
while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
cchOptions--, cchOptions++;
/* Quietly ignore empty patterns and empty options. */
if (!cchOptions || !cchPattern)
return VINF_SUCCESS;
/*
* Add the pair and verify the option string.
*/
uint32_t iPair = pSettings->cPairs;
if ((iPair % 32) == 0)
{
void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
if (!pvNew)
return VERR_NO_MEMORY;
pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
}
pSettings->paPairs[iPair].pszPattern = RTStrDupN(pchLine, cchPattern);
pSettings->paPairs[iPair].pszOptions = RTStrDupN(pchOptions, cchOptions);
int rc;
if ( pSettings->paPairs[iPair].pszPattern
&& pSettings->paPairs[iPair].pszOptions)
rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
else
rc = VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
pSettings->cPairs = iPair + 1;
else
{
RTStrFree(pSettings->paPairs[iPair].pszPattern);
RTStrFree(pSettings->paPairs[iPair].pszOptions);
}
return rc;
}
/**
* Loads in the settings from @a pszFilename.
*
* @returns IPRT status code.
* @param pSettings Where to load the settings file.
* @param pszFilename The file to load.
*/
static int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
{
SCMSTREAM Stream;
int rc = ScmStreamInitForReading(&Stream, pszFilename);
if (RT_FAILURE(rc))
{
RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
return rc;
}
SCMEOL enmEol;
const char *pchLine;
size_t cchLine;
while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
{
/* Ignore leading spaces. */
while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
pchLine++, cchLine--;
/* Ignore empty lines and comment lines. */
if (cchLine < 1 || *pchLine == '#')
continue;
/* What kind of line is it? */
const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
if (pchColon)
rc = scmSettingsAddPair(pSettings, pchLine, cchLine);
else
rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine);
if (RT_FAILURE(rc))
{
RTMsgError("%s:%d: %Rrc\n", pszFilename, ScmStreamTellLine(&Stream), rc);
break;
}
}
if (RT_SUCCESS(rc))
{
rc = ScmStreamGetStatus(&Stream);
if (RT_FAILURE(rc))
RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
}
ScmStreamDelete(&Stream);
return rc;
}
/**
* Parse the specified settings file creating a new settings struct from it.
*
* @returns IPRT status code
* @param ppSettings Where to return the new settings.
* @param pszFilename The file to parse.
* @param pSettingsBase The base settings we inherit from.
*/
static int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
{
PSCMSETTINGS pSettings;
int rc = scmSettingsCreate(&pSettings, pSettingsBase);
if (RT_SUCCESS(rc))
{
rc = scmSettingsLoadFile(pSettings, pszFilename);
if (RT_SUCCESS(rc))
{
*ppSettings = pSettings;
return VINF_SUCCESS;
}
scmSettingsDestroy(pSettings);
}
*ppSettings = NULL;
return rc;
}
/**
* Create an initial settings structure when starting processing a new file or
* directory.
*
* This will look for .scm-settings files from the root and down to the
* specified directory, combining them into the returned settings structure.
*
* @returns IPRT status code.
* @param ppSettings Where to return the pointer to the top stack
* object.
* @param pBaseSettings The base settings we inherit from (globals
* typically).
* @param pszPath The absolute path to the new directory or file.
*/
static int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
{
/*
* We'll be working with a stack copy of the path.
*/
char szFile[RTPATH_MAX];
size_t cchDir = strlen(pszPath);
if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
return VERR_FILENAME_TOO_LONG;
/*
* Create the bottom-most settings.
*/
PSCMSETTINGS pSettings;
int rc = scmSettingsCreate(&pSettings, pBaseSettings);
if (RT_FAILURE(rc))
return rc;
/*
* Enumerate the path components from the root and down. Load any setting
* files we find.
*/
size_t cComponents = RTPathCountComponents(pszPath);
for (size_t i = 1; i <= cComponents; i++)
{
rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
if (RT_SUCCESS(rc))
rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
if (RT_FAILURE(rc))
break;
if (RTFileExists(szFile))
{
rc = scmSettingsLoadFile(pSettings, szFile);
if (RT_FAILURE(rc))
break;
}
}
if (RT_SUCCESS(rc))
*ppSettings = pSettings;
else
scmSettingsDestroy(pSettings);
return rc;
}
/**
* Pushes a new settings set onto the stack.
*
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
* @param pSettings The settings to push onto the stack.
*/
static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
{
PSCMSETTINGS pOld = *ppSettingsStack;
pSettings->pDown = pOld;
pSettings->pUp = NULL;
if (pOld)
pOld->pUp = pSettings;
*ppSettingsStack = pSettings;
}
/**
* Pushes the settings of the specified directory onto the stack.
*
* We will load any .scm-settings in the directory. A stack entry is added even
* if no settings file was found.
*
* @returns IPRT status code.
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
* @param pszDir The directory to do this for.
*/
static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
{
char szFile[RTPATH_MAX];
int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
if (RT_SUCCESS(rc))
{
PSCMSETTINGS pSettings;
rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
if (RT_SUCCESS(rc))
{
if (RTFileExists(szFile))
rc = scmSettingsLoadFile(pSettings, szFile);
if (RT_SUCCESS(rc))
{
scmSettingsStackPush(ppSettingsStack, pSettings);
return VINF_SUCCESS;
}
scmSettingsDestroy(pSettings);
}
}
return rc;
}
/**
* Pops a settings set off the stack.
*
* @returns The popped setttings.
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
*/
static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
{
PSCMSETTINGS pRet = *ppSettingsStack;
PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
*ppSettingsStack = pNew;
if (pNew)
pNew->pUp = NULL;
if (pRet)
{
pRet->pUp = NULL;
pRet->pDown = NULL;
}
return pRet;
}
/**
* Pops and destroys the top entry of the stack.
*
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
*/
static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
{
scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
}
/**
* Constructs the base settings for the specified file name.
*
* @returns IPRT status code.
* @param pSettingsStack The top element on the settings stack.
* @param pszFilename The file name.
* @param pszBasename The base name (pointer within @a pszFilename).
* @param cchBasename The length of the base name. (For passing to
* RTStrSimplePatternMultiMatch.)
* @param pBase Base settings to initialize.
*/
static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
{
int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
if (RT_SUCCESS(rc))
{
/* find the bottom entry in the stack. */
PCSCMSETTINGS pCur = pSettingsStack;
while (pCur->pDown)
pCur = pCur->pDown;
/* Work our way up thru the stack and look for matching pairs. */
while (pCur)
{
size_t const cPairs = pCur->cPairs;
if (cPairs)
{
for (size_t i = 0; i < cPairs; i++)
if ( RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
pszBasename, cchBasename, NULL)
|| RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
pszFilename, RTSTR_MAX, NULL))
{
rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions);
if (RT_FAILURE(rc))
break;
}
if (RT_FAILURE(rc))
break;
}
/* advance */
pCur = pCur->pUp;
}
}
if (RT_FAILURE(rc))
scmSettingsBaseDelete(pBase);
return rc;
}
/* -=-=-=-=-=- misc -=-=-=-=-=- */
/**
* Prints a verbose message if the level is high enough.
*
* @param pState The rewrite state. Optional.
* @param iLevel The required verbosity level.
* @param pszFormat The message format string. Can be NULL if we
* only want to trigger the per file message.
* @param ... Format arguments.
*/
static void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...)
{
if (iLevel <= g_iVerbosity)
{
if (pState && !pState->fFirst)
{
RTPrintf("%s: info: Rewriting '%s'...\n", g_szProgName, pState->pszFilename);
pState->fFirst = true;
}
if (pszFormat)
{
RTPrintf("%s: info: ", g_szProgName);
va_list va;
va_start(va, pszFormat);
RTPrintfV(pszFormat, va);
va_end(va);
}
}
}
/* -=-=-=-=-=- rewriters -=-=-=-=-=- */
/**
* Strip trailing blanks (space & tab).
*
* @returns True if modified, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if (!pSettings->fStripTrailingBlanks)
return false;
bool fModified = false;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
int rc;
if ( cchLine == 0
|| !RT_C_IS_BLANK(pchLine[cchLine - 1]) )
rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
else
{
cchLine--;
while (cchLine > 0 && RT_C_IS_BLANK(pchLine[cchLine - 1]))
cchLine--;
rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
fModified = true;
}
if (RT_FAILURE(rc))
return false;
}
if (fModified)
ScmVerbose(pState, 2, " * Stripped trailing blanks\n");
return fModified;
}
/**
* Expand tabs.
*
* @returns True if modified, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if (!pSettings->fConvertTabs)
return false;
size_t const cchTab = pSettings->cchTab;
bool fModified = false;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
int rc;
const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
if (!pchTab)
rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
else
{
size_t offTab = 0;
const char *pchChunk = pchLine;
for (;;)
{
size_t cchChunk = pchTab - pchChunk;
offTab += cchChunk;
ScmStreamWrite(pOut, pchChunk, cchChunk);
size_t cchToTab = cchTab - offTab % cchTab;
ScmStreamWrite(pOut, g_szTabSpaces, cchToTab);
offTab += cchToTab;
pchChunk = pchTab + 1;
size_t cchLeft = cchLine - (pchChunk - pchLine);
pchTab = (const char *)memchr(pchChunk, '\t', cchLeft);
if (!pchTab)
{
rc = ScmStreamPutLine(pOut, pchChunk, cchLeft, enmEol);
break;
}
}
fModified = true;
}
if (RT_FAILURE(rc))
return false;
}
if (fModified)
ScmVerbose(pState, 2, " * Expanded tabs\n");
return fModified;
}
/**
* Worker for rewrite_ForceNativeEol, rewrite_ForceLF and rewrite_ForceCRLF.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
* @param enmDesiredEol The desired end of line indicator type.
*/
static bool rewrite_ForceEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings, SCMEOL enmDesiredEol)
{
if (!pSettings->fConvertEol)
return false;
bool fModified = false;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
if ( enmEol != enmDesiredEol
&& enmEol != SCMEOL_NONE)
{
fModified = true;
enmEol = enmDesiredEol;
}
int rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
if (RT_FAILURE(rc))
return false;
}
if (fModified)
ScmVerbose(pState, 2, " * Converted EOL markers\n");
return fModified;
}
/**
* Force native end of line indicator.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF);
#else
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF);
#endif
}
/**
* Force the stream to use LF as the end of line indicator.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF);
}
/**
* Force the stream to use CRLF as the end of line indicator.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF);
}
/**
* Strip trailing blank lines and/or make sure there is exactly one blank line
* at the end of the file.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*
* @remarks ASSUMES trailing white space has been removed already.
*/
static bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if ( !pSettings->fStripTrailingLines
&& !pSettings->fForceTrailingLine
&& !pSettings->fForceFinalEol)
return false;
size_t const cLines = ScmStreamCountLines(pIn);
/* Empty files remains empty. */
if (cLines <= 1)
return false;
/* Figure out if we need to adjust the number of lines or not. */
size_t cLinesNew = cLines;
if ( pSettings->fStripTrailingLines
&& ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
{
while ( cLinesNew > 1
&& ScmStreamIsWhiteLine(pIn, cLinesNew - 2))
cLinesNew--;
}
if ( pSettings->fForceTrailingLine
&& !ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
cLinesNew++;
bool fFixMissingEol = pSettings->fForceFinalEol
&& ScmStreamGetEolByLine(pIn, cLinesNew - 1) == SCMEOL_NONE;
if ( !fFixMissingEol
&& cLines == cLinesNew)
return false;
/* Copy the number of lines we've arrived at. */
ScmStreamRewindForReading(pIn);
size_t cCopied = RT_MIN(cLinesNew, cLines);
ScmStreamCopyLines(pOut, pIn, cCopied);
if (cCopied != cLinesNew)
{
while (cCopied++ < cLinesNew)
ScmStreamPutLine(pOut, "", 0, ScmStreamGetEol(pIn));
}
/* Fix missing EOL if required. */
else if (fFixMissingEol)
{
if (ScmStreamGetEol(pIn) == SCMEOL_LF)
ScmStreamWrite(pOut, "\n", 1);
else
ScmStreamWrite(pOut, "\r\n", 2);
}
ScmVerbose(pState, 2, " * Adjusted trailing blank lines\n");
return true;
}
/**
* Makefile.kup are empty files, enforce this.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
/* These files should be zero bytes. */
if (pIn->cb == 0)
return false;
ScmVerbose(pState, 2, " * Truncated file to zero bytes\n");
return true;
}
/**
* Rewrite a kBuild makefile.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*
* @todo
*
* Ideas for Makefile.kmk and Config.kmk:
* - sort if1of/ifn1of sets.
* - line continuation slashes should only be preceeded by one space.
*/
static bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
return false;
}
/**
* Rewrite a C/C++ source or header file.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*
* @todo
*
* Ideas for C/C++:
* - space after if, while, for, switch
* - spaces in for (i=0;i<x;i++)
* - complex conditional, bird style.
* - remove unnecessary parentheses.
* - sort defined RT_OS_*|| and RT_ARCH
* - sizeof without parenthesis.
* - defined without parenthesis.
* - trailing spaces.
* - parameter indentation.
* - space after comma.
* - while (x--); -> multi line + comment.
* - else statement;
* - space between function and left parenthesis.
* - TODO, XXX, @todo cleanup.
* - Space before/after '*'.
* - ensure new line at end of file.
* - Indentation of precompiler statements (#ifdef, #defines).
* - space between functions.
*/
static bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
return false;
}
/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
/**
* Processes a file.
*
* @returns IPRT status code.
* @param pszFilename The file name.
* @param pszBasename The base name (pointer within @a pszFilename).
* @param cchBasename The length of the base name. (For passing to
* RTStrSimplePatternMultiMatch.)
* @param pBaseSettings The base settings to use. It's OK to modify
* these.
*/
static int scmProcessFileInner(const char *pszFilename, const char *pszBasename, size_t cchBasename,
PSCMSETTINGSBASE pBaseSettings)
{
/*
* Init the rewriter state data.
*/
SCMRWSTATE State;
State.fFirst = false;
State.pszFilename = pszFilename;
/*
* Do the file level filtering.
*/
if ( pBaseSettings->pszFilterFiles
&& *pBaseSettings->pszFilterFiles
&& !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
{
ScmVerbose(NULL, 5, "file filter mismatch: \"%s\"\n", pszFilename);
return VINF_SUCCESS;
}
if ( pBaseSettings->pszFilterOutFiles
&& *pBaseSettings->pszFilterOutFiles
&& ( RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
|| RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
{
ScmVerbose(NULL, 5, "file filter out: \"%s\"\n", pszFilename);
return VINF_SUCCESS;
}
/*
* Try find a matching rewrite config for this filename.
*/
PCSCMCFGENTRY pCfg = NULL;
for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
{
pCfg = &g_aConfigs[iCfg];
break;
}
if (!pCfg)
{
ScmVerbose(NULL, 4, "No rewriters configured for \"%s\"\n", pszFilename);
return VINF_SUCCESS;
}
ScmVerbose(&State, 4, "matched \"%s\"\n", pCfg->pszFilePattern);
/*
* Create an input stream from the file and check that it's text.
*/
SCMSTREAM Stream1;
int rc = ScmStreamInitForReading(&Stream1, pszFilename);
if (RT_FAILURE(rc))
{
RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
return rc;
}
if (ScmStreamIsText(&Stream1))
{
ScmVerbose(&State, 3, NULL);
/*
* Gather SCM and editor settings from the stream.
*/
rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
if (RT_SUCCESS(rc))
{
ScmStreamRewindForReading(&Stream1);
/*
* Create two more streams for output and push the text thru all the
* rewriters, switching the two streams around when something is
* actually rewritten. Stream1 remains unchanged.
*/
SCMSTREAM Stream2;
rc = ScmStreamInitForWriting(&Stream2, &Stream1);
if (RT_SUCCESS(rc))
{
SCMSTREAM Stream3;
rc = ScmStreamInitForWriting(&Stream3, &Stream1);
if (RT_SUCCESS(rc))
{
bool fModified = false;
PSCMSTREAM pIn = &Stream1;
PSCMSTREAM pOut = &Stream2;
for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
{
bool fRc = pCfg->papfnRewriter[iRw](&State, pIn, pOut, pBaseSettings);
if (fRc)
{
PSCMSTREAM pTmp = pOut;
pOut = pIn == &Stream1 ? &Stream3 : pIn;
pIn = pTmp;
fModified = true;
}
ScmStreamRewindForReading(pIn);
ScmStreamRewindForWriting(pOut);
}
/*
* If rewritten, write it back to disk.
*/
if (fModified)
{
if (!g_fDryRun)
{
ScmVerbose(&State, 1, "Writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
if (RT_FAILURE(rc))
RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
}
else
{
ScmVerbose(&State, 1, NULL);
ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol, g_fDiffIgnoreLeadingWS,
g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars, pBaseSettings->cchTab, g_pStdOut);
ScmVerbose(&State, 3, "Would have modified the file \"%s%s\"\n", pszFilename, g_pszChangedSuff);
}
}
else
ScmVerbose(&State, 3, "No change\n", pszFilename);
ScmStreamDelete(&Stream3);
}
else
RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
ScmStreamDelete(&Stream2);
}
else
RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
}
else
RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
}
else
ScmVerbose(&State, 4, "not text file: \"%s\"\n", pszFilename);
ScmStreamDelete(&Stream1);
return rc;
}
/**
* Processes a file.
*
* This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
* directory recursion method.
*
* @returns IPRT status code.
* @param pszFilename The file name.
* @param pszBasename The base name (pointer within @a pszFilename).
* @param cchBasename The length of the base name. (For passing to
* RTStrSimplePatternMultiMatch.)
* @param pSettingsStack The settings stack (pointer to the top element).
*/
static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
PSCMSETTINGS pSettingsStack)
{
SCMSETTINGSBASE Base;
int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
if (RT_SUCCESS(rc))
{
rc = scmProcessFileInner(pszFilename, pszBasename, cchBasename, &Base);
scmSettingsBaseDelete(&Base);
}
return rc;
}
/**
* Tries to correct RTDIRENTRY_UNKNOWN.
*
* @returns Corrected type.
* @param pszPath The path to the object in question.
*/
static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
{
RTFSOBJINFO Info;
int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
if (RT_FAILURE(rc))
return RTDIRENTRYTYPE_UNKNOWN;
if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
return RTDIRENTRYTYPE_DIRECTORY;
if (RTFS_IS_FILE(Info.Attr.fMode))
return RTDIRENTRYTYPE_FILE;
return RTDIRENTRYTYPE_UNKNOWN;
}
/**
* Recurse into a sub-directory and process all the files and directories.
*
* @returns IPRT status code.
* @param pszBuf Path buffer containing the directory path on
* entry. This ends with a dot. This is passed
* along when recusing in order to save stack space
* and avoid needless copying.
* @param cchDir Length of our path in pszbuf.
* @param pEntry Directory entry buffer. This is also passed
* along when recursing to save stack space.
* @param pSettingsStack The settings stack (pointer to the top element).
* @param iRecursion The recursion depth. This is used to restrict
* the recursions.
*/
static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
PSCMSETTINGS pSettingsStack, unsigned iRecursion)
{
Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
/*
* Make sure we stop somewhere.
*/
if (iRecursion > 128)
{
RTMsgError("recursion too deep: %d\n", iRecursion);
return VINF_SUCCESS; /* ignore */
}
/*
* Try open and read the directory.
*/
PRTDIR pDir;
int rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE);
if (RT_FAILURE(rc))
{
RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
return rc;
}
for (;;)
{
/* Read the next entry. */
rc = RTDirRead(pDir, pEntry, NULL);
if (RT_FAILURE(rc) && rc != VERR_NO_MORE_FILES)
RTMsgError("RTDirRead -> %Rrc\n", rc);
if (RT_FAILURE(rc))
break;
/* Skip '.' and '..'. */
if ( pEntry->szName[0] == '.'
&& ( pEntry->cbName == 1
|| ( pEntry->cbName == 2
&& pEntry->szName[1] == '.')))
continue;
/* Enter it into the buffer so we've got a full name to work
with when needed. */
if (pEntry->cbName + cchDir >= RTPATH_MAX)
{
RTMsgError("Skipping too long entry: %s", pEntry->szName);
continue;
}
memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
/* Figure the type. */
RTDIRENTRYTYPE enmType = pEntry->enmType;
if (enmType == RTDIRENTRYTYPE_UNKNOWN)
enmType = scmFigureUnknownType(pszBuf);
/* Process the file or directory, skip the rest. */
if (enmType == RTDIRENTRYTYPE_FILE)
rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
{
/* Append the dot for the benefit of the pattern matching. */
if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
{
RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
continue;
}
memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
if ( !pSettingsStack->Base.pszFilterOutDirs
|| !*pSettingsStack->Base.pszFilterOutDirs
|| ( !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
pEntry->szName, pEntry->cbName, NULL)
&& !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
pszBuf, cchSubDir, NULL)
)
)
{
rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
if (RT_SUCCESS(rc))
{
rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
scmSettingsStackPopAndDestroy(&pSettingsStack);
}
}
}
if (RT_FAILURE(rc))
break;
}
RTDirClose(pDir);
return RT_SUCCESS(rc) ? 0 : 1;
}
/**
* Process a directory tree.
*
* @returns IPRT status code.
* @param pszDir The directory to start with. This is pointer to
* a RTPATH_MAX sized buffer.
*/
static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
{
/*
* Setup the recursion.
*/
int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
if (RT_SUCCESS(rc))
{
RTDIRENTRY Entry;
rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
}
else
RTMsgError("RTPathAppend: %Rrc\n", rc);
return rc;
}
/**
* Processes a file or directory specified as an command line argument.
*
* @returns IPRT status code
* @param pszSomething What we found in the commad line arguments.
* @param pSettingsStack The settings stack (pointer to the top element).
*/
static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
{
char szBuf[RTPATH_MAX];
int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
if (RT_SUCCESS(rc))
{
PSCMSETTINGS pSettings;
rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
if (RT_SUCCESS(rc))
{
scmSettingsStackPush(&pSettingsStack, pSettings);
if (RTFileExists(szBuf))
{
const char *pszBasename = RTPathFilename(szBuf);
if (pszBasename)
{
size_t cchBasename = strlen(pszBasename);
rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
}
else
{
RTMsgError("RTPathFilename: NULL\n");
rc = VERR_IS_A_DIRECTORY;
}
}
else
rc = scmProcessDirTree(szBuf, pSettingsStack);
PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
Assert(pPopped == pSettings);
scmSettingsDestroy(pSettings);
}
else
RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
}
else
RTMsgError("RTPathAbs: %Rrc\n", rc);
return rc;
}
int main(int argc, char **argv)
{
int rc = RTR3Init();
if (RT_FAILURE(rc))
return 1;
/*
* Init the settings.
*/
PSCMSETTINGS pSettings;
rc = scmSettingsCreate(&pSettings, &g_Defaults);
if (RT_FAILURE(rc))
{
RTMsgError("scmSettingsCreate: %Rrc\n", rc);
return 1;
}
/*
* Parse arguments and process input in order (because this is the only
* thing that works at the moment).
*/
static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
{
{ "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
{ "--real-run", 'D', RTGETOPT_REQ_NOTHING },
{ "--file-filter", 'f', RTGETOPT_REQ_STRING },
{ "--quiet", 'q', RTGETOPT_REQ_NOTHING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
{ "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
};
memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetOptState;
rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
AssertReleaseRCReturn(rc, 1);
size_t cProcessed = 0;
while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
{
switch (rc)
{
case 'd':
g_fDryRun = true;
break;
case 'D':
g_fDryRun = false;
break;
case 'f':
g_pszFileFilter = ValueUnion.psz;
break;
case 'h':
RTPrintf("Source Code Massager\n"
"\n"
"Usage: %s [options] <files & dirs>\n"
"\n"
"Options:\n", g_szProgName);
for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
{
bool fAdvanceTwo = false;
if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
{
if ((fAdvanceTwo = i + 1 < RT_ELEMENTS(s_aOpts) && strstr(s_aOpts[i+1].pszLong, "-no-") != NULL))
RTPrintf(" %s, %s\n", s_aOpts[i].pszLong, s_aOpts[i + 1].pszLong);
else
RTPrintf(" %s\n", s_aOpts[i].pszLong);
}
else if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
RTPrintf(" %s string\n", s_aOpts[i].pszLong);
else
RTPrintf(" %s value\n", s_aOpts[i].pszLong);
switch (s_aOpts[i].iShort)
{
case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break;
case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break;
case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break;
case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break;
case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break;
case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break;
}
i += fAdvanceTwo;
}
return 1;
case 'q':
g_iVerbosity = 0;
break;
case 'v':
g_iVerbosity++;
break;
case 'V':
{
/* The following is assuming that svn does it's job here. */
static const char s_szRev[] = "$Revision$";
const char *psz = RTStrStripL(strchr(s_szRev, ' '));
RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
return 0;
}
case SCMOPT_DIFF_IGNORE_EOL:
g_fDiffIgnoreEol = true;
break;
case SCMOPT_DIFF_NO_IGNORE_EOL:
g_fDiffIgnoreEol = false;
break;
case SCMOPT_DIFF_IGNORE_SPACE:
g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
break;
case SCMOPT_DIFF_NO_IGNORE_SPACE:
g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
break;
case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
g_fDiffIgnoreLeadingWS = true;
break;
case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
g_fDiffIgnoreLeadingWS = false;
break;
case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
g_fDiffIgnoreTrailingWS = true;
break;
case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
g_fDiffIgnoreTrailingWS = false;
break;
case SCMOPT_DIFF_SPECIAL_CHARS:
g_fDiffSpecialChars = true;
break;
case SCMOPT_DIFF_NO_SPECIAL_CHARS:
g_fDiffSpecialChars = false;
break;
case VINF_GETOPT_NOT_OPTION:
{
if (!g_fDryRun)
{
if (!cProcessed)
{
RTPrintf("%s: Warning! This program will make changes to your source files and\n"
"%s: there is a slight risk that bugs or a full disk may cause\n"
"%s: LOSS OF DATA. So, please make sure you have checked in\n"
"%s: all your changes already. If you didn't, then don't blame\n"
"%s: anyone for not warning you!\n"
"%s:\n"
"%s: Press any key to continue...\n",
g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
g_szProgName, g_szProgName);
RTStrmGetCh(g_pStdIn);
}
cProcessed++;
}
rc = scmProcessSomething(ValueUnion.psz, pSettings);
if (RT_FAILURE(rc))
return rc;
break;
}
default:
{
int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion);
if (RT_SUCCESS(rc2))
break;
if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
return 2;
return RTGetOptPrintError(rc, &ValueUnion);
}
}
}
scmSettingsDestroy(pSettings);
return 0;
}