char* nextToken(char* cur, byte* val, TokenType* ttype) { *ttype = TT_END; while (*cur) { if (*cur == ' ') { cur++; continue; } else if (isHex(*cur) && isHex(*(cur+1))) { *val = (hexVal(*cur) << 4) | hexVal(*(cur+1)); *ttype = TT_OPCODE; cur += 2; break; } else if (*cur == 'd') { *ttype = TT_D; cur++; break; } else if (*cur == 'e') { *ttype = TT_E; cur++; break; } else if ((*cur == 'n') && (*(cur+1) == 'n')) { *ttype = TT_NN; cur += 2; break; } else if (*cur == 'n') { *ttype = TT_N; cur++; break; } else { fatal2("Unknown char at ", cur); } }; return cur; }
/*! Load the picture data \param format the Picture Format. \param image_name the name of the image. Must be unique. \param imgProps the RTF properties for the image. \return true if success, otherwise false. \desc Load the picture data from the flow. Will move the file position and assume proper RTF file structure. It will take care of inserting the picture into the document. \todo TODO: We assume the data comes in hex. Check this assumption as we might have to handle binary data as well \see IE_Imp_RTF::HandlePicture */ bool IE_Imp_RTF::LoadPictData(PictFormat format, const char * image_name, struct RTFProps_ImageProps & imgProps, bool isBinary, long binaryLen) { // first, we load the actual data into a buffer bool ok; bool retval = true; const UT_uint16 chars_per_byte = 2; const UT_uint16 BITS_PER_BYTE = 8; const UT_uint16 bits_per_char = BITS_PER_BYTE / chars_per_byte; UT_ByteBuf pictData; UT_uint16 chLeft = chars_per_byte; UT_Byte pic_byte = 0; FG_Graphic* pFG = NULL; UT_Error error = UT_OK; unsigned char ch; if (!isBinary) { if (!ReadCharFromFile(&ch)) { retval = false; goto cleanup; } while (ch != '}') { int digit; if (!hexVal(ch, digit)) { retval = false; goto cleanup; } pic_byte = (pic_byte << bits_per_char) + digit; // if we have a complete byte, we put it in the buffer if (--chLeft == 0) { pictData.append(&pic_byte, 1); chLeft = chars_per_byte; pic_byte = 0; } if (!ReadCharFromFile(&ch)) { retval = false; goto cleanup; } } } else { UT_ASSERT_HARMLESS(binaryLen); UT_DEBUGMSG(("Loading binary data image of %ld bytes\n", binaryLen)); for (long i = 0; i < binaryLen; i++) { if (!ReadCharFromFileWithCRLF(&ch)) { retval = false; goto cleanup; } pictData.append(&ch, 1); } } // We let the caller handle this SkipBackChar(ch); error = IE_ImpGraphic::loadGraphic(pictData, iegftForRTF(format), &pFG); if ((error == UT_OK) && pFG) { imgProps.width = static_cast<UT_uint32>(pFG->getWidth ()); imgProps.height = static_cast<UT_uint32>(pFG->getHeight ()); // Not sure whether this is the right way, but first, we should // insert any pending chars if (!FlushStoredChars(true)) { UT_DEBUGMSG(("Error flushing stored chars just before inserting a picture\n")); DELETEP(pFG); return false; } ok = InsertImage (pFG, image_name, imgProps); DELETEP(pFG); if (!ok) { return false; } } else { // if we're not inserting a graphic, we should destroy the buffer UT_DEBUGMSG (("no translator found: %d\n", error)); } cleanup: return retval; }