std::string jsion_cpp::ini::ReadString( std::string sectionName, std::string keyName, std::string defaultValue, std::string fileName) { std::ifstream fs_ini(fileName.c_str(), std::ios_base::in); if (fs_ini) { std::string line(""); std::string section = std::string("[") + sectionName + std::string("]"); while (getline(fs_ini, line)) { if (section.compare(eatSpace(line)) == 0) { while (getline(fs_ini, line)) { std::string temp = eatSpace(line); if (temp.length() > 0 && '[' == temp[0]) { return defaultValue; } if (temp.length() > keyName.length() + 1 && temp.compare(0, keyName.length(), keyName) == 0 && temp[keyName.length()] == '=') { return temp.substr(keyName.length() + 1); } } } } } return defaultValue; }
/*----------------------------------------------------------------------------*/ int checkTwoOperands(Data * data, char * tag,char* command){ char operand1[MAX_TAG_LEN]; char operand2[MAX_TAG_LEN]; /* get the 1st operand*/ if(sscanf(data->line, "%30[^,\n ]s", operand1) == 0){ printf("[Error] on line %d: no operands given - two operands required\n", data->lc); data->containError=TRUE; return 0; } /* check there's a comma between the two operands */ data->line+= strlen(operand1); eatSpace(data); if(!(*data->line)==','){ printf("[Error] on line %d: no comma between operands\n", data->lc); data->containError=TRUE; return 0; } data->line++; /* get the 2nd operand*/ if(sscanf(data->line, "%30s", operand2) == 0){ printf("[Error] on line %d: second operand required\n", data->lc); data->containError=TRUE; return 0; } data->line+= strlen(operand2); /* add a terminating character to the operand string */ eatSpace(data); /* shouldn't be any additional character or arguments after the 2nd operand */ if(isEndOfLine(data->line) == 0){ printf("[Error] on line %d: extra characters after 2nd operand \n", data->lc); data->containError=TRUE; return 0; } /* check the first operand is valid syntactically */ if (isOperandValidSyntax(data,operand1) == 0){ printf("[Error] on line %d: first operand is invalid \n", data->lc); data->containError=TRUE; return 0; } /* check the second operand is valid syntactically */ if (isOperandValidSyntax(data,operand2) == 0){ printf("[Error] on line %d: second operand is invalid \n", data->lc); data->containError=TRUE; return 0; } /* add the address under the tag if it was given */ if(tag != NULL){ addTag(data,tag,data->ic); } data->ic+=3; return 1; }
BOOL StrToU64( char *str, unsigned_64 *u64, BOOL neg ) { unsigned_64 r64; unsigned_64 v64; int value; int radix; str = eatSpace( str ); U32ToU64( 0, u64 ); if( neg == TRUE ) { if( *str == '-') { str++; } else { if( *str == '+' ) { str++; neg = FALSE; } } } str = eatSpace( str ); switch( *str ){ case '\0': return( FALSE ); case '0': str++; if( tolower(*str) == 'x' ) { radix = 16; str++; } else { radix = 8; } break; default: radix = 10; } U32ToU64( radix, &r64 ); while( *str != '\0' ) { if( isdigit( *str ) ) { value = *str - '0'; } else { value = 10 + tolower( *str ) - 'a'; } if( value < 0 || value >= radix ) { return ( FALSE ); } U32ToU64( value, &v64 ); U64Mul( u64, &r64, u64 ); U64Add( &v64, u64, u64 ); str++; } if( neg == TRUE) { U64Neg( u64, u64 ); } return( TRUE ); }
/*----------------------------------------------------------------------------*/ int checkOneOperands(Data * data, char * tag){ char operand[MAX_TAG_LEN]; if(sscanf(data->line, "%30s", operand) == 0){ printf("[Error] on line %d: no operands given - one operand required\n", data->lc); data->containError=TRUE; return 0; } data->line+= strlen(operand); eatSpace(data); /* shouldn't be any additional character or arguments after the operand */ if(isEndOfLine(data->line) == 0){ printf("[Error] on line %d: extra argument or characters after the operand \n", data->lc); data->containError=TRUE; return 0; } if (isOperandValidSyntax(data,operand) == 0){ printf("[Error] on line %d: operand is invalid \n", data->lc); data->containError=TRUE; return 0; } /* what happens when IC and DC are the same? how does it diffrentiate address? is DC limited to 100? */ if(tag != NULL){ addTag(data,tag,data->ic); } data->ic+=2; return 1; }
/* * MarkEndOfNextWordForward - find a pointer to the end of the next * word (in the forwards direction) */ vi_rc MarkEndOfNextWordForward( i_mark *result, i_mark *curr, bool big ) { char *s; btype block_type; btype block_type2; noWrap = false; *result = *curr; if( EditFlags.IsChangeWord ) { EditFlags.IsChangeWord = false; s = ptrFromMark( result ); if( s == NULL ) { return( ERR_NO_ERR ); } block_type = charType( *s, big ); block_type2 = charType( *(s + 1), big ); if( block_type == BLOCK_ENDOFLINE || block_type != block_type2 ) { return( ERR_NO_ERR ); } } s = incrementMark( result ); if( s == NULL ) { s = nextLine( result ); if( s == NULL ) { if( EditFlags.OperatorWantsMove ) { *result = *curr; return( ERR_NO_ERR ); } return( ERR_NOT_THAT_MANY_WORDS ); } } s = eatSpace( result, false ); if( s == NULL ) { return( ERR_NOT_THAT_MANY_WORDS ); } block_type = charType( *s, big ); while( charType( *(s + 1), big ) == block_type ) { s = incrementMark( result ); if( s == NULL ) { break; } } return( ERR_NO_ERR ); } /* MarkEndOfNextWordForward */
/* * MarkEndOfNextWordForward - find a pointer to the start of the next * word (in the backwards direction) */ vi_rc MarkStartOfNextWordBackward( i_mark *result, i_mark *curr, bool big ) { char *s; btype block_type; noWrap = false; *result = *curr; s = decrementMark( result ); if( s == NULL ) { s = prevLine( result ); if( s == NULL ) { return( ERR_NOT_THAT_MANY_WORDS ); } if( EditFlags.Modeless ) { incrementMark( result ); return ERR_NO_ERR; } } s = eatSpace( result, true ); if( s == NULL ) { return( ERR_NOT_THAT_MANY_WORDS ); } /* * because we are looking at the previous character in the following loop, * we have to be careful in case we are sitting at the start of a line. */ if( result->column > 1 ) { block_type = charType( *s, big ); while( charType( *(s - 1), big ) == block_type ) { s = decrementMark( result ); if( s == NULL || result->column == 1 ) { break; } } } return( ERR_NO_ERR ); } /* MarkStartOfNextWordBackward */
/* * MarkStartOfNextWordForward: given a mark curr which denotes a logical * current position, we return the location of the next logical word in * the result mark, or ERR_NOT_THAT_MANY_WORDS if there are no more logical * words in the file. Note that this will span lines. */ vi_rc MarkStartOfNextWordForward( i_mark *result, i_mark *curr, bool big ) { char *s; btype block_type; noWrap = false; *result = *curr; s = ptrFromMark( result ); while( s == NULL ) { s = nextLine( result ); if( s == NULL ) { return( ERR_NOT_THAT_MANY_WORDS ); } if( EditFlags.Modeless ) { return( ERR_NO_ERR ); } } block_type = charType( *s, big ); while( charType( *s, big ) == block_type ) { s = incrementMark( result ); if( s == NULL ) { if( EditFlags.OperatorWantsMove || EditFlags.Modeless ) { return( ERR_NO_ERR ); } s = nextLine( result ); if( s == NULL ) { return( ERR_NOT_THAT_MANY_WORDS ); } break; } } eatSpace( result, false ); return( ERR_NO_ERR ); } /* MarkStartOfNextWordForward */
/*----------------------------------------------------------------------------*/ int firstPassCommandsManager(Data * data, char * tag){ char command[5]; int operandNum; int commandIndex; if (sscanf(data->line, "%4s", command) == 0){ printf("[Error] on line %d:- command error invalid \n", data->lc); data->containError=TRUE; return 0; } commandIndex = getCommandIndex(command); if (commandIndex == -1){ printf("[Error] on line %d:- command error not defined\n", data->lc); data->containError=TRUE; return 0; } data->line+=strlen(command); if(!isspace(*(data->line))){ printf("[Error] on line %d:- extra characters or no space after command name\n", data->lc); data->containError=TRUE; return 0; } eatSpace(data); operandNum = getNumOfOperands(commandIndex); if(operandNum==0){ return checkZeroOperands(data,tag); }else if(operandNum==1){ return checkOneOperands(data, tag); }else if(operandNum==2){ return checkTwoOperands(data, tag,command); } return 0; }
bool jsion_cpp::ini::WriteString( std::string sectionName, std::string keyName, std::string keyValue, std::string fileName) { std::ifstream fs_ini(fileName.c_str(), std::ios_base::in); std::ofstream fs_ini_bak((fileName + ".bak").c_str(), std::ios_base::out | std::ios_base::trunc); if (fs_ini && fs_ini_bak) { std::string line = ""; std::string section = std::string("[") + sectionName + std::string("]"); bool written = false; while (getline(fs_ini, line)) { fs_ini_bak << line << std::endl; if (!written && 0 == section.compare(eatSpace(line))) { while (getline(fs_ini, line)) { std::string temp = eatSpace(line); if (temp.length() > 0 && '[' == temp[0]) { fs_ini_bak << keyName << " = " << keyValue << std::endl; fs_ini_bak << line << std::endl; written = true; break; } if (temp.length() > keyName.length() && 0 == temp.compare(0, keyName.length(), keyName) && '=' == temp[keyName.length()]) { fs_ini_bak << keyName << " = " << keyValue << std::endl; written = true; break; } fs_ini_bak << line << std::endl; }// end while if (!written) { fs_ini_bak << keyName << " = " << keyValue << std::endl; written = true; }// end if }// end if written && section == temp }// end while if (!written) { fs_ini_bak << section << std::endl; fs_ini_bak << keyName << " = " << keyValue << std::endl; written = true; } fs_ini.close(); fs_ini_bak.close(); } else { return false; } std::ifstream fs_ini_bak_new((fileName + ".bak").c_str(), std::ios_base::in); std::ofstream fs_ini_new(fileName.c_str(), std::ios_base::out | std::ios_base::trunc); if (fs_ini_bak_new && fs_ini_new) { std::string line = ""; while (getline(fs_ini_bak_new, line)) { fs_ini_new << line << std::endl; } fs_ini_bak_new.close(); fs_ini_new.close(); } else { return false; } return true; }
/* Get the next ESP input token. input points to the next input token. parse->token will hold the parsed token. The function returns the token id. */ static int getEspToken(EspParse *parse) { char *start, *end, *next; int tid, done, c; start = next = parse->next; end = &start[slen(start)]; mprFlushBuf(parse->token); tid = ESP_TOK_LITERAL; for (done = 0; !done && next < end; next++) { c = *next; switch (c) { case '<': if (next[1] == '%' && ((next == start) || next[-1] != '\\')) { next += 2; if (mprGetBufLength(parse->token) > 0) { next -= 3; } else { next = eatSpace(parse, next); if (*next == '=') { /* <%= directive */ tid = ESP_TOK_EXPR; next = eatSpace(parse, ++next); while (next < end && !(*next == '%' && next[1] == '>' && next[-1] != '\\')) { if (*next == '\n') parse->lineNumber++; if (!addChar(parse, *next++)) { return ESP_TOK_ERR; } } } else if (*next == '@') { tid = ESP_TOK_CONTROL; next = eatSpace(parse, ++next); while (next < end && !(*next == '%' && next[1] == '>' && next[-1] != '\\')) { if (*next == '\n') parse->lineNumber++; if (!addChar(parse, *next++)) { return ESP_TOK_ERR; } } } else { tid = ESP_TOK_CODE; while (next < end && !(*next == '%' && next[1] == '>' && next[-1] != '\\')) { if (*next == '\n') parse->lineNumber++; if (!addChar(parse, *next++)) { return ESP_TOK_ERR; } } } if (*next && next > start && next[-1] == '-') { /* Remove "-" */ mprAdjustBufEnd(parse->token, -1); mprAddNullToBuf(parse->token); next = eatNewLine(parse, next + 2) - 1; } else { next++; } } done++; } else { if (!addChar(parse, c)) { return ESP_TOK_ERR; } } break; case '@': if ((next == start) || next[-1] != '\\') { if (next[1] == '@' || next[1] == '*') { next += 2; if (mprGetBufLength(parse->token) > 0) { next -= 3; } else { tid = next[-1] == '@' ? ESP_TOK_VAR : ESP_TOK_FIELD; next = eatSpace(parse, next); while (isalnum((int) *next) || *next == '_') { if (*next == '\n') parse->lineNumber++; if (!addChar(parse, *next++)) { return ESP_TOK_ERR; } } next--; } done++; } } break; case '\n': parse->lineNumber++; /* Fall through */ case '\r': default: if (c == '\"' || c == '\\') { if (!addChar(parse, '\\')) { return ESP_TOK_ERR; } } if (!addChar(parse, c)) { return ESP_TOK_ERR; } break; } } if (mprGetBufLength(parse->token) == 0) { tid = ESP_TOK_EOF; } parse->next = next; return tid; }