IOobject.C 12.2 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6
7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2011-2017 OpenFOAM Foundation
9
    Copyright (C) 2016-2020 OpenCFD Ltd.
10
11
12
13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14
15
16
17
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
18
19
20
21
22
23
24

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26
27
28
29
30
31
32
33
34

\*---------------------------------------------------------------------------*/

#include "IOobject.H"
#include "Time.H"
#include "IFstream.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

35
36
namespace Foam
{
37
    defineTypeNameAndDebug(IOobject, 0);
38
39
}

40
41
42
char Foam::IOobject::scopeSeparator
(
    #ifdef _WIN32
43
    // Windows: using ':' causes scoping conflicts with d:/path etc
44
45
46
47
48
49
50
    Foam::debug::infoSwitch("scopeSeparator", '_')
    #else
    Foam::debug::infoSwitch("scopeSeparator", ':')
    #endif
);


51
52
53
54
55
const Foam::Enum
<
    Foam::IOobject::fileCheckTypes
>
Foam::IOobject::fileCheckTypesNames
Mark Olesen's avatar
Mark Olesen committed
56
({
57
58
59
60
    { fileCheckTypes::timeStamp, "timeStamp" },
    { fileCheckTypes::timeStampMaster, "timeStampMaster" },
    { fileCheckTypes::inotify, "inotify" },
    { fileCheckTypes::inotifyMaster, "inotifyMaster" },
Mark Olesen's avatar
Mark Olesen committed
61
});
62
63
64
65
66


// Default fileCheck type
Foam::IOobject::fileCheckTypes Foam::IOobject::fileModificationChecking
(
Mark Olesen's avatar
Mark Olesen committed
67
    fileCheckTypesNames.get
68
    (
69
70
        "fileModificationChecking",
        debug::optimisationSwitches()
71
72
73
    )
);

74

75
//! \cond file-scope
76
77
78
79
80
81
82
83
84
namespace Foam
{
    // Register re-reader
    class addfileModificationCheckingToOpt
    :
        public ::Foam::simpleRegIOobject
    {
    public:

85
86
87
88
89
90
91
        addfileModificationCheckingToOpt
            (const addfileModificationCheckingToOpt&) = delete;

        void operator=
            (const addfileModificationCheckingToOpt&) = delete;

        explicit addfileModificationCheckingToOpt(const char* name)
92
93
94
95
        :
            ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
        {}

96
        virtual ~addfileModificationCheckingToOpt() = default;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

        virtual void readData(Foam::Istream& is)
        {
            IOobject::fileModificationChecking =
                IOobject::fileCheckTypesNames.read(is);
        }

        virtual void writeData(Foam::Ostream& os) const
        {
            os <<  IOobject::fileCheckTypesNames
                [IOobject::fileModificationChecking];
        }
    };

    addfileModificationCheckingToOpt addfileModificationCheckingToOpt_
    (
        "fileModificationChecking"
    );
115
116
117

} // End namespace Foam
//! \endcond
118

119

120
121
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //

william's avatar
william committed
122
bool Foam::IOobject::fileNameComponents
123
(
124
    const fileName& path,
125
126
127
128
129
    fileName& instance,
    fileName& local,
    word& name
)
{
130
    // Convert explicit relative file-system path to absolute file-system path.
131
    if (path.starts_with("./") || path.starts_with("../"))
132
133
134
135
136
137
    {
        fileName absPath = cwd()/path;
        absPath.clean();

        return fileNameComponents(absPath, instance, local, name);
    }
138

139
140
141
142
    instance.clear();
    local.clear();
    name.clear();

143
    // Called with directory
144
    if (isDir(path))
145
    {
146
147
        WarningInFunction
            << " called with directory: " << path << endl;
148

149
150
151
        return false;
    }

152
153
154
155
156
157
158
159
160
161
162
    const auto first = path.find('/');
    const auto last  = path.rfind('/');

    // The raw length of name (without validating for word chars)
    auto nameLen = path.size();

    if (first == std::string::npos)
    {
        // No '/' found (or empty entirely)
        // => no instance or local

163
        name = word::validate(path);
164
    }
165
166
167
168
169
170
171
    else if
    (
        first == 0
        #ifdef _WIN32
     || (first == 2 && path[1] == ':')  // Eg, d:/path
        #endif
    )
172
    {
173
        // Absolute path (starts with '/' or 'd:/')
174
175
        // => no local

176
        instance = path.substr(0, last);
177

178
179
        const std::string ending = path.substr(last+1);
        nameLen = ending.size();  // The raw length of name
180
        name = word::validate(ending);
181
    }
182
183
    else
    {
184
185
186
        // Normal case.
        // First part is instance, remainder is local
        instance = path.substr(0, first);
187

188
        if (last > first)
189
        {
190
191
            // With local
            local = path.substr(first+1, last-first-1);
192
        }
193

194
195
        const std::string ending = path.substr(last+1);
        nameLen = ending.size();  // The raw length of name
196
        name = word::validate(ending);
197
198
    }

199
    // Check for valid (and stripped) name, regardless of the debug level
200
    if (!nameLen || nameLen != name.size())
201
    {
202
        WarningInFunction
203
            << "has invalid word for name: \"" << name
204
205
            << "\"\nwhile processing path: " << path << endl;

206
207
208
209
210
211
212
        return false;
    }

    return true;
}


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
Foam::IOobject Foam::IOobject::selectIO
(
    const IOobject& io,
    const fileName& altFile,
    const word& ioName
)
{
    if (altFile.empty())
    {
        return io;
    }

    // Construct from file path instead

    fileName altPath = altFile;

    if (isDir(altPath))
    {
        // Resolve directories as well

        if (ioName.empty())
        {
            altPath /= io.name();
        }
        else
        {
            altPath /= ioName;
        }
    }
    altPath.expand();


    return
        IOobject
        (
            altPath,
            io.db(),
            io.readOpt(),
            io.writeOpt(),
            io.registerObject(),
            io.globalObject()
        );
}


258
259
Foam::word Foam::IOobject::group(const word& name)
{
260
    const auto i = name.rfind('.');
261

262
    if (i == std::string::npos || i == 0)
263
264
265
    {
        return word::null;
    }
266
267

    return name.substr(i+1);
268
269
270
271
272
}


Foam::word Foam::IOobject::member(const word& name)
{
273
    const auto i = name.rfind('.');
274

275
    if (i == std::string::npos || i == 0)
276
277
278
    {
        return name;
    }
279
280

    return name.substr(0, i);
281
282
283
}


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::IOobject::IOobject
(
    const word& name,
    const fileName& instance,
    const objectRegistry& registry,
    readOption ro,
    writeOption wo,
    bool registerObject
)
:
    name_(name),
    headerClassName_(typeName),
    note_(),
    instance_(instance),
    local_(),
    db_(registry),
    rOpt_(ro),
    wOpt_(wo),
    registerObject_(registerObject),
305
    globalObject_(false),
306
    objState_(GOOD),
307
308
    labelByteSize_(sizeof(label)),
    scalarByteSize_(sizeof(scalar))
309
310
311
{
    if (objectRegistry::debug)
    {
Henry Weller's avatar
Henry Weller committed
312
313
        InfoInFunction
            << "Constructing IOobject called " << name_
314
315
316
317
318
319
320
321
322
323
324
325
326
327
            << " of type " << headerClassName_
            << endl;
    }
}


Foam::IOobject::IOobject
(
    const word& name,
    const fileName& instance,
    const fileName& local,
    const objectRegistry& registry,
    readOption ro,
    writeOption wo,
328
329
    bool registerObject,
    bool globalObject
330
331
332
333
334
335
336
337
338
339
340
)
:
    name_(name),
    headerClassName_(typeName),
    note_(),
    instance_(instance),
    local_(local),
    db_(registry),
    rOpt_(ro),
    wOpt_(wo),
    registerObject_(registerObject),
341
    globalObject_(globalObject),
342
    objState_(GOOD),
343
344
    labelByteSize_(sizeof(label)),
    scalarByteSize_(sizeof(scalar))
345
346
347
{
    if (objectRegistry::debug)
    {
Henry Weller's avatar
Henry Weller committed
348
349
        InfoInFunction
            << "Constructing IOobject called " << name_
350
351
352
353
354
355
            << " of type " << headerClassName_
            << endl;
    }
}


356
357
358
359
360
361
Foam::IOobject::IOobject
(
    const fileName& path,
    const objectRegistry& registry,
    readOption ro,
    writeOption wo,
362
363
    bool registerObject,
    bool globalObject
364
365
366
367
368
369
370
371
372
373
374
)
:
    name_(),
    headerClassName_(typeName),
    note_(),
    instance_(),
    local_(),
    db_(registry),
    rOpt_(ro),
    wOpt_(wo),
    registerObject_(registerObject),
375
    globalObject_(globalObject),
376
    objState_(GOOD),
377
378
    labelByteSize_(sizeof(label)),
    scalarByteSize_(sizeof(scalar))
379
{
380
381
    if (!fileNameComponents(path, instance_, local_, name_))
    {
382
        FatalErrorInFunction
383
            << " invalid path specification"
384
385
            << exit(FatalError);
    }
386
387
388

    if (objectRegistry::debug)
    {
Henry Weller's avatar
Henry Weller committed
389
390
        InfoInFunction
            << "Constructing IOobject called " << name_
391
392
393
394
395
396
            << " of type " << headerClassName_
            << endl;
    }
}


397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
Foam::IOobject::IOobject
(
    const IOobject& io,
    const objectRegistry& registry
)
:
    name_(io.name_),
    headerClassName_(io.headerClassName_),
    note_(io.note_),
    instance_(io.instance_),
    local_(io.local_),
    db_(registry),
    rOpt_(io.rOpt_),
    wOpt_(io.wOpt_),
    registerObject_(io.registerObject_),
    globalObject_(io.globalObject_),
413
414
415
    objState_(io.objState_),
    labelByteSize_(io.labelByteSize_),
    scalarByteSize_(io.scalarByteSize_)
416
417
418
{}


419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
Foam::IOobject::IOobject
(
    const IOobject& io,
    const word& name
)
:
    name_(name),
    headerClassName_(io.headerClassName_),
    note_(io.note_),
    instance_(io.instance_),
    local_(io.local_),
    db_(io.db_),
    rOpt_(io.rOpt_),
    wOpt_(io.wOpt_),
    registerObject_(io.registerObject_),
434
    globalObject_(io.globalObject_),
435
436
437
    objState_(io.objState_),
    labelByteSize_(io.labelByteSize_),
    scalarByteSize_(io.scalarByteSize_)
438
439
440
{}


441
442
443
444
445
446
447
448
449
450
451
452
453
454
// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

const Foam::objectRegistry& Foam::IOobject::db() const
{
    return db_;
}


const Foam::Time& Foam::IOobject::time() const
{
    return db_.time();
}


455
456
457
458
459
460
const Foam::fileName& Foam::IOobject::rootPath() const
{
    return time().rootPath();
}


461
462
463
464
465
466
467
468
const Foam::fileName& Foam::IOobject::caseName() const
{
    return time().caseName();
}


Foam::fileName Foam::IOobject::path() const
{
469
    // A file is 'outside' of the case if it has been specified using an
470
    // absolute path
471

472
473
474
475
476
477
478
479
480
    const auto first = instance().find('/');

    if
    (
        first == 0
        #ifdef _WIN32
     || (first == 2 && instance()[1] == ':')  // Eg, d:/path
        #endif
    )
481
    {
482
        // Absolute path (starts with '/' or 'd:/')
483
484
        return instance();
    }
485
486

    return rootPath()/caseName()/instance()/db_.dbDir()/local();
487
488
489
490
491
492
493
494
495
}


Foam::fileName Foam::IOobject::path
(
    const word& instance,
    const fileName& local
) const
{
496
    // Note: can only be called with relative instance since is word type
497
498
499
500
    return rootPath()/caseName()/instance/db_.dbDir()/local;
}


501
502
503
504
505
Foam::fileName Foam::IOobject::localFilePath
(
    const word& typeName,
    const bool search
) const
506
{
507
508
    // Do not check for undecomposed files
    return fileHandler().filePath(false, *this, typeName, search);
509
510
511
}


512
513
514
515
516
Foam::fileName Foam::IOobject::globalFilePath
(
    const word& typeName,
    const bool search
) const
517
{
518
519
    // Check for undecomposed files
    return fileHandler().filePath(true, *this, typeName, search);
520
521
522
523
524
525
526
}


void Foam::IOobject::setBad(const string& s)
{
    if (objState_ != GOOD)
    {
527
        FatalErrorInFunction
Henry Weller's avatar
Henry Weller committed
528
            << "Recurrent failure for object " << s
529
530
531
532
533
            << exit(FatalError);
    }

    if (error::level)
    {
Henry Weller's avatar
Henry Weller committed
534
535
        InfoInFunction
            << "Broken object " << s << info() << endl;
536
537
538
539
540
541
    }

    objState_ = BAD;
}


542
543
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

544
545
546
547
548
549
550
551
552
void Foam::IOobject::operator=(const IOobject& io)
{
    name_ = io.name_;
    headerClassName_ = io.headerClassName_;
    note_ = io.note_;
    instance_ = io.instance_;
    local_ = io.local_;
    rOpt_ = io.rOpt_;
    wOpt_ = io.wOpt_;
553
    globalObject_ = io.globalObject_;
554
    objState_ = io.objState_;
555
556
    labelByteSize_ = io.labelByteSize_;
    scalarByteSize_ = io.scalarByteSize_;
557
558
559
560
}


// ************************************************************************* //