]> SALOME platform Git repositories - modules/paravis.git/blob - src/ParaView/vtkParseString.c
Salome HOME
Merge from BR_PORTING_VTK6 01/03/2013
[modules/paravis.git] / src / ParaView / vtkParseString.c
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    vtkParseString.c
5
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13
14 =========================================================================*/
15 /*-------------------------------------------------------------------------
16   Copyright (c) 2012 David Gobbi.
17
18   Contributed to the VisualizationToolkit by the author in April 2012
19   under the terms of the Visualization Toolkit 2008 copyright.
20 -------------------------------------------------------------------------*/
21
22 #include "vtkParseString.h"
23 #include <stdlib.h>
24 #include <string.h>
25
26 /*----------------------------------------------------------------
27  * String utility methods
28  *
29  * Strings are centrally allocated and are const.  They should not
30  * be freed until the parse is complete and all the data structures
31  * generated by the parse have been freed.
32  */
33
34 /* allocate a string of n+1 bytes */
35 void vtkParse_InitStringCache(StringCache *cache)
36 {
37   cache->NumberOfChunks = 0;
38   cache->Chunks = NULL;
39   cache->ChunkSize = 0;
40   cache->Position = 0;
41 }
42
43 /* allocate a string of n+1 bytes */
44 char *vtkParse_NewString(StringCache *cache, size_t n)
45 {
46   size_t nextPosition;
47   char *cp;
48
49   if (cache->ChunkSize == 0)
50     {
51     cache->ChunkSize = 8176;
52     }
53
54   // align next start position on an 8-byte boundary
55   nextPosition = (((cache->Position + n + 8) | 7 ) - 7);
56
57   if (cache->NumberOfChunks == 0 || nextPosition > cache->ChunkSize)
58     {
59     if (n + 1 > cache->ChunkSize)
60       {
61       cache->ChunkSize = n + 1;
62       }
63     cp = (char *)malloc(cache->ChunkSize);
64
65     /* if empty, alloc for the first time */
66     if (cache->NumberOfChunks == 0)
67       {
68       cache->Chunks = (char **)malloc(sizeof(char *));
69       }
70     /* if count is power of two, reallocate with double size */
71     else if ((cache->NumberOfChunks & (cache->NumberOfChunks-1)) == 0)
72       {
73       cache->Chunks = (char **)realloc(
74         cache->Chunks, (2*cache->NumberOfChunks)*sizeof(char *));
75       }
76
77     cache->Chunks[cache->NumberOfChunks++] = cp;
78
79     cache->Position = 0;
80     nextPosition = (((n + 8) | 7) - 7);
81     }
82
83   cp = &cache->Chunks[cache->NumberOfChunks-1][cache->Position];
84   cp[0] = '\0';
85
86   cache->Position = nextPosition;
87
88   return cp;
89 }
90
91 /* free all allocated strings */
92 void vtkParse_FreeStringCache(StringCache *cache)
93 {
94   unsigned long i;
95
96   for (i = 0; i < cache->NumberOfChunks; i++)
97     {
98     free(cache->Chunks[i]);
99     }
100   if (cache->Chunks)
101     {
102     free(cache->Chunks);
103     }
104
105   cache->Chunks = NULL;
106   cache->NumberOfChunks = 0;
107 }
108
109 /* duplicate the first n bytes of a string and terminate it */
110 const char *vtkParse_CacheString(StringCache *cache, const char *in, size_t n)
111 {
112   char *res = NULL;
113
114   res = vtkParse_NewString(cache, n);
115   strncpy(res, in, n);
116   res[n] = '\0';
117
118   return res;
119 }