55 |
static gboolean QuestionIsVisible = FALSE; |
static gboolean QuestionIsVisible = FALSE; |
56 |
static gint AutosaveSig = 0; |
static gint AutosaveSig = 0; |
57 |
|
|
58 |
|
/* returns the sign of an integer */ |
59 |
|
short sgn (int i) |
60 |
|
{ |
61 |
|
return ( (i == 0) ? (0) : (i > 1) ? (1) : (-1) ); |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
/* This function returns 'string' with all instances |
/* This function returns 'string' with all instances |
66 |
of 'obj' replaced with instances of 'replacement' |
of 'obj' replaced with instances of 'replacement' |
67 |
It modifies string and re-allocs it. |
It modifies string and re-allocs it. |
68 |
*/ |
*/ |
69 |
gchar *str_replace_tokens (gchar **string, gchar obj, gchar *replacement) |
gchar *str_replace_str (gchar **string, const gchar *obj, |
70 |
|
const gchar *replace) |
71 |
{ |
{ |
72 |
gchar *p; |
unsigned int chars_correct = 0; |
73 |
gint rsize = strlen (replacement); |
unsigned int inpos = 0; |
74 |
gint osize = 1; |
int diff = strlen(replace) - strlen(obj); |
75 |
gint diff = rsize - osize; |
gchar c; |
76 |
|
|
77 |
p = *string; |
//g_printf("str_replace_str: |%s|%s|%s|\n", *string, obj, replace); |
|
while ((p = strchr (p, obj))) |
|
|
{ |
|
|
*string = g_realloc (*string, strlen (*string) + diff + 1); |
|
|
g_memmove (p + rsize, p + osize, strlen (p + osize) + 1); |
|
78 |
|
|
79 |
memcpy (p, replacement, rsize); |
/* traverse string */ |
80 |
|
while ( ( c = *(*string + inpos) ) != '\0') |
81 |
|
{ |
82 |
|
if ( c == obj[chars_correct] ) |
83 |
|
{ |
84 |
|
chars_correct++; |
85 |
|
} |
86 |
|
else |
87 |
|
{ |
88 |
|
/* start over */ |
89 |
|
chars_correct = 0; |
90 |
|
} |
91 |
|
|
92 |
|
/* got whole string? */ |
93 |
|
if ( chars_correct == strlen (obj) ) |
94 |
|
{ |
95 |
|
/* yes, do replace/move */ |
96 |
|
|
97 |
|
/* do we have to realloc, and if yes, now or after |
98 |
|
the replace? */ |
99 |
|
switch ( sgn (diff) ) |
100 |
|
{ |
101 |
|
case (0): /* don't need to realloc() and move */ |
102 |
|
memcpy ( *string + inpos - strlen (replace) + diff, |
103 |
|
replace, strlen (replace) ); |
104 |
|
break; |
105 |
|
|
106 |
|
case (1): /* realloc() before, make bigger */ |
107 |
|
*string = g_realloc ( *string, strlen (*string) + 1 + diff ); |
108 |
|
g_memmove ( *string + inpos, |
109 |
|
*string + inpos - diff, |
110 |
|
strlen(*string) + diff - inpos); |
111 |
|
memcpy ( *string + inpos - 1, replace, strlen (replace) ); |
112 |
|
break; |
113 |
|
|
114 |
|
case (-1): /* realloc() after, make smaller */ |
115 |
|
g_memmove ( *string + inpos + diff, |
116 |
|
*string + inpos, |
117 |
|
strlen(*string) + 1 - inpos ); /* with trailing '\0' */ |
118 |
|
memcpy ( *string + inpos - strlen (replace) + diff + 1, |
119 |
|
replace, strlen (replace) ); |
120 |
|
*string = g_realloc ( *string, strlen (*string) + 1 ); |
121 |
|
break; |
122 |
|
} |
123 |
|
|
124 |
|
chars_correct = 0; |
125 |
|
inpos += diff; |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
p = p + rsize; |
inpos++; |
130 |
} |
} |
131 |
|
|
132 |
|
return (*string); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
/* compatibility function - FIXME: adjust remaining calls */ |
137 |
|
gchar *str_replace_tokens(gchar **string, const gchar obj, const gchar *replace) |
138 |
|
{ |
139 |
|
gchar objstr[2]; |
140 |
|
objstr[0] = obj; objstr[1] = '\0'; |
141 |
|
|
142 |
return *string; |
return (str_replace_str (string, objstr, replace)); |
143 |
} |
} |
144 |
|
|
145 |
|
|
146 |
/* This function is used by 'init_file_properties' to set the base filename |
/* This function is used by 'init_file_properties' to set the base filename |
147 |
and the type of the file. */ |
and the type of the file. */ |
148 |
|
|
383 |
|
|
384 |
conf_key = g_strdup_printf ("General/RecentFiles/File%d", i); |
conf_key = g_strdup_printf ("General/RecentFiles/File%d", i); |
385 |
conf_val = get_string_conf (conf_key); |
conf_val = get_string_conf (conf_key); |
386 |
|
|
387 |
|
str_replace_str (&conf_val, g_get_home_dir(), "~"); |
388 |
str_replace_tokens (&conf_val, '_', "__"); |
str_replace_tokens (&conf_val, '_', "__"); |
389 |
str_replace_tokens (&conf_val, '/', "\\/"); |
str_replace_tokens (&conf_val, '/', "\\/"); |
390 |
|
|