NameSimplifier.C 9.37 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2014, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39 40
#include <NameSimplifier.h>
#include <maptypes.h>
hrchilds's avatar
hrchilds committed
41 42 43 44
#include <Utility.h>

using std::string;
using std::vector;
hrchilds's avatar
hrchilds committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

// ****************************************************************************
// Method: NameSimplifier::NameSimplifier
//
// Purpose: 
//   Constructor for the NameSimplifier class.
//
// Programmer: Brad Whitlock
// Creation:   Wed Feb 4 13:46:35 PST 2004
//
// Modifications:
//   
// ****************************************************************************

NameSimplifier::NameSimplifier() : names()
{
}

// ****************************************************************************
// Method: NameSimplifier::~NameSimplifier
//
// Purpose: 
//   Destructor for the NameSimplifier class.
//
// Programmer: Brad Whitlock
// Creation:   Wed Feb 4 13:46:51 PST 2004
//
// Modifications:
//   
// ****************************************************************************

NameSimplifier::~NameSimplifier()
{
}

// ****************************************************************************
// Method: NameSimplifier::AddName
//
// Purpose: 
//   Adds a name to be simplified.
//
// Arguments:
//   name : The name to simplify.
//
// Programmer: Brad Whitlock
// Creation:   Wed Feb 4 13:47:12 PST 2004
//
// Modifications:
hrchilds's avatar
hrchilds committed
93 94 95
//    Jeremy Meredith, Tue Apr  6 12:19:51 PDT 2004
//    Made use of the UniqueFileName construct here.
//
hrchilds's avatar
hrchilds committed
96 97 98 99 100
// ****************************************************************************

void
NameSimplifier::AddName(const std::string &n)
{
hrchilds's avatar
hrchilds committed
101
    AddName(QualifiedFilename(n));
hrchilds's avatar
hrchilds committed
102 103 104 105 106
}

void
NameSimplifier::AddName(const QualifiedFilename &n)
{
hrchilds's avatar
hrchilds committed
107 108 109 110 111 112 113
    int newindex = names.size();
    names.push_back(UniqueFileName(n));

    for (int j=0; j<newindex; j++)
    {
        UniqueFileName::Uniquify(names[newindex],names[j]);
    }
hrchilds's avatar
hrchilds committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

// ****************************************************************************
// Method: NameSimplifier::ClearNames
//
// Purpose: 
//   Clears out the name list.
//
// Programmer: Brad Whitlock
// Creation:   Wed Feb 4 13:47:45 PST 2004
//
// Modifications:
//   
// ****************************************************************************

void
NameSimplifier::ClearNames()
{
    names.clear();
}

hrchilds's avatar
hrchilds committed
135 136 137 138 139 140 141 142 143 144

// ****************************************************************************
//  Constructor:  NameSimplifier::UniqueFileName::UniqueFileName
//
//  Arguments:
//    qfn        a QualifiedFilename to convert
//
//  Programmer:  Jeremy Meredith
//  Creation:    April  6, 2004
//
hrchilds's avatar
hrchilds committed
145 146 147 148
//  Modifications:
//    Jeremy Meredith, Wed Apr  7 12:12:48 PDT 2004
//    Account for the fact that not all input names had full paths.
//
hrchilds's avatar
hrchilds committed
149 150 151 152 153 154 155 156 157 158
// ****************************************************************************

NameSimplifier::UniqueFileName::UniqueFileName(const QualifiedFilename &qfn)
{
    useHost = false;
    pathCount = 0;

    host = qfn.host;
    file = qfn.filename;
    separator = qfn.separator;
hrchilds's avatar
hrchilds committed
159
    leadingSlash = (qfn.path.size() > 0 && qfn.path[0] == separator);
hrchilds's avatar
hrchilds committed
160
    path = SplitValues(qfn.path, separator);
bonnell's avatar
bonnell committed
161
    pathLen = (int)path.size();
hrchilds's avatar
hrchilds committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
}


// ****************************************************************************
//  Method:  NameSimplifier::UniqueFileName::GetAsString
//
//  Purpose:
//    Convert into a single string
//
//  Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    April  6, 2004
//
hrchilds's avatar
hrchilds committed
177 178 179 180
//  Modifications:
//    Jeremy Meredith, Wed Apr  7 12:12:48 PDT 2004
//    Account for the fact that not all input names had full paths.
//
hrchilds's avatar
hrchilds committed
181 182 183 184 185 186 187 188
// ****************************************************************************

string
NameSimplifier::UniqueFileName::GetAsString() const
{
    string out;
    if (useHost)
        out += host + ":";
hrchilds's avatar
hrchilds committed
189
    if (leadingSlash && pathCount == pathLen)
hrchilds's avatar
hrchilds committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        out += separator;
    for (int i=0; i<pathCount; i++)
        out += path[pathLen-pathCount + i] + separator;
    out += file;
    return out;
}


// ****************************************************************************
//  Method:  NameSimplifier::UniqueFileName::Unique
//
//  Purpose:
//    Returns true if we can consider the representations of two
//    file names unique.
//
//  Arguments:
//    a,b        the UniqueFileNames to test
//
//  Note:  This is definitely not an exact equality test.
//
//  Programmer:  Jeremy Meredith
//  Creation:    April  6, 2004
//
// ****************************************************************************

bool
NameSimplifier::UniqueFileName::Unique(const UniqueFileName &a,
                                       const UniqueFileName &b)
{
    string as = a.GetAsString();
    string bs = b.GetAsString();
    int    al = as.length();
    int    bl = bs.length();

    // Ensure that a string does not exactly match
    // the ending piece of the other one
    if (as == bs)
        return false;
    else if ((al < bl) && (as == bs.substr(bl - al)))
        return false;
    else if ((bl < al) && (bs == as.substr(al - bl)))
        return false;
    else
        return true;
}


// ****************************************************************************
//  Method:  NameSimplifier::UniqueFileName::Uniquify
//
//  Purpose:
//    Make two file names unique
//
//  Arguments:
//    a,b        the file names to make unique
//
//  Programmer:  Jeremy Meredith
//  Creation:    April  6, 2004
//
// ****************************************************************************

void
NameSimplifier::UniqueFileName::Uniquify(UniqueFileName &a, UniqueFileName &b)
{
    // Filenames are often unique already
    if (Unique(a, b))
        return;

    // If we can make them unique by simply adding a host prefix, do it
    if (a.host != b.host)
    {
        a.useHost = true;
        b.useHost = true;
        // They should now be unique since our host name is prepended.
        // Test again for sanity, and return now that they are unique.
        if (Unique(a, b))
            return;
    }

    // If two filenames are the same on the same host, we need at 
    // least one path prefix for both
    if (a.pathCount == 0 && b.pathCount != 0 && a.pathLen > 0)
        a.pathCount++;
    if (b.pathCount == 0 && a.pathCount != 0 && b.pathLen > 0)
        b.pathCount++;

    // Start adding more path prefix segments as needed
    while (!Unique(a,b) &&
           (a.pathCount < a.pathLen || b.pathCount < b.pathLen))
    {
        // Add to the shorter one, or add to both if they are equal
        if (a.pathCount < b.pathCount && a.pathCount < a.pathLen)
            a.pathCount++;
        else if (b.pathCount < a.pathCount && b.pathCount < b.pathLen)
            b.pathCount++;
        else
        {
            if (a.pathCount < a.pathLen)
                a.pathCount++;
            if (b.pathCount < b.pathLen)
                b.pathCount++;
        }
    }
}

hrchilds's avatar
hrchilds committed
295 296 297 298 299
// ****************************************************************************
// Method: NameSimplifier::GetSimplifiedNames
//
// Purpose: 
//   Returns a stringVector of simplified names.
hrchilds's avatar
hrchilds committed
300
//   Uniqueness is now guaranteed as names are added.
hrchilds's avatar
hrchilds committed
301 302 303 304 305 306
//
// Arguments:
//   n : The return string vector.
//
// Returns:    
//
hrchilds's avatar
hrchilds committed
307 308
// Programmer: Jeremy Meredith
// Creation:   April  6, 2004
hrchilds's avatar
hrchilds committed
309 310 311 312 313 314 315 316 317 318
//
// Modifications:
//   
// ****************************************************************************

void
NameSimplifier::GetSimplifiedNames(stringVector &n) const
{
    n.clear();

319
    for (size_t i=0; i<names.size(); i++)
hrchilds's avatar
hrchilds committed
320
    {
hrchilds's avatar
hrchilds committed
321
        n.push_back(names[i].GetAsString());
hrchilds's avatar
hrchilds committed
322 323
    }
}