IOobject.C 10.2 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
7
8
9
10
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28
29
30
31

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

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

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

32
33
namespace Foam
{
34
    defineTypeNameAndDebug(IOobject, 0);
35
36
}

37
38
39
40
41
42
43
44
45
46
47
const Foam::Enum
<
    Foam::IOobject::fileCheckTypes
>
Foam::IOobject::fileCheckTypesNames
{
    { fileCheckTypes::timeStamp, "timeStamp" },
    { fileCheckTypes::timeStampMaster, "timeStampMaster" },
    { fileCheckTypes::inotify, "inotify" },
    { fileCheckTypes::inotifyMaster, "inotifyMaster" },
};
48
49
50
51
52


// Default fileCheck type
Foam::IOobject::fileCheckTypes Foam::IOobject::fileModificationChecking
(
53
    fileCheckTypesNames.lookup
54
    (
55
56
        "fileModificationChecking",
        debug::optimisationSwitches()
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
93
94
namespace Foam
{
    // Register re-reader
    class addfileModificationCheckingToOpt
    :
        public ::Foam::simpleRegIOobject
    {
    public:

        addfileModificationCheckingToOpt(const char* name)
        :
            ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
        {}

        virtual ~addfileModificationCheckingToOpt()
        {}

        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"
    );
95
96
}

97

98
99
100
101
102
103
104
105
106
107
108
// file-scope
//
// A file is 'outside' of the case if it has been specified using an
// absolute path (starts with '/')
//
static inline bool isOutsideOfCase(const std::string& file)
{
    return !file.empty() && file[0] == '/';
}


109
110
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //

william's avatar
william committed
111
bool Foam::IOobject::fileNameComponents
112
(
113
    const fileName& path,
114
115
116
117
118
    fileName& instance,
    fileName& local,
    word& name
)
{
119
120
121
122
123
124
125
126
    // Convert explicit relative file-system path to absolute file-system path.
    if (path.startsWith("./") || path.startsWith("../"))
    {
        fileName absPath = cwd()/path;
        absPath.clean();

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

128
129
130
131
    instance.clear();
    local.clear();
    name.clear();

132
    // Called with directory
133
    if (isDir(path))
134
    {
135
136
        WarningInFunction
            << " called with directory: " << path << endl;
137

138
139
140
        return false;
    }

141
142
143
144
145
146
147
148
149
150
151
    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

152
        name = word::validate(path);
153
154
    }
    else if (first == 0)
155
    {
156
157
158
        // Absolute path (starts with '/')
        // => no local

159
160
        instance = path.substr(0, last);

161
162
        const std::string ending = path.substr(last+1);
        nameLen = ending.size();  // The raw length of name
163
        name = word::validate(ending);
164
    }
165
166
    else
    {
167
168
169
        // Normal case.
        // First part is instance, remainder is local
        instance = path.substr(0, first);
170

171
        if (last > first)
172
        {
173
174
            // With local
            local = path.substr(first+1, last-first-1);
175
        }
176

177
178
        const std::string ending = path.substr(last+1);
        nameLen = ending.size();  // The raw length of name
179
        name = word::validate(ending);
180
181
    }

182
    // Check for valid (and stripped) name, regardless of the debug level
183
    if (!nameLen || nameLen != name.size())
184
    {
185
        WarningInFunction
186
            << "has invalid word for name: \"" << name
187
188
            << "\"\nwhile processing path: " << path << endl;

189
190
191
192
193
194
195
        return false;
    }

    return true;
}


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// * * * * * * * * * * * * * * * * 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),
217
    globalObject_(false),
218
219
220
221
    objState_(GOOD)
{
    if (objectRegistry::debug)
    {
Henry Weller's avatar
Henry Weller committed
222
223
        InfoInFunction
            << "Constructing IOobject called " << name_
224
225
226
227
228
229
230
231
232
233
234
235
236
237
            << " of type " << headerClassName_
            << endl;
    }
}


Foam::IOobject::IOobject
(
    const word& name,
    const fileName& instance,
    const fileName& local,
    const objectRegistry& registry,
    readOption ro,
    writeOption wo,
238
239
    bool registerObject,
    bool globalObject
240
241
242
243
244
245
246
247
248
249
250
)
:
    name_(name),
    headerClassName_(typeName),
    note_(),
    instance_(instance),
    local_(local),
    db_(registry),
    rOpt_(ro),
    wOpt_(wo),
    registerObject_(registerObject),
251
    globalObject_(globalObject),
252
253
254
255
    objState_(GOOD)
{
    if (objectRegistry::debug)
    {
Henry Weller's avatar
Henry Weller committed
256
257
        InfoInFunction
            << "Constructing IOobject called " << name_
258
259
260
261
262
263
            << " of type " << headerClassName_
            << endl;
    }
}


264
265
266
267
268
269
Foam::IOobject::IOobject
(
    const fileName& path,
    const objectRegistry& registry,
    readOption ro,
    writeOption wo,
270
271
    bool registerObject,
    bool globalObject
272
273
274
275
276
277
278
279
280
281
282
)
:
    name_(),
    headerClassName_(typeName),
    note_(),
    instance_(),
    local_(),
    db_(registry),
    rOpt_(ro),
    wOpt_(wo),
    registerObject_(registerObject),
283
    globalObject_(globalObject),
284
285
    objState_(GOOD)
{
286
287
    if (!fileNameComponents(path, instance_, local_, name_))
    {
288
        FatalErrorInFunction
289
            << " invalid path specification"
290
291
            << exit(FatalError);
    }
292
293
294

    if (objectRegistry::debug)
    {
Henry Weller's avatar
Henry Weller committed
295
296
        InfoInFunction
            << "Constructing IOobject called " << name_
297
298
299
300
301
302
            << " of type " << headerClassName_
            << endl;
    }
}


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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_),
    objState_(io.objState_)
{}


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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_),
338
    globalObject_(io.globalObject_),
339
340
341
342
    objState_(io.objState_)
{}


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //

Foam::IOobject::~IOobject()
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

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


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


363
364
365
366
367
368
const Foam::fileName& Foam::IOobject::rootPath() const
{
    return time().rootPath();
}


369
370
371
372
373
374
const Foam::fileName& Foam::IOobject::caseName() const
{
    return time().caseName();
}


Henry's avatar
Henry committed
375
376
Foam::word Foam::IOobject::group() const
{
377
    return name_.ext();
Henry's avatar
Henry committed
378
379
380
}


381
382
Foam::word Foam::IOobject::member() const
{
383
    return name_.lessExt();
384
385
386
}


387
388
Foam::fileName Foam::IOobject::path() const
{
389
    if (isOutsideOfCase(instance()))
390
391
392
393
394
395
396
    {
        return instance();
    }
    else
    {
        return rootPath()/caseName()/instance()/db_.dbDir()/local();
    }
397
398
399
400
401
402
403
404
405
}


Foam::fileName Foam::IOobject::path
(
    const word& instance,
    const fileName& local
) const
{
406
    // Note: can only be called with relative instance since is word type
407
408
409
410
    return rootPath()/caseName()/instance/db_.dbDir()/local;
}


411
412
413
414
415
Foam::fileName Foam::IOobject::localFilePath
(
    const word& typeName,
    const bool search
) const
416
{
417
418
    // Do not check for undecomposed files
    return fileHandler().filePath(false, *this, typeName, search);
419
420
421
}


422
423
424
425
426
Foam::fileName Foam::IOobject::globalFilePath
(
    const word& typeName,
    const bool search
) const
427
{
428
429
    // Check for undecomposed files
    return fileHandler().filePath(true, *this, typeName, search);
430
431
432
433
434
435
436
}


void Foam::IOobject::setBad(const string& s)
{
    if (objState_ != GOOD)
    {
437
        FatalErrorInFunction
Henry Weller's avatar
Henry Weller committed
438
            << "Recurrent failure for object " << s
439
440
441
442
443
            << exit(FatalError);
    }

    if (error::level)
    {
Henry Weller's avatar
Henry Weller committed
444
445
        InfoInFunction
            << "Broken object " << s << info() << endl;
446
447
448
449
450
451
    }

    objState_ = BAD;
}


452
453
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

454
455
456
457
458
459
460
461
462
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_;
463
    globalObject_ = io.globalObject_;
464
465
466
467
468
    objState_ = io.objState_;
}


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