bool BaseLoader::readLine(char* buf, int size, FILE* f) { buf[0] = '\0'; if (fgets(buf, size, f) == NULL) return false; if ((int)strlen(buf)==size-1 && buf[size-1] != '\n') skipToEOL(f); return true; }
void IniParser::readToken() { //printf("(IniParser::readToken) token-type:%s line:%d, col:%d\n", __ini_tokenName(_token), _token.getLine(), _token.getColumn()); switch (_token.getType()) { case QuotedStringToken: readQuotedStringToken(); nextChar(); break; case StringToken: readStringToken(); break; case NumberToken: readNumberToken(); break; case DoubleToken: nextChar(); readDoubleToken(); break; case EqualsToken: nextChar(); break; case CommentToken: skipToEOL(); //nextChar(); // Bad to get the next char, as it could be the EOL needed to terminate the current identifier break; case NodeToken: readNodeToken(); nextChar(); break; case EOLToken: nextChar(); break; case EOFToken: // Do nothing break; default: throw IniParserException(PARSERERROR_PARSER, "IniParser::readToken", NULL, this, "Unhandled token: %s", __ini_tokenName(_token)); break; } _handler->handleToken(_token); }
bool SphereLoader::load(const char *filename) { std::string fname = filename; if (!sofa::helper::system::DataRepository.findFile(fname)) return false; char cmd[64]; FILE* file; static const char* SPH_FORMAT = "sph 1.0"; if ((file = fopen(fname.c_str(), "r")) == NULL) { std::cout << "ERROR: cannot read file '" << filename << "'. Exiting..." << std::endl; return false; } // std::cout << "Loading model'" << filename << "'" << std::endl; int totalNumSpheres=0; // Check first line if (fgets(cmd, 7, file) == NULL || !strcmp(cmd,SPH_FORMAT)) { fclose(file); return false; } skipToEOL(file); while (fscanf(file, "%s", cmd) != EOF) { if (!strcmp(cmd,"nums")) { if (fscanf(file, "%d", &totalNumSpheres) == EOF) std::cerr << "Error: SphereLoader: fscanf function has encountered an error." << std::endl; setNumSpheres(totalNumSpheres); } else if (!strcmp(cmd,"sphe")) { int index; double cx=0,cy=0,cz=0,r=1; if (fscanf(file, "%d %lf %lf %lf %lf\n", &index, &cx, &cy, &cz, &r) == EOF) std::cerr << "Error: SphereLoader: fscanf function has encountered an error." << std::endl; addSphere((SReal)cx,(SReal)cy,(SReal)cz,(SReal)r); ++totalNumSpheres; } else if (cmd[0]=='#') { skipToEOL(file); } else // it's an unknown keyword { printf("%s: Unknown Sphere keyword: %s\n", filename, cmd); skipToEOL(file); } } // printf("Model contains %d spheres\n", totalNumSpheres); (void) fclose(file); return true; }
bool SphereLoader::load() { const char* filename = m_filename.getFullPath().c_str(); std::string fname = std::string(filename); if (!sofa::helper::system::DataRepository.findFile(fname)) return false; char cmd[64]; FILE* file; static const char* SPH_FORMAT = "sph 1.0"; if ((file = fopen(fname.c_str(), "r")) == NULL) { std::cout << "ERROR: cannot read file '" << filename << "'. Exiting..." << std::endl; return false; } helper::vector<sofa::defaulttype::Vec<3,SReal> >& my_positions = *positions.beginEdit(); helper::vector<SReal>& my_radius = *radius.beginEdit(); // std::cout << "Loading model'" << filename << "'" << std::endl; int totalNumSpheres=0; // Check first line if (fgets(cmd, 7, file) == NULL || !strcmp(cmd,SPH_FORMAT)) { fclose(file); return false; } skipToEOL(file); while (fscanf(file, "%s", cmd) != EOF) { if (!strcmp(cmd,"nums")) { int total; if (fscanf(file, "%d", &total) == EOF) std::cerr << "Error: SphereLoader: fscanf function has encountered an error." << std::endl; my_positions.reserve(total); } else if (!strcmp(cmd,"sphe")) { int index; double cx=0,cy=0,cz=0,r=1; if (fscanf(file, "%d %lf %lf %lf %lf\n", &index, &cx, &cy, &cz, &r) == EOF) std::cerr << "Error: SphereLoader: fscanf function has encountered an error." << std::endl; my_positions.push_back(Vector3((SReal)cx,(SReal)cy,(SReal)cz)); my_radius.push_back((SReal)r); ++totalNumSpheres; } else if (cmd[0]=='#') { skipToEOL(file); } else // it's an unknown keyword { printf("%s: Unknown Sphere keyword: %s\n", filename, cmd); skipToEOL(file); } } // printf("Model contains %d spheres\n", totalNumSpheres); (void) fclose(file); if (d_scale.isSet()) { const SReal sx = d_scale.getValue()[0]; const SReal sy = d_scale.getValue()[1]; const SReal sz = d_scale.getValue()[2]; for (unsigned int i = 0; i < my_radius.size(); i++) { my_radius[i] *= sx; } for (unsigned int i = 0; i < my_positions.size(); i++) { my_positions[i].x() *= sx; my_positions[i].y() *= sy; my_positions[i].z() *= sz; } } if (d_translation.isSet()) { const SReal dx = d_translation.getValue()[0]; const SReal dy = d_translation.getValue()[1]; const SReal dz = d_translation.getValue()[2]; for (unsigned int i = 0; i < my_positions.size(); i++) { my_positions[i].x() += dx; my_positions[i].y() += dy; my_positions[i].z() += dz; } } positions.endEdit(); radius.endEdit(); return true; }
bool SphereLoader::Load(const std::string& filename, SphereLoaderDataHook& data) { /// Make sure that fscanf() uses a dot '.' as the decimal separator. helper::system::TemporaryLocale locale(LC_NUMERIC, "C"); std::string fname = filename; if (!sofa::helper::system::DataRepository.findFile(fname)) return false; char cmd[64]; FILE* file; static const char* SPH_FORMAT = "sph 1.0"; if ((file = fopen(fname.c_str(), "r")) == NULL) { msg_error("SphereLoader") << "ERROR: cannot read file '" << filename << "'. (Aborting)"; return false; } int totalNumSpheres=0; /// Check first line if (fgets(cmd, 7, file) == NULL || !strcmp(cmd,SPH_FORMAT)) { fclose(file); return false; } skipToEOL(file); std::ostringstream cmdScanFormat; cmdScanFormat << "%" << (sizeof(cmd) - 1) << "s"; while (fscanf(file,cmdScanFormat.str().c_str(), cmd) != EOF) { if (!strcmp(cmd,"nums")) { if (fscanf(file, "%d", &totalNumSpheres) == EOF) msg_error("SphereLoader") << "fscanf function has encountered an error." ; data.setNumSpheres(totalNumSpheres); } else if (!strcmp(cmd,"sphe")) { int index; double cx=0,cy=0,cz=0,r=1; if (fscanf(file, "%d %lf %lf %lf %lf\n", &index, &cx, &cy, &cz, &r) == EOF) msg_error("SphereLoader") << "fscanf function has encountered an error." ; data.addSphere((SReal)cx,(SReal)cy,(SReal)cz,(SReal)r); ++totalNumSpheres; } else if (cmd[0]=='#') { skipToEOL(file); } else // it's an unknown keyword { msg_info("SphereLoader") << "'"<< filename << "' unknown Sphere keyword: " << cmd ; skipToEOL(file); } } (void) fclose(file); return true; }