Пример #1
0
static void WarnForMissingFunction(const char *name)
{
	char s[256];
	sprintf(s, GetMessage(msgFunctionNotAvailable), name);
	MAlert *a = new MInfoAlert(s);
	a->Go();
} 
Пример #2
0
void CCellView::Read(BPositionIO& stream)
{
	CSwapStream str(stream);

	scChunk chunk;
	long offset;
	int *funcList, *styleList, *fontList, *formatList;
	int funcCount, styleCount, fontCount, formatCount, borderFont = fBorderFontID;
	bool warnForIncorrectFormula = true;
	scCell cl;

	styleCount = 0;
	styleList = (int *)MALLOC(0);
	fontCount = 0;
	fontList = (int *)MALLOC(0);
	formatCount = 0;
	formatList = (int *)MALLOC(0);
	funcList = NULL;
	
	scCSElement *colStyles = NULL;
	int colStyleCount = 0;

	offset = 0;

	StProgress progress(this, 1, pColorYellow, false);
	StWriteLock lock(fContainer);

	try
	{
		stream.Seek(offset, SEEK_SET);
		
		str >> chunk;

		if (chunk.type == kscVersion)
		{
			scVersion vers;
			str >> vers;
			if (vers.major != 3)
				THROW((errTooNewFileFormat));
		}
		else
			THROW((errUnknownFileFormat, ((CCellWindow *)Window())->EntryRef()->name));

		do
		{
			if (stream.Seek(offset, SEEK_SET) < offset)
			{
				MStopAlert("File is too short").Go();
				break;
			}
			
			str >> chunk;
			offset += 4 + chunk.size;
			
			switch (chunk.type)
			{
				case kscVersion:
					break;
				case kscHeader:
				{
					scHeader head;
					str >> head;
					
					funcList = (int *)CALLOC(head.functionCount, sizeof(int));
					FailNil(funcList);
					funcCount = head.functionCount;
					progress.NewMax(head.cellCount);
					break;
				}
				case kscView:
				{
					scView view;
					str >> view;
					
					if (view.windowRect.left > 0 && view.windowRect.top > 0)
					{
						BRect r;
						{
							r = BScreen().Frame();
						}

						r.InsetBy(4, 4);
						r = r & view.windowRect;
						
						if (r.IsValid() && r.Width() >= 300 && r.Height() >= 100)
						{
							Window()->MoveTo(r.left, r.top);
							Window()->ResizeTo(r.Width(), r.Height());
						}
					}
					if (view.position.h > 0 && view.position.v > 0)
						fPosition = view.position;
					fFrozen = view.frozen;
					if (view.selection.IsValid())
						fSelection = view.selection;
					if (fSelection.Contains(view.curCell))
						fCurCell = view.curCell;
					borderFont = view.headingFont;
					fShowGrid = (view.flags & kscShowGrid) != 0;
					fShowBorders = (view.flags & kscShowHeadings) != 0;
					if (!fShowBorders)
					{
						fCellBounds.left -= fBorderWidth;
						fCellBounds.top -= fBorderHeight;
					}

					Window()->Show();
					Window()->UpdateIfNeeded();
					break;
				}
				case kscPrint:
					break;
				case kscWidths:
				case kscHeights:
				{
					int count = chunk.size/(sizeof(short)*2);
					scWidthElement *elems = (scWidthElement *)MALLOC(chunk.size);
					FailNil(elems);
					
					int k = count * sizeof(scWidthElement);
					stream.Read(elems, k);
					
					if (chunk.type == kscWidths)
						fCellWidths.Read(count, elems);
					else
						fCellHeights.Read(count, elems);

					FREE(elems);
					break;
				}
				case kscColStyles:
				{
					colStyleCount = chunk.size/(sizeof(short)*2);
					colStyles = (scCSElement *)MALLOC(chunk.size);
					FailNil(colStyles);
					
					for (int i = 0; i < colStyleCount; i++)
						str >> colStyles[i];
					break;
				}
				case kscName:
				{
					char buf[50];
					scName *name = (scName *)buf;
					int l = std::min((int)chunk.size, 50);
					
					stream.Read(name, l);

					range r;
					
					memcpy(&r, name->reference+1, sizeof(cell));
					if (name->reference[0] == valRange)
						memcpy(&r.bottom, name->reference+5, sizeof(cell));
					else
						r.BotRight() = r.TopLeft();
					
					swap_order(r);
					
					if (fNames->count(name->name) == 0)
						(*fNames)[CName(name->name)] = r;
					break;
				}
				case kscFunc:
				{
					scFunc func;
					str >> func;
					
					if (!funcList) THROW((errCorruptedFile));
					if (func.funcNr >= funcCount)
						THROW((errCorruptedFile));
					
					funcList[func.funcNr] = GetFunctionNr(func.name);
					if (funcList[func.funcNr] == -1)
						WarnForMissingFunction(func.name);
					break;
				}
				case kscFont:
				{
					scFont font;
					str >> font;
					font_family fam;
					font_style sty;
					
					ReadCString(stream, sizeof(font_style), sty);
					ReadCString(stream, sizeof(font_family), fam);
				
					int *t = (int *)REALLOC(fontList, (fontCount+1)*sizeof(int));
					FailNil(t);
					fontList = t;
					fontList[fontCount] = gFontSizeTable.GetFontID(
						fam, sty, font.size, font.color);
					fontCount++;
					break;
				}
				case kscFormat:
				{
					scFormat format;
					str >> format;
					
					int *t = (int *)REALLOC(formatList, (formatCount+1)*sizeof(int));
					FailNil(t);
					formatList = t;
					
					if (format.nr < eFirstNewFormat)
						formatList[formatCount] = format.nr;
					else
					{
						char fs[256];
						ReadCString(stream, 255, fs);
						formatList[formatCount] = gFormatTable.GetFormatID(fs);
					}
						
					formatCount++;
					break;
				}
				case kscStyle:
				{
					scStyle style;
					str >> style;
					
					int *t = (int *)REALLOC(styleList, (styleCount+1)*sizeof(int));
					FailNil(t);
					styleList = t;
					
					CellStyle cs;

					if (style.font >= fontCount)
						THROW((errCorruptedFile));
					cs.fFont = fontList[style.font];

					if (style.format >= formatCount)
						THROW((errCorruptedFile));
					cs.fFormat = formatList[style.format];

					cs.fAlignment = style.align;
					cs.fLowColor = style.lowColor;
					cs.fLocked = (style.flags & kscLocked) != 0;
					cs.fHidden = (style.flags & kscHidden) != 0;
					
					styleList[styleCount] = gStyleTable.GetStyleID(cs);
					
					styleCount++;
					break;
				}
				case kscCellEmpty:
				case kscCellBool:
				case kscCellNumber:
				case kscCellDateTime:
				case kscCellText:
				{
					str >> cl;
					
					Value val;
					
					switch (chunk.type)
					{
						case kscCellBool:
						{
							bool b;
							str >> b;
							val = b;
							break;
						}
						case kscCellNumber:
						{
							double d;
							str >> d;
							val = d;
							break;
						}
						case kscCellDateTime:
						{
							time_t t;
							str >> t;
							val = t;
							break;
						}
						case kscCellText:
						{
							// do nothing yet...
						}
					}
	
					fContainer->NewCell(cl.loc, val, NULL);
					if (cl.style >= styleCount)
						/*THROW((errCorruptedFile))*/;
					else
						fContainer->SetCellStyleNr(cl.loc, styleList[cl.style]);
					
					progress.Step();
					break;
				}
				case kscString:
				{
					char s[256];
					Value val;
					stream.Read(s, std::min((int)chunk.size, 255));
					s[std::min((int)chunk.size, 255)] = 0;
					val = s;
					
					fContainer->SetValue(cl.loc, val);
					break;
				}
				case kscFormula:
				{
					CFormula form;
					try
					{
						if (!funcList) THROW((errCorruptedFile));
						form.Read(stream, funcList);
						Value v;
						form.Calculate(cl.loc, v, fContainer);
						fContainer->SetCellFormula(cl.loc, form.CopyString());
					}
					catch (CErr& e)
					{
						CATCHED;
						char s[32];
						cl.loc.GetName(s);
#if DEBUG
						printf("%s in formula of cell %s\n", (char *)e, s);
#endif
						if (warnForIncorrectFormula)
						{
							char m[256];
							sprintf(m, GetMessage(msgIncorrectFormula), s);
							MAlert *a = new MWarningAlert(m, GetMessage(msgOK),
								GetMessage(msgNoMoreWarnings));
							if (a->Go() == 2)
								warnForIncorrectFormula = false;
						}
					}
					form.Clear();
					break;
				}
				case kscChart:
					ReadChart(stream, chunk.size);
				case kscEnd:
					break;
				default:
					MStopAlert("File contains errors").Go();
					chunk.type = kscEnd;
					break;
			}
		}
		while (chunk.type != kscEnd);

// adjust the fields that couldn't be adjusted before
		if (fontCount && borderFont < fontCount)
			fBorderFontID = fontList[borderFont];
		
		if (colStyles && styleList)
		{
			for (int i = 0; i < colStyleCount; i++)
			{
				if (colStyles[i].style >= 0 && colStyles[i].style < styleCount)	
					colStyles[i].style = styleList[colStyles[i].style];
			}
			fContainer->GetColumnStyles().Read(colStyleCount, colStyles);
		}
	}
Пример #3
0
long CutContaining(MTextAddOn *addon)
{
	//  error checking

	long	selStart;
	long	selEnd;

	if (sSelectionOnly)
	{
		addon->GetSelection(&selStart, &selEnd);
		if (selEnd <= selStart)
			return B_ERROR;
	}
	else
	{
		selStart = 0;
		selEnd = addon->TextLength();
	}

	//  set-up

	const char *txt = addon->Text();
	int size = selEnd - selStart;

	regex_t pb;
	memset(&pb, 0, sizeof(pb));
	int r = regcomp(&pb, sText, REG_EXTENDED);

	if (r == REG_NOERROR)
	{
		char *new_text = new char[size + 1];
		int ns, ni, ts, ti;

		ns = ni = ts = ti = 0;

		while (ti <= size)
		{
			if (ti == size || txt[ti] == '\n')
			{
				int sl = ni - ns + 1;

				new_text[ni] = 0;

				r = regexec(&pb, new_text + ns, 0, NULL, 0);

				if (r == 0)
				{
					addon->Select(ts, ti + 1);
					addon->Delete();
					txt = addon->Text();

					ti -= sl;
					size -= sl;

					new_text[ni] = '\n';
					ns = ni + 1;
				}
				else if (r == REG_NOMATCH)
				{
					ni = ns - 1;
					ts = ti + 1;
				}
				else
					break;
			}
			else
				new_text[ni] = txt[ti];

			ni++;
			ti++;
		}

		if (r < 2)
		{
			if (be_clipboard->Lock())
			{
				be_clipboard->Clear();
				be_clipboard->Data()->AddData("text/plain", B_MIME_DATA, new_text, ns);
				be_clipboard->Commit();
				be_clipboard->Unlock();
			}
		}

		delete[] new_text;
	}

	if (r != REG_NOERROR && r != REG_NOMATCH)
	{
		char err[100];

		regerror(r, &pb, err, 100);

		MAlert *a = new MWarningAlert(err);
		a->Go();
	}

	regfree(&pb);

	return B_NO_ERROR;
} /* CutContaining */