NeXus  4.4.3
 All Data Structures Files Functions Variables Typedefs Macros Pages
nxstack.c
Go to the documentation of this file.
1 /*
2  This is some code to handle a stack of NeXus files. This is used to implement
3  external linking within the NeXus-API
4 
5  Copyright (C) 1997-2006 Mark Koennecke
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21  For further information, see <http://www.nexusformat.org>
22 
23  Added code to support the path stack for NXgetpath,
24  Mark Koennecke, October 2009
25 */
26 #include <stdlib.h>
27 #include <string.h>
28 #include <napi.h>
29 #include <napi_internal.h>
30 #include "nxstack.h"
31 
32 /*-----------------------------------------------------------------------
33  Data definitions
34 ---------------------------------------------------------------------*/
35 
36 typedef struct {
38  NXlink closeID;
39  char filename[1024];
41 
42 
43 typedef struct __fileStack {
47  char pathStack[NXMAXSTACK][NX_MAXNAMELEN];
48 }fileStack;
49 /*---------------------------------------------------------------------*/
51  pFileStack pNew = NULL;
52 
53  pNew = (pFileStack)malloc(sizeof(fileStack));
54  if(pNew == NULL){
55  return NULL;
56  }
57  memset(pNew,0,sizeof(fileStack));
58  pNew->fileStackPointer = -1;
59  pNew->pathPointer = -1;
60  return pNew;
61 }
62 /*---------------------------------------------------------------------*/
64  if(self != NULL){
65  free(self);
66  }
67 }
68 /*---------------------------------------------------------------------*/
70  return sizeof(fileStack);
71 }
72 /*----------------------------------------------------------------------*/
73 void pushFileStack(pFileStack self, pNexusFunction pDriv, char *file){
74  size_t length;
75 
76  self->fileStackPointer++;
77  self->fileStack[self->fileStackPointer].pDriver = pDriv;
78  memset(&self->fileStack[self->fileStackPointer].closeID,0,sizeof(NXlink));
79  length = strlen(file);
80  if(length >= 1024){
81  length = 1023;
82  }
83  memcpy(&self->fileStack[self->fileStackPointer].filename,file,length);
84 }
85 /*----------------------------------------------------------------------*/
87  self->fileStackPointer--;
88  if(self->fileStackPointer < -1){
89  self->fileStackPointer = -1;
90  }
91 }
92 /*----------------------------------------------------------------------*/
94  return self->fileStack[self->fileStackPointer].pDriver;
95 }
96 /*---------------------------------------------------------------------*/
98  return self->fileStack[self->fileStackPointer].filename;
99 }
100 /*----------------------------------------------------------------------*/
101 void peekIDOnStack(pFileStack self, NXlink *id){
102  memcpy(id, &self->fileStack[self->fileStackPointer].closeID, sizeof(NXlink));
103 }
104 /*---------------------------------------------------------------------*/
105 void setCloseID(pFileStack self, NXlink id){
106  memcpy(&self->fileStack[self->fileStackPointer].closeID, &id, sizeof(NXlink));
107 }
108 /*----------------------------------------------------------------------*/
110  return self->fileStackPointer;
111 }
112 /*----------------------------------------------------------------------*/
113 void pushPath(pFileStack self, const char *name){
114  self->pathPointer++;
115  strncpy(self->pathStack[self->pathPointer],name,NX_MAXNAMELEN-1);
116 }
117 /*-----------------------------------------------------------------------*/
118 void popPath(pFileStack self){
119  self->pathPointer--;
120  if(self->pathPointer < -1){
121  self->pathPointer = -1;
122  }
123 }
124 /*-----------------------------------------------------------------------*/
125 int buildPath(pFileStack self, char *path, int pathlen){
126  int i;
127  size_t totalPathLength;
128  char *totalPath;
129 
130  for(i = 0, totalPathLength = 5; i <= self->pathPointer; i++){
131  totalPathLength += strlen(self->pathStack[i]) + 1;
132  }
133  totalPath = (char*)malloc(totalPathLength*sizeof(char));
134  if(totalPath == NULL){
135  return 0;
136  }
137  memset(totalPath,0,totalPathLength*sizeof(char));
138  for(i = 0; i <= self->pathPointer; i++){
139  strcat(totalPath,"/");
140  strcat(totalPath,self->pathStack[i]);
141  }
142 
143  strncpy(path,totalPath,pathlen-1);
144  free(totalPath);
145  return 1;
146 }
NXlink closeID
Definition: nxstack.c:38
struct __fileStack * pFileStack
Definition: nxstack.h:30
#define MAXEXTERNALDEPTH
Definition: nxstack.h:31
pFileStack makeFileStack()
Definition: nxstack.c:50
void pushFileStack(pFileStack self, pNexusFunction pDriv, char *file)
Definition: nxstack.c:73
int pathPointer
Definition: nxstack.c:46
void killFileStack(pFileStack self)
Definition: nxstack.c:63
void peekIDOnStack(pFileStack self, NXlink *id)
Definition: nxstack.c:101
char * peekFilenameOnStack(pFileStack self)
Definition: nxstack.c:97
char pathStack[NXMAXSTACK][NX_MAXNAMELEN]
Definition: nxstack.c:47
int buildPath(pFileStack self, char *path, int pathlen)
Definition: nxstack.c:125
void popPath(pFileStack self)
Definition: nxstack.c:118
int fileStackDepth(pFileStack self)
Definition: nxstack.c:109
struct __fileStack fileStack
pNexusFunction pDriver
Definition: nxstack.c:37
void pushPath(pFileStack self, const char *name)
Definition: nxstack.c:113
int getFileStackSize()
Definition: nxstack.c:69
void setCloseID(pFileStack self, NXlink id)
Definition: nxstack.c:105
pNexusFunction peekFileOnStack(pFileStack self)
Definition: nxstack.c:93
int fileStackPointer
Definition: nxstack.c:44
void popFileStack(pFileStack self)
Definition: nxstack.c:86