// Load FtglOutlineFont void FtglOutlineFont::loadFont() { if (isLoaded()) return; // Check for required parameters if( filename() == nullptr ) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "No ttf file" << std::endl; } return; } // Generate filename const size_t FONTPATHNAME_LENGTH = 256; char fontPathname[FONTPATHNAME_LENGTH]; if (fontDirectory() != nullptr) lcStrcpy(fontPathname, FONTPATHNAME_LENGTH, fontDirectory()); else lcStrcpy(fontPathname, FONTPATHNAME_LENGTH, "./"); lcStrcat(fontPathname, FONTPATHNAME_LENGTH, filename()); FTGLOutlineFont* ftglFont = new FTGLOutlineFont(fontPathname); if (ftglFont != nullptr && !ftglFont->Error()) { // set the face size and return the pointer, then tell our base class that we have a loaded font ftglFont->FaceSize(getFaceSize()); ftgl(ftglFont); setFontLoaded(); } else { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "FtglOutlineFont::loadFont() - font did not load correctly: file: \""; std::cerr << fontPathname << "\""; std::cerr << std::endl; } std::exit(1); } }
//------------------------------------------------------------------------------ // Function collect the data to be displayed //------------------------------------------------------------------------------ void Display::updateDisplay() { Basic::IoData* ioData = nullptr; if (ioHandler != nullptr) ioData = ioHandler->getInputData(); // Item/channel mapping for (unsigned int i = 0; i < TBL_SIZE; i++) { bool ok = false; if (types[i] != NONE && ioData != nullptr) { // Set the data if (types[i] == AI) { LCreal v = 0; ok = ioData->getAnalogInput(channels[i], &v); if (ok) { table_ai[i] = v; table_typeRo[i] = R_AI; } } else if (types[i] == DI) { bool flg = false; ok = ioData->getDiscreteInput(channels[i], &flg); if (ok) { if (flg) table_typeRo[i] = R_DI_1; else table_typeRo[i] = R_DI_0; } } // Create the label { // if not provided; make the default label if (!labelFlags[i]) { char cbuff[32]; if (types[i] == AI) std::sprintf(cbuff, "AI(%03d)", channels[i]); else std::sprintf(cbuff, "DI(%03d)", channels[i]); lcStrcpy(labels[i], sizeof(labels[i]), cbuff); } // copy the label with a ':' lcStrcpy(labelBuffs[i], sizeof(labelBuffs[i]), labels[i]); lcStrcat(labelBuffs[i], sizeof(labelBuffs[i]), ":"); } } if (!ok) { table_ai[i] = 0; } } }
//------------------------------------------------------------------------------ // catStr() -- appends a copy of 's' to the end of this string. //------------------------------------------------------------------------------ void String::catStr(const char* s) { // early out if nothing to append if (s == 0) return; // if this string was empty then we're really just setStr() if ( isEmpty() ) { setStr(s); return; } // Have new text to append to the original text size_t l = n + strlen(s); if (l >= nn) { char* t = str; nn = (l+1); str = new char[nn]; lcStrcpy(str,nn,t); delete[] t; } lcStrcat(str,nn,s); n = l; }
//------------------------------------------------------------------------------ // Open the data file //------------------------------------------------------------------------------ bool FileReader::openFile() { // When we're already open, just return if (isOpen()) return true; // local flags (default is success) bool tOpened = true; bool tFailed = false; // Need a file name if (filename == nullptr || filename->len() == 0) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "FileReader::openFile(): Unable to open data file: no file name" << std::endl; } tOpened = false; tFailed = true; } else { //--- // Allocate space for the full file name //--- size_t nameLength = 0; if (pathname != nullptr) { nameLength += pathname->len(); // add the length of the path name nameLength += 1; // add a character for the slash } nameLength += filename->len(); // add the length of the file name nameLength += 1; // Add one for the null(0) at the end of the string char* fullname = new char[nameLength]; fullname[0] = '\0'; //--- // Create the (initial) full file name //--- if (pathname != nullptr && pathname->len() > 0) { lcStrcat(fullname, nameLength ,*pathname); lcStrcat(fullname, nameLength, "/"); } lcStrcat(fullname,nameLength,*filename); //--- // Make sure that it exists //--- bool validName = doesFileExist(fullname); //--- // When we have a valid file name ... //--- if ( validName ) { //--- // Make sure we have an input stream //--- if (sin == nullptr) sin = new std::ifstream(); //--- // Open the file (binary input mode) //--- sin->open(fullname, std::ios_base::in | std::ios_base::binary ); if (isMessageEnabled(MSG_INFO)) { std::cout << "FileReader::openFile() Opening data file = " << fullname << std::endl; } if (sin->fail()) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "FileReader::openFile(): Failed to open data file: " << fullname << std::endl; } tOpened = false; tFailed = true; } } delete[] fullname; } fileOpened = tOpened; fileFailed = tFailed; return fileOpened; }
// Load the font for one character GLubyte* BitmapFont::loadTypeFace(const GLint index, const GLenum reverse) { // If no font to load, return if (fontMap[index] == 0) return 0; // Create the font file name const size_t FONTPATHNAME_LENGTH = 256; char fontPathname[FONTPATHNAME_LENGTH]; if (fontDirectory() != 0) lcStrcpy(fontPathname, FONTPATHNAME_LENGTH, fontDirectory()); else lcStrcpy(fontPathname, FONTPATHNAME_LENGTH, "./"); lcStrcat(fontPathname, FONTPATHNAME_LENGTH, fontMap[index]); // Open the font file FILE* fp = 0; if( (fp = fopen(fontPathname, "r")) ==0 ) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "BitmapFont::loadTypeFace: unable to open font file: " << fontPathname << std::endl; } return 0; } // used to store the num of input items successfully matched and assigned // by fscanf function int nItemsMatched; // Calculate the size of the font unsigned int width1; nItemsMatched = fscanf(fp, "%u\n", &width1); unsigned int height1; nItemsMatched = fscanf(fp, "%u\n", &height1); unsigned int numBytesWide = int(ceil(double(width1) / 8.0)); unsigned int numFileBytes = numBytesWide * height1; unsigned int numFontBytes = numBytesWide * getBitmapHeight(); GLubyte* bitmap = new GLubyte[numFontBytes]; unsigned int i; // index // Pad rest of the height unsigned int diff = numFontBytes - numFileBytes; for (i = 0; i < diff; i++) { bitmap[i] = reverse ? 255 : 0; } // Read in the bitmap bytes for (; i < numFontBytes; i++) { int value; nItemsMatched = fscanf(fp, "0x%x\n", &value); bitmap[i] = reverse ? GLubyte(~value) : GLubyte(value); } fclose(fp); // Reverse the bitmap reverseBitmapOrder(bitmap, numFontBytes, numBytesWide); return bitmap; }
//------------------------------------------------------------------------------ // Open the data file //------------------------------------------------------------------------------ bool PrintHandler::openFile() { // When we're already open, just return if (isOpen()) return true; // If we don't have a file name then we're using the standard output if (filename == 0 || filename->len() == 0) return true; // clear the old 'full' file name setFullFilename(0); // local flags (default is success) bool tOpened = true; bool tFailed = false; //--- // Allocate space for the full file name //--- size_t nameLength = 0; if (pathname != 0) { nameLength += pathname->len(); // add the length of the path name nameLength += 1; // add a character for the slash } nameLength += filename->len(); // add the length of the file name nameLength += 4; // add characters for possible version number, "_V99" nameLength += 1; // Add one for the null(0) at the end of the string char* fullname = new char[nameLength]; fullname[0] = '\0'; //--- // Create the (initial) full file name //--- if (pathname != 0 && pathname->len() > 0) { lcStrcat(fullname, nameLength ,*pathname); lcStrcat(fullname, nameLength, "/"); } lcStrcat(fullname,nameLength,*filename); //--- // Make sure that it doesn't already exist (we don't want to over write good data). //--- bool validName = !doesFileExist(fullname); if ( !validName ) { // If the file already exists, try appending a version number "v99" .. char* origname = new char[nameLength]; lcStrcpy(origname, nameLength, fullname); validName = false; for (unsigned int i = 1; i <= 99 && !validName; i++) { std::sprintf(fullname, "%s_v%02d", origname, i); validName = !doesFileExist(fullname); } if ( !validName ) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "PrintHandler::openFile(): All version of the data file already exists: " << origname << std::endl; } tOpened = false; tFailed = true; } delete[] origname; } //--- // When we have a valid file name ... //--- if ( validName ) { // The file name with the path and version number setFullFilename(fullname); //--- // Make sure we have an output stream //--- if (sout == 0) sout = new std::ofstream(); //--- // Open the file //--- sout->open(fullname, std::ios_base::out); if (isMessageEnabled(MSG_INFO)) { std::cout << "PrintHandler::openFile() Opening data file = " << fullname << std::endl; } if (sout->fail()) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "PrintHandler::openFile(): Failed to open data file: " << fullname << std::endl; } tOpened = false; tFailed = true; } } delete[] fullname; fileOpened = tOpened; fileFailed = tFailed; return fileOpened; }
//------------------------------------------------------------------------------ // updateData() -- Update the log file //------------------------------------------------------------------------------ bool Logger::openFile() { // local flags (default is success) bool tOpened = true; bool tFailed = false; // Need a file name if (filename->len() == 0) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "Logger::openFile(): Unable to open logger file: no file name" << std::endl; } tOpened = false; tFailed = true; } else { //--- // Allocate space for the full file name //--- size_t len = pathname->len(); // start with the length of the path name len += 1; // add a character for the slash len += filename->len(); // add the length of the file name len += 4; // add characters for possible version number, "_V99" len += 1; // Add one for the null(0) at the end of the string const size_t NAME_LENGTH = len; char* fullname = new char[NAME_LENGTH]; fullname[0] = '\0'; //--- // Create the (initial) full file name //--- if (pathname->len() > 0) { lcStrcat(fullname,NAME_LENGTH,*pathname); lcStrcat(fullname,NAME_LENGTH,"/"); } lcStrcat(fullname,NAME_LENGTH,*filename); //--- // Make sure that it doesn't already exist (we don't want to over write good data). //--- bool validName = !doesFileExist(fullname); if ( !validName ) { // If the file already exists, try appending a version number "v99" .. char* origname = new char[NAME_LENGTH]; lcStrcpy(origname, NAME_LENGTH, fullname); validName = false; for (unsigned int i = 1; i <= 99 && !validName; i++) { sprintf(fullname, "%s_v%02d", origname, i); validName = !doesFileExist(fullname); } if ( !validName ) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "Logger::openFile(): All version of the logger file already exists: " << origname << std::endl; } tOpened = false; tFailed = true; } delete[] origname; } if ( validName ) { //--- // Make sure we have an output stream //--- if (lout == 0) lout = new std::ofstream(); //--- // Open the file //--- if (isMessageEnabled(MSG_INFO)) { std::cout << "Logger::openFile() Opening log file = " << fullname << std::endl; } lout->open(fullname); if (lout->fail()) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "Logger::openFile(): Failed to open log file: " << fullname << std::endl; } tOpened = false; tFailed = true; } else if (topLine != 0) { *lout << *topLine << std::endl; } } delete[] fullname; } opened = tOpened; failed = tFailed; return opened; }