summaryrefslogtreecommitdiffstats
path: root/kxsldbg/kxsldbgpart/libxsldbg/files_unix.cpp
blob: 5c1bcbff91825d5097cbba5cb5606456a0ecc542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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

/***************************************************************************
                          files_unix.c  -  file functions *nix platform
                                                specific
                             -------------------
    begin                : Tue Jan 29 2002
    copyright            : (C) 2001 by Keith Isdale
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "xsldbg.h"
#include "files.h"
#include "utils.h"
#include "options.h"

static char *tempNames[2] = { NULL, NULL };

  /**
   * filesPlatformInit:
   *
   * Intialize the platform specific files module
   *
   *  This is a platform specific interface
   *
   *
   * Returns 1 if sucessful
   *         0 otherwise  
   */
int
filesPlatformInit(void)
{
    const char *namePrefix = "/tmp/";
    int nameIndex;
    int result = 1;

    /* The "base" names for files files to use */
    const char *names[] = {
        "_xsldbg_tmp1.txt",
        "_xsldbg_tmp2.txt"
    };

    if (getenv("USER")) {
        for (nameIndex = 0; nameIndex < 2; nameIndex++) {
            tempNames[nameIndex] = (char*)
                xmlMalloc(strlen(namePrefix) + strlen(getenv("USER")) +
                          strlen(names[nameIndex]) + 1);
            if (tempNames[nameIndex]) {
                xmlStrCpy(tempNames[nameIndex], namePrefix);
                xmlStrCat(tempNames[nameIndex], getenv("USER"));
                xmlStrCat(tempNames[nameIndex], names[nameIndex]);
            } else {
                xsldbgGenericErrorFunc(i18n("Error: Out of memory.\n"));
                break;
                result = 0;
            }
        }
    } else {
	 xsldbgGenericErrorFunc(i18n("Error: USER environment variable is not set.\n"));
    }
    return result;
}


  /**
   * filesPlatformFree:
   *
   * Free memory used by the platform specific files module
   *
   *  This is a platform specific interface
   *
   */
void
filesPlatformFree(void)
{
    int nameIndex;

    for (nameIndex = 0; nameIndex < 2; nameIndex++) {
        if (tempNames[nameIndex])
            xmlFree(tempNames[nameIndex]);
    }
}

  /**
   * filesTempFileName:
   * @fileNumber : Number of temp file required
   *
   * Return the name of tempfile. For each call to this function
   *     with the same @fileNumber the same file name will be returned
   *     File number : 0 is used by cat command
   *     File number : 1 is used by profiling output  
   *
   *  This is a platform specific interface
   *
   * Returns The name of temp file to be used for temporary results, 
   *         NULL otherwise
   */
const char *
filesTempFileName(int fileNumber)
{

    const char *result = NULL;

    if ((fileNumber < 0) || ((fileNumber + 1) > 2)){ //don't use > (int) sizeof(tempNames), it depends on the platform and is wrong even on i586
#ifdef WITH_XSLDBG_DEBUG_PROCESS
        xsltGenericError(xsltGenericErrorContext,
                         "Error: Unable to allocate temporary file %d for xsldbg\n",
                         fileNumber);
#endif
    }else{
        result = tempNames[fileNumber];
    }

    return result;
}



  /**
   * filesExpandName:
   * @fileName : A valid fileName
   *
   * Converts a fileName to an absolute path
   *          If operating system supports it a leading "~" in the fileName
   *          will be converted to the user's home path. Otherwise
   *          the same name will be returned
   *
   * Returns A copy of the converted @fileName or a copy of 
   *           the @fileName as supplied. May return NULL
   */
xmlChar *
filesExpandName(const xmlChar * fileName)
{
    xmlChar *result = NULL;

    if (fileName) {
        if ((fileName[0] == '~') && getenv("HOME")) {
            result =
                (xmlChar *) xmlMalloc(xmlStrLen(fileName) +
                                      strlen(getenv("HOME")) + 1);
            if (result) {
                xmlStrCpy(result, getenv("HOME"));
                xmlStrCat(result, &fileName[1]);
            } else {
                xsldbgGenericErrorFunc(i18n("Error: Out of memory.\n"));
            }
        } else if (!xmlStrnCmp(fileName, "file:/", 6)){
            /* return a copy of the corrected path */
            result = filesURItoFileName(fileName);
        }else{
            /* return a copy only */
             result = xmlStrdup(fileName);
        }
    }
    return result;
}


  /**
   * filesSearchFileName:
   * @fileType : Is valid
   *
   * Return a copy of the file name to use as an argument to searching
   *
   * Returns A copy of the file name to use as an argument to searching
   */
xmlChar *
filesSearchFileName(FilesSearchFileNameEnum fileType)
{
    xmlChar *result = NULL;
    int type = fileType;
    int preferHtml = optionsGetIntOption(OPTIONS_PREFER_HTML);
    const xmlChar *baseDir = NULL;
    const xmlChar *name = NULL;
    static const char *searchNames[] = {
        /* Note: File names here are in native format, to be appended to the
         *  help directory name or search results path
         */
        /* First list names when prefer html is false */
        "searchresult.xml",     /* input  */
        "search.xsl",           /* stylesheet to use */
        "searchresult.txt",     /* where to put the result */
        /*Now for the names to use when prefer html is true */
        "searchresult.xml",     /* input  */
        "searchhtml.xsl",       /* stylesheet to use */
        "searchresult.html"     /* where to put the result */
    };

    if (!optionsGetStringOption(OPTIONS_DOCS_PATH)
        || !filesSearchResultsPath()) {
        xsldbgGenericErrorFunc(i18n("Error: The value of the option docspath or searchresultspath is empty. See help on setoption or options command for more information.\n"));
        return result;
    }


    name = (xmlChar *) searchNames[(preferHtml * 3) + type];
    switch (type) {
        case FILES_SEARCHINPUT:
            baseDir = filesSearchResultsPath();
            break;

        case FILES_SEARCHXSL:
            baseDir = optionsGetStringOption(OPTIONS_DOCS_PATH);
            break;

        case FILES_SEARCHRESULT:
            baseDir = filesSearchResultsPath();
            break;
    }

    result = (xmlChar*)xmlMalloc(xmlStrLen(baseDir) + xmlStrLen(name) + 1);
    if (result) {
        xmlStrCpy(result, baseDir);
        xmlStrCat(result, name);
    }
    return result;
}