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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/****************************************************************************
*
* earray : euler's array of command, output and comment strings
*
****************************************************************************/
#include "earray.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#define ALLOC_CHUNK 16
#define ALLOC_SIZE(n) (ALLOC_CHUNK*(((n)/ALLOC_CHUNK)+1))
typedef struct estring {
GString * str;
int type;
} estring;
struct earray {
estring * data;
int len;
int alloc;
};
static int watchforrealloc(earray *a, int n);
/*---------------------------------------------------------------------------
* constructor / destructor
*---------------------------------------------------------------------------*/
earray * e_new()
{
earray *a;
a = (earray*)malloc(sizeof(earray));
if (a) {
a->data = (estring*)malloc(ALLOC_CHUNK*sizeof(estring));
if (a->data) {
a->alloc = ALLOC_CHUNK;
a->len = 0;
return a;
}
free(a);
}
return NULL;
}
void e_free(earray *a)
{
int i;
if (!a) return;
for (i=0 ; i<a->len ; i++) {
g_string_free(a->data[i].str,TRUE);
}
free(a->data);
free(a);
}
int e_load(earray *a, char *filename)
{
FILE *in;
DIR *dir;
char line[1024];
int type;
if (!a) return 0;
/* try to see if it is not a directory (because fopen succeeds even if
it is a directory... */
dir = opendir(filename);
if (dir) {
fprintf(stderr,"It must be a file. I can't open %s !\n",filename);
closedir(dir);
return 0;
}
/* load the notebook */
in=fopen(filename,"r");
if (!in)
{
fprintf(stderr,"Could not open the file %s !\n",filename);
return 0;
}
/* clear the old notebook */
e_clear(a);
while (!feof(in)) {
int n;
if (!fgets(line,1024,in)) break;
n=strlen(line);
if (n>=2 && line[n-2]=='\r') line[n-2]=0;
if (line[n-1]=='\n') line[n-1]=0;
switch (line[0]) {
case '>':type=E_PROMPT;break;
case '$':type=E_UDF;break;
case '%':type=E_COMMENT;break;
default:type=E_OUTPUT;
}
e_append(a,line,type);
}
fclose(in);
/* if the notebook file was empty... */
if (!a->len) e_append(a,"",E_OUTPUT);
return 1;
}
int e_save(earray *a, char *filename)
{
FILE *out;
int i;
if (!a) return 0;
out=fopen(filename,"w");
if (!out) {
fprintf(stderr,"Could not save to the file %s !\n",filename);
return 0;
}
for (i=0 ; i<a->len ; i++) {
char ch = a->data[i].str->str[0];
if (a->data[i].type==E_OUTPUT && (ch=='>' || ch=='$' || ch=='%'))
fputc(' ',out);
fputs(a->data[i].str->str,out);
fputc('\n',out);
}
fclose(out);
return 1;
}
int e_get_length(earray *a)
{
if (a) return a->len;
return 0;
}
/*---------------------------------------------------------------------------
* add / remove lines to the array
*---------------------------------------------------------------------------*/
void e_clear(earray *a)
{
int i;
if (!a) return;
for (i=0 ; i<a->len ; i++) {
g_string_free(a->data[i].str,TRUE);
}
a->len = 0;
watchforrealloc(a,0);
}
int e_append(earray *a, char *text, int type)
{
if (a) {
GString *s = g_string_new(text);
if (s) {
if (watchforrealloc(a,a->len+1)) {
a->data[a->len].str = s;
a->data[a->len].type = type;
a->len++;
return 1;
}
g_string_free(s,TRUE);
}
}
return 0;
}
int e_insert(earray *a, int index, char *text, int type)
{
if (a) {
GString *s;
if (index>=a->len) return e_append(a,text,type);
if (index<0) index=0;
s = g_string_new(text);
if (s) {
if (watchforrealloc(a,a->len+1)) {
memmove(&(a->data[index+1]),&(a->data[index]),(a->len-index)*sizeof(estring));
a->data[index].str = s;
a->data[index].type = type;
a->len++;
return 1;
}
g_string_free(s,TRUE);
}
}
return 0;
}
void e_remove(earray *a, int index)
{
if (!a) return;
if (index<0 || index>=a->len-1) return;
memmove(&(a->data[index]),&(a->data[index+1]),(a->len-index)*sizeof(estring));
a->len--;
watchforrealloc(a,a->len);
}
/*---------------------------------------------------------------------------
* set / get things in specified line in the array
*---------------------------------------------------------------------------*/
void e_set_type(earray *a, int index, int type)
{
if (!a) return;
if (index<0 || index>=a->len) return;
a->data[index].type = type;
}
int e_get_type(earray *a, int index)
{
if (a && index>=0 && index<a->len)
return a->data[index].type;
return 0;
}
void e_set_text(earray *a, int index, char *text)
{
if (!a) return;
if (index<0 || index>=a->len) return;
g_string_assign(a->data[index].str,text);
}
char * e_get_text(earray *a, int index)
{
if (a && index>=0 && index<a->len)
return a->data[index].str->str;
return NULL;
}
void e_append_char(earray *a, int index, char c)
{
if (a && index>=0 && index<a->len)
g_string_append_c(a->data[index].str,c);
}
void e_append_text(earray *a, int index, char *text)
{
if (a && index>=0 && index<a->len)
g_string_append(a->data[index].str,text);
}
void e_insert_char(earray *a, int index, int pos, char c)
{
if (a && index>=0 && index<a->len)
g_string_insert_c(a->data[index].str,pos,c);
}
void e_insert_text(earray *a, int index, int pos, char *text)
{
if (a && index>=0 && index<a->len)
g_string_insert(a->data[index].str,pos,text);
}
void e_remove_text(earray *a, int index, int pos, int len)
{
if (a && index>=0 && index<a->len)
g_string_erase(a->data[index].str,pos,len);
}
int e_get_text_length(earray *a, int index)
{
return a->data[index].str->len;
}
/*---------------------------------------------------------------------------
* watch if the array should be resized
*---------------------------------------------------------------------------*/
static int watchforrealloc(earray *a, int n)
{
int alloc = ALLOC_SIZE(n);
if (alloc!=a->alloc) {
estring *data;
data = (estring*)realloc(a->data,alloc*sizeof(estring));
if (data) {
a->data = data;
a->alloc = alloc;
return 1;
}
return 0;
}
return 1;
}
|