1/*---------------------------------------------------------------------------*
2 *  PFileSystemImpl.c  *
3 *                                                                           *
4 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
5 *                                                                           *
6 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
7 *  you may not use this file except in compliance with the License.         *
8 *                                                                           *
9 *  You may obtain a copy of the License at                                  *
10 *      http://www.apache.org/licenses/LICENSE-2.0                           *
11 *                                                                           *
12 *  Unless required by applicable law or agreed to in writing, software      *
13 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 *  See the License for the specific language governing permissions and      *
16 *  limitations under the License.                                           *
17 *                                                                           *
18 *---------------------------------------------------------------------------*/
19
20#include "LCHAR.h"
21#include "PFileSystemImpl.h"
22#include "plog.h"
23#include "pmemory.h"
24
25#define MTAG NULL
26
27ESR_BOOL PFileSystemCreated = ESR_FALSE;
28
29/**
30 * [file path, PFileSystem*] mapping.
31 */
32PHashTable* PFileSystemPathMap = NULL;
33
34/**
35 * Portable standard input.
36 */
37PFile* PSTDIN = NULL;
38/**
39 * Portable standard output.
40 */
41PFile* PSTDOUT = NULL;
42/**
43 * Portable standard error.
44 */
45PFile* PSTDERR = NULL;
46
47/**
48 * Current working directory.
49 */
50LCHAR PFileSystemCurrentDirectory[P_PATH_MAX] = L("/");
51
52#ifdef USE_THREAD
53/* Prototype of private function */
54PORTABLE_API ESR_ReturnCode PtrdFlush();
55#endif
56
57
58ESR_ReturnCode PFileSystemCreate(void)
59{
60  ESR_ReturnCode rc;
61
62  if (PFileSystemCreated)
63    return ESR_SUCCESS;
64
65#ifdef USE_STACKTRACE
66  CHKLOG(rc, PStackTraceCreate());
67#endif
68  CHKLOG(rc, PHashTableCreate(NULL, MTAG, &PFileSystemPathMap));
69  CHKLOG(rc, PFileSystemInitializeStreamsImpl());
70  PFileSystemCreated = ESR_TRUE;
71  return ESR_SUCCESS;
72CLEANUP:
73  return rc;
74}
75
76ESR_ReturnCode PFileSystemDestroy(void)
77{
78  ESR_ReturnCode rc;
79  LCHAR* key;
80  PHashTableEntry* entry;
81  PHashTableEntry* oldEntry;
82
83  if (!PFileSystemCreated)
84    return ESR_SUCCESS;
85  PFileSystemCreated = ESR_FALSE;
86  if (PFileSystemPathMap != NULL)
87  {
88    CHKLOG(rc, PHashTableEntryGetFirst(PFileSystemPathMap, &entry));
89    while (entry != NULL)
90    {
91      CHKLOG(rc, PHashTableEntryGetKeyValue(entry, (void **)&key, (void **)NULL));
92      oldEntry = entry;
93      CHKLOG(rc, PHashTableEntryAdvance(&entry));
94      CHKLOG(rc, PHashTableEntryRemove(oldEntry));
95      FREE(key);
96    }
97    CHKLOG(rc, PHashTableDestroy(PFileSystemPathMap));
98    PFileSystemPathMap = NULL;
99  }
100  CHKLOG(rc, PFileSystemShutdownStreamsImpl());
101#ifdef USE_STACKTRACE
102  CHKLOG(rc, PStackTraceDestroy());
103#endif
104  return ESR_SUCCESS;
105CLEANUP:
106  return rc;
107}
108