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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* Copyright (c) Schrodinger, LLC.
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information.
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-*
-*
-*
Z* -------------------------------------------------------------------
*/
#include"os_predef.h"
#include"Parse.h"
#include <stdio.h>
#include <string.h>
const char *ParseNextLine(const char *p)
{
char ch;
const char mask = -16; /* 0xF0 */
while((mask & p[0]) && (mask & p[1]) && (mask & p[2]) && (mask & p[3])) /* trusting short-circuit to avoid overrun */
p += 4;
while((ch = *p)) {
p++;
if(ch == 0xD) { /* Mac or PC */
if((*p) == 0xA) /* PC */
return p + 1;
return p;
} else if(ch == 0xA) { /* Unix */
return p;
}
}
return p;
}
/*========================================================================*/
const char *ParseNCopy(char *q, const char *p, int n)
{ /* n character copy */
char ch;
while((ch = *p)) {
if((ch == 0xD) || (ch == 0xA)) /* don't copy end of lines */
break;
if(!n)
break;
n--;
p++;
*(q++) = ch;
}
*q = 0;
return p;
}
const char *ParseSkipEquals(const char *p)
{
while(*p) {
if(*p != '=')
p++;
else
break;
}
if(*p) {
p++;
while(*p) {
if(*p < 33) /* skip whitespace */
p++;
else
break;
}
}
return p;
}
/**
* Skip all characters in `chars`
*
* Example to skip all whitespace:
*
* p = ParseSkipChars(p, " \t\r\n");
*/
static const char *ParseSkipChars(const char *p, const char *chars) {
while (*p && strchr(chars, *p)) { ++p; }
return p;
}
/*========================================================================*/
const char *ParseIntCopy(char *q, const char *p, int n)
{ /* integer copy */
while(*p) {
if((*p == 0xD) || (*p == 0xA)) /* don't skip end of lines */
break;
if(*p <= 32 || !((*p >= '0') && (*p <= '9')))
p++;
else
break;
}
while(*p) {
if(*p <= 32)
break;
if(!n)
break;
if((*p == 0xD) || (*p == 0xA)) /* don't copy end of lines */
break;
if(!((*p >= '0') && (*p <= '9')))
break;
*(q++) = *(p++);
n--;
}
*q = 0;
return p;
}
/*========================================================================*/
const char *ParseAlphaCopy(char *q, const char *p, int n)
{ /* integer copy */
while(*p) {
if((*p == 0xD) || (*p == 0xA)) /* don't skip end of lines */
break;
if(*p <= 32 || !(((*p >= 'A') && (*p <= 'Z')) || ((*p >= 'a') && (*p <= 'z'))))
p++;
else
break;
}
while(*p) {
if(*p <= 32)
break;
if(!n)
break;
if((*p == 0xD) || (*p == 0xA)) /* don't copy end of lines */
break;
if(!(((*p >= 'A') && (*p <= 'Z')) || ((*p >= 'a') && (*p <= 'z'))))
break;
*(q++) = *(p++);
n--;
}
*q = 0;
return p;
}
/* ParseFloat3List: scan in Python-like list of 3 floats */
int ParseFloat3List(const char *parg, float *vals){
int n;
// skip white space and opening brackets
parg = ParseSkipChars(parg, "([ \t\r\n");
for (int i = 0; i < 3; ++i) {
if (!sscanf(parg, "%f%n", vals, &n))
return false;
// skip white space and commas
parg = ParseSkipChars(parg + n, ", \t\r\n");
++vals;
}
return true;
}
/*========================================================================*/
const char *ParseWordCopy(char *q, const char *p, int n)
{ /* word copy */
while(*p) {
if((*p == 0xD) || (*p == 0xA)) /* don't skip end of lines */
break;
if(*p <= 32)
p++;
else
break;
}
while(*p) {
if(*p <= 32)
break;
if(!n) {
while(*p > 32) /* finish scanning word, but don't copy into field */
p++;
break;
}
if((*p == 0xD) || (*p == 0xA)) /* don't copy end of lines */
break;
*(q++) = *(p++);
n--;
}
*q = 0;
return p;
}
/*========================================================================*/
const char *ParseWordNumberCopy(char *q, const char *p, int n)
{ /* word copy */
int digit_seen_last = 0;
while(*p) {
if((*p == 0xD) || (*p == 0xA)) /* don't skip end of lines */
break;
if(*p <= 32)
p++;
else
break;
}
while(*p) {
if(*p <= 32)
break;
if(!n) {
while(*p > 32) /* finish scanning word, but don't copy into field */
p++;
break;
}
if((*p == 0xD) || (*p == 0xA)) /* don't copy end of lines */
break;
if(digit_seen_last && (*p == '-')) /* parse 123.123-1234.465 as two separate words */
break;
digit_seen_last = (((*p) >= '0') && ((*p) <= '9')) || ((*p) == '.');
*(q++) = *(p++);
n--;
}
*q = 0;
return p;
}
/*========================================================================
* ParseWord
* Copy first word from p into q with fewer than n letters
* PARAMS
* char* q
* destination; will contain the first word from p
* char* p
* source; comes in as a string of tokens
* RETURNS
* p modified so it points to one character past the end of the first
* word of p. Eg. "im a temp selection" => " a temp selection" and
* the return value *q = "im"
*/
const char *ParseWord(char *q, const char *p, int n)
{ /* word copy, across lines */
/* increment ptr past non character input, like spaces and line feeds, bells. */
while(*p) {
if(*p <= 32)
p++;
else
break;
}
/* copy p to q stopping when we hit a space, or run out of
* space (memory) according to the limit n */
while(*p) {
if(*p <= 32)
break;
if(!n)
break;
*(q++) = *(p++);
n--;
}
*q = 0;
return p;
}
/*========================================================================*/
const char *ParseNTrim(char *q, const char *p, int n)
{ /* n character trimmed copy */
char *q_orig = q;
while(*p && n) {
if((*p == 0xD) || (*p == 0xA)) /* don't skip end of lines */
break;
if(*p <= 32) {
p++;
n--;
} else
break;
}
while(*p) {
if(!n)
break;
if((*p == 0xD) || (*p == 0xA)) /* don't copy end of lines */
break;
*(q++) = *(p++);
n--;
}
while(q > q_orig) {
if(*(q - 1) <= 32)
q--;
else
break;
}
*q = 0;
return p;
}
/*========================================================================*/
const char *ParseNTrimRight(char *q, const char *p, int n)
{ /* n character trimmed copy */
char *q_orig = q;
while(*p) {
if(!n)
break;
if((*p == 0xD) || (*p == 0xA)) /* don't copy end of lines */
break;
*(q++) = *(p++);
n--;
}
while(q > q_orig) {
if(*(q - 1) <= 32)
q--;
else
break;
}
*q = 0;
return p;
}
/*========================================================================*/
const char *ParseCommaCopy(char *q, const char *p, int n)
{ /* n character copy up to comma */
while(*p) {
if(!n)
break;
if((*p == 0xD) || (*p == 0xA)) /* don't copy end of lines */
break;
if(*p == ',')
break;
*(q++) = *(p++);
n--;
}
*q = 0;
return p;
}
/*========================================================================*/
const char *ParseNSkip(const char *p, int n)
{ /* n character skip */
while(*p) {
if(!n)
break;
if((*p == 0xD) || (*p == 0xA)) /* stop at newlines */
break;
p++;
n--;
}
return p;
}
|