/// <summary> Evaluate an RTF control that needs special processing. /// /// </summary> /// <param name="ipfn"> /// </param> /// <returns> /// </returns> private ErrorRtf ParseSpecialKeyword(IPFN ipfn) { ErrorRtf ret = ErrorRtf.OK; if (!UtilStrByteMode.isLocaleDefLangDBCS()) { if (_destState == RDS.SKIP && ipfn != IPFN.BIN) { /* if we're skipping, and it's not */ return(ret); /* the \bin keyword, ignore it. */ } if (ipfn == IPFN.FONT || ipfn == IPFN.CHARSET || ipfn == IPFN.UNICODE) { return(ret); } } else { if (_destState == RDS.SKIP && ipfn != IPFN.BIN && ipfn != IPFN.FONT && ipfn != IPFN.CHARSET && ipfn != IPFN.UNICODE) { return(ret); } } if (ipfn == IPFN.BIN) { _internalState = RIS.BIN; _cbBin = _lParam; } else if (ipfn == IPFN.SKIP_DEST) { _skipDestIfUnk = true; } else if (ipfn == IPFN.HEX) { _internalState = RIS.HEX; } else if (ipfn == IPFN.FONT) { _fontNum = (int)_lParam; } else if (ipfn == IPFN.CHARSET) { _charsetTable[_fontNum] = (int)_lParam; } else if (ipfn == IPFN.UNICODE) { _internalState = RIS.UNICODE; } else { ret = ErrorRtf.BAD_TABLE; } return(ret); }
/// <summary> /// Get the first year (A.D.) of a specified Japanese era /// This method is modeled after "date_jpn_yr_4_a" function in /// "\mglocal\jpn\jpndate_jpn.cpp". /// </summary> /// <param name = "ucp_str:">name of a specified Japanese era /// </param> /// <param name = "s_len:">length (the number of bytes) of ucp_str /// </param> /// <returns> year (A.D.) /// </returns> private int date_jpn_yr_4_a(String ucp_str, int s_len) { int s_maxgen = MaxGengo - 1; String GengoStrWork; if (s_len > 0) // if s_len == 0, s_maxgen is max. { if (s_len > 1) { ucp_str = UtilStrByteMode.leftB(ucp_str, s_len); } for (; s_maxgen > 0; s_maxgen--) { if (s_len == 1) { if (ucp_str.Equals(GengoStr[s_maxgen][IDX_UPPERCASE_ALPHA]) || ucp_str.Equals(GengoStr[s_maxgen][IDX_LOWERCASE_ALPHA])) { break; } } else if (s_len > 1) { GengoStrWork = UtilStrByteMode.leftB(GengoStr[s_maxgen][IDX_KANJI], s_len); if (ucp_str.Equals(GengoStrWork)) { break; } } } } if (s_maxgen > 0) { return(StartDayOfGengo[s_maxgen][IDX_YEAR]); } else { GengoStrWork = ucp_str.Substring(0, 1); if (GengoStrWork.Equals(" ") || GengoStrWork.Equals("?")) { return(1); } else { return(0); } } }
/// <summary> /// Get the name of an era in date string /// </summary> /// <param name = "strDate:">string of input strDate /// </param> /// <param name = "strPicture:">string of picture /// </param> /// <param name = "intStartPos:">start position to search /// </param> /// <returns> name of an era /// </returns> private static String getEraNameStrInDate(String strDate, String strPicture, int intStartPos) { String strBuff = null; int intLetters = 0; // A DBCS character in "strDate" consumes two characters in "strPicture". // If "strDate" contains DBCS, the position of "strPicture" has to skip next index. // This variable indicates the number of skipped indexes. int intPicIdxOfs = 0; for (int i = intStartPos; i + intPicIdxOfs < strPicture.Length; i++) { if (strPicture[i + intPicIdxOfs] == PICInterface.PIC_JY1) { intLetters = 1; } // the number of charaters, not bytes else if (strPicture[i + intPicIdxOfs] == PICInterface.PIC_JY2) { intLetters = 1; } // the number of charaters, not bytes else if (strPicture[i + intPicIdxOfs] == PICInterface.PIC_JY4) { intLetters = 2; } // the number of charaters, not bytes else { if (i < strDate.Length) { // If "strDate" contains DBCS, the position of "strPicture" has to skip next index. if (UtilStrByteMode.isHalfWidth(strDate[i]) == false && UtilStrByteMode.isHalfWidth(strPicture[i + intPicIdxOfs])) { intPicIdxOfs++; } } continue; } //strBuff = utilStrByteMode.midB(strDate, i, intLetters); strBuff = strDate.Substring(i, intLetters); break; // exit loop } return(strBuff); }
/// <summary> Constructor /// /// </summary> public Rtf() { _stack = new Stack(); _group = 0; _cbBin = 0; _lParam = 0; _outputOnce = false; _skipDestIfUnk = false; _processCrlfSpecial = false; _destState = RDS.NORM; _internalState = RIS.NORM; _fontNum = 0; if (UtilStrByteMode.isLocaleDefLangDBCS()) { setCodePageTable(); } }
public static String memmove(String dest, int destCount, String src, int srcCount, int len) { StringBuilder outVal = new StringBuilder(dest.Length + len); if (UtilStrByteMode.isLocaleDefLangJPN() && dest.Length < destCount) { outVal.Append(new String(' ', destCount)); } else { outVal.Append(dest.Substring(0, destCount)); } outVal.Append(src.Substring(srcCount, len)); try { outVal.Append(dest.Substring(outVal.Length)); } catch (ArgumentOutOfRangeException) { //doNothing } return(outVal.ToString()); }
/// <summary> Converts Rtf Text to Plain Text /// Step 1: Isolate RTF keywords and send them to ParseKeyword; Push and pop state at the start and end of /// RTF groups Send text to ParseChar for further processing. /// /// </summary> /// <param name="rtfTxt"> /// </param> /// <param name="outputTxt"> /// </param> /// <returns> /// </returns> public ErrorRtf toTxt(String rtfTxt, StringBuilder outputTxt) { long cNibble = 2; long b = 0; int currPos = 0; bool skipNewline = false; long blobStrLen; char blobChar; ErrorRtf ec; byte[] dbcsBytes = new byte[2]; bool skipParseChar = false; int charset = 0; int codePage = 0; _outputOnce = false; _processCrlfSpecial = false; blobStrLen = rtfTxt.Length; _index = 0; _destState = RDS.NORM; if (rtfTxt == null || blobStrLen == 0 || !isRtf(rtfTxt)) { return(ErrorRtf.OK); } while (_index < blobStrLen) { blobChar = rtfTxt[_index]; _index++; if (_group < 0) { return(ErrorRtf.STACK_UNDERFLOW); } /* if we're parsing binary data, handle it directly */ if (_internalState == RIS.BIN) { if ((ec = ParseChar(blobChar, outputTxt)) != ErrorRtf.OK) { return(ec); } } else { switch (blobChar) { case '{': skipNewline = false; if ((ec = PushState()) != ErrorRtf.OK) { return(ec); } break; case '}': skipNewline = true; if ((ec = PopState()) != ErrorRtf.OK) { return(ec); } break; case '\\': skipNewline = false; if ((ec = ParseKeyword(rtfTxt, outputTxt)) != ErrorRtf.OK) { return(ec); } break; case (char)RtfChar.LF: case (char)RtfChar.CR: /* cr and lf are noise characters... */ if (_processCrlfSpecial) { /* Once we reach the 0x0a while ProcessCRLFSpecial_, reset the ProcessCRLFSpecial_ */ if (blobChar == (char)RtfChar.LF) { _processCrlfSpecial = false; } } else { /*---------------------------------------------------------------*/ /* skip new lines coming only from the RTF header 1/1/98 - #2390 */ /*---------------------------------------------------------------*/ /* Skip the LF (0x0a) if we are not in the ProcessCRLFSpecial_ */ if (blobChar == (char)RtfChar.LF || (blobChar == (char)RtfChar.CR && skipNewline && !_outputOnce)) { break; } } goto default; default: if (blobChar != (char)RtfChar.CR) { skipNewline = false; } if (_internalState == RIS.NORM) { if ((ec = ParseChar(blobChar, outputTxt)) != ErrorRtf.OK) { return(ec); } } else if (_internalState == RIS.UNICODE) { if ((ec = ParseChar((char)_lParam, outputTxt)) != ErrorRtf.OK) { return(ec); } _internalState = RIS.NORM; } else { /* parsing hex data */ if (_internalState != RIS.HEX) { return(ErrorRtf.ASSERTION); } b = b << 4; if (Char.IsDigit(blobChar)) { b += (int)blobChar - (int)'0'; } else { if (Char.IsLower(blobChar)) { if (blobChar < 'a' || blobChar > 'f') { return(ErrorRtf.INVALID_HEX); } b += 10 + (int)blobChar - (int)'a'; } else { if (blobChar < 'A' || blobChar > 'F') { return(ErrorRtf.INVALID_HEX); } b += 10 + (int)blobChar - (int)'A'; } } cNibble--; if (cNibble == 0) { if (UtilStrByteMode.isLocaleDefLangDBCS()) { charset = getCharset(_fontNum); if (!skipParseChar && is1stByte(b, charset)) // leading byte of a double-byte character { dbcsBytes[0] = (byte)b; dbcsBytes[1] = (byte)0; skipParseChar = true; } else { if (skipParseChar && is2ndByte(b, charset)) // trailing byte of a double-byte character { dbcsBytes[1] = (byte)b; } else // single-byte character { dbcsBytes[0] = (byte)b; dbcsBytes[1] = (byte)0; } // convert DBCS to Unicode codePage = getCodePage(charset); string workStr = Encoding.GetEncoding(codePage).GetString(dbcsBytes, 0, 2); char[] workChar = workStr.ToCharArray(); b = (long)workChar[0]; skipParseChar = false; } } if (!skipParseChar) { if ((ec = ParseChar((char)b, outputTxt)) != ErrorRtf.OK) { return(ec); } } cNibble = 2; b = 0; _internalState = RIS.NORM; } } /* end else (ris != risNorm) */ break; } /* switch */ } /* else (ris != risBin) */ } /* while */ if (_group < 0L) { return(ErrorRtf.STACK_UNDERFLOW); } if (_group > 0L) { return(ErrorRtf.UNMATCHED_BRACE); } /*-------------------------------------------------------------------*/ /* Eliminate suffix of carrige return + line feed */ /* (Check last characters - just in case format is not the expected) */ /*-------------------------------------------------------------------*/ currPos = outputTxt.Length; if (currPos >= 3 && (outputTxt[currPos - 3] == (char)RtfChar.CR && outputTxt[currPos - 2] == (char)RtfChar.LF && outputTxt[currPos - 1] == (char)RtfChar.CR || outputTxt[currPos - 3] == (char)RtfChar.LF && outputTxt[currPos - 2] == (char)RtfChar.CR && outputTxt[currPos - 1] == (char)RtfChar.CR)) { outputTxt.Remove(currPos - 3, 3); } return(ErrorRtf.OK); }