📄 token.c
字号:
/* -------------------------------------------------------------------- *//* SMS Client, send messages to mobile phones and pagers *//* *//* token.c *//* *//* Copyright (C) 1997,1998 Angelo Masci *//* *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Library General Public *//* License as published by the Free Software Foundation; either *//* version 2 of the License, or (at your option) any later version. *//* *//* This library is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *//* Library General Public License for more details. *//* *//* You should have received a copy of the GNU Library General Public *//* License along with this library; if not, write to the Free *//* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *//* You can contact the author at this e-mail address: *//* *//* angelo@styx.demon.co.uk *//* *//* -------------------------------------------------------------------- *//* $Id$ -------------------------------------------------------------------- */#include <stdio.h>#include <ctype.h>#include <string.h>#include "token.h"#include "common.h"/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */char *get_line(char *str, int n, int *line_count, FILE *fp){ char *ptr; (*line_count)++; while(fgets(str, n, fp) != NULL) { ptr = str; while(isspace(*ptr)) { ptr++; } if (*ptr != '\0') { return str; } (*line_count)++; /* A blank line was skipped */ /* so increment the line count */ } return NULL;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int escape_char_token(char *token, int maxlen, char *ptr, char **eptr){ int type; ptr++; if (*ptr == 'a') { /* \a */ type = ALERT_TOKEN; token[0] = '\f'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == 'b') { /* \b */ type = BACKSPACE_TOKEN; token[0] = '\b'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == 'f') { /* \f */ type = FORMFEED_TOKEN; token[0] = '\f'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == 'n') { /* \n */ type = NEWLINE_TOKEN; token[0] = '\n'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == 'r') { /* \r */ type = CARRIAGE_RETURN_TOKEN; token[0] = '\r'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == 't') { /* \t */ type = HORIZONTAL_TAB_TOKEN; token[0] = '\t'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == 'v') { /* \v */ type = VERTICLE_TAB_TOKEN; token[0] = '\v'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == 'x') { /* \x */ int low_nibble, high_nibble; ptr++; if (isxdigit(*ptr)) { if ((*ptr >= '0') && (*ptr <= '9')) { high_nibble = (*ptr - '0') << 4; } else if ((*ptr >= 'a') && (*ptr <= 'f')) { high_nibble = (10 + (*ptr - 'a')) << 4; } else { high_nibble = (10 + (*ptr - 'A')) << 4; } ptr++; if (isxdigit(*ptr)) { if ((*ptr >= '0') && (*ptr <= '9')) { low_nibble = (*ptr - '0'); } else if ((*ptr >= 'a') && (*ptr <= 'f')) { low_nibble = (10 + (*ptr - 'a')); } else { low_nibble = (10 + (*ptr - 'A')); } type = HEX_TOKEN; token[0] = high_nibble + low_nibble; token[1] = '\0'; *eptr = ++ptr; } else { type = ERROR_HEX_TOKEN; ptr -= 3; token[0] = *ptr++; token[1] = *ptr++; token[2] = *ptr++; token[3] = '\0'; *eptr = ptr; } } else { type = ERROR_HEX_TOKEN; ptr -= 2; token[0] = *ptr++; token[1] = *ptr++; token[2] = '\0'; *eptr = ptr; } } else if (*ptr == '\\') { /* \ */ type = BACKSLASH_TOKEN; token[0] = '\\'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == '\"') { /* Quote */ type = QUOTE_TOKEN; token[0] = '\"'; token[1] = '\0'; *eptr = ++ptr; } else if (*ptr == '\'') { /* Single Quote */ type = SINGLE_QUOTE_TOKEN; token[0] = '\''; token[1] = '\0'; *eptr = ++ptr; } else { type = BACKSLASH_TOKEN; token[0] = '\\'; token[1] = '\0'; *eptr = ptr; } return type;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int isstring(char c){ if (isspace(c)) { return FALSE; } if ((c != '\"') && (c != '\'') && (c != '#') && (c != '=') && (c != '\0') && (c != '\n')) { return TRUE; } return FALSE;} /* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int get_token(char *token, int maxlen, char *ptr, char **eptr){ int type, i; while (isspace(*ptr)) /* Skip white space */ { ptr++; } if (isstring(*ptr)) { type = STRING_TOKEN; for (i=0; i<(maxlen -1); i++) { if (*ptr == '\0') { break; } if (!isstring(*ptr)) { break; } if (*ptr == '\\') { char etoken[128]; escape_char_token(etoken, maxlen, ptr, &ptr); token[i] = '\0'; sms_strcat(token, etoken); continue; } token[i] = *ptr; ptr++; } token[i] = '\0'; *eptr = ptr; } else if (*ptr == '\"') /* Quoted String */ { /* All characters within */ /* doublequotes */ /* If missing closing quote */ /* Quoted String Error token */ /* returned */ char etoken[128]; type = QUOTED_STRING_TOKEN; ptr++; for (i=0; i<(maxlen -1); i++) { if (*ptr == '\0') { break; } if (*ptr == '\\') { escape_char_token(etoken, maxlen, ptr, &ptr); token[i] = '\0'; sms_strcat(token, etoken); continue; } else if (*ptr == '\"') { break; } token[i] = *ptr; ptr++; } if (*ptr != '\"') { type = QUOTED_STRING_ERROR_TOKEN; } else { ptr++; } token[i] = '\0'; *eptr = ptr; } else if (*ptr == '\'') /* Single Quoted String */ { /* All characters within */ /* singlequotes */ /* If missing closing quote */ /* Single Quoted String Error */ /* token returned */ type = SINGLE_QUOTED_STRING_TOKEN; ptr++; for (i=0; i<(maxlen -1); i++) { if (*ptr == '\0') { break; } if (*ptr == '\'') { break; } token[i] = *ptr; ptr++; } if (*ptr != '\'') { type = SINGLE_QUOTED_STRING_ERROR_TOKEN; } else { ptr++; } token[i] = '\0'; *eptr = ptr; } else if (*ptr == '#') /* Comment */ { type = COMMENT_TOKEN; ptr++; i = 0; while (*ptr != '\0') { token[i] = *ptr++; } token[i] = *ptr; *eptr = ptr; } else if (*ptr == '=') /* Assignment */ { type = ASSIGNMENT_TOKEN; token[0] = '='; token[1] = '\0'; *eptr = ++ptr; } else if ((*ptr == '\0') || (*ptr == '\n')) /* EOL */ { type = NULL_TOKEN; token[0] = '\0'; *eptr = ptr; } else { type = UNKNOWN_TOKEN; for (i=0; i<(maxlen -1); i++) { if (*ptr == '\0') { break; } token[i] = *ptr; ptr++; } token[i] = '\0'; *eptr = ptr; } return type;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -