void PTextBuffer::ResizeGap(int32 gapSize) { if (fGapSize == gapSize) return; char *t = fText; try { if (gapSize > fGapSize) { fText = (char *)realloc(fText, fLogicalSize + gapSize); FailNil(fText); } memmove(fText + fGap + gapSize, fText + fGap + fGapSize, fPhysicalSize - (fGap + fGapSize)); if (gapSize < fGapSize) { fText = (char *)realloc(fText, fLogicalSize + gapSize); FailNil(fText); } fGapSize = gapSize; fPhysicalSize = fLogicalSize + gapSize; } catch (HErr& e) { fText = t; throw; } // PrintToStream(); } /* PTextBuffer::ResizeGap */
CDiff::CDiff(vector<int>& vx, vector<int>& vy) { fVX = fVY = fD = NULL; fN = vx.size(); fM = vy.size(); fVX = (int *)malloc(fN * sizeof(int)); FailNil(fVX); copy(vx.begin(), vx.end(), fVX); fVY = (int *)malloc(fM * sizeof(int)); FailNil(fVY); copy(vy.begin(), vy.end(), fVY); int diags = fN + fM + 3; fD = (int *)malloc(diags * (2 * sizeof(int))); FailNil(fD); fFD = fD + fM + 1; fBD = fD + diags + fM + 1; fCX.insert(fCX.begin(), fN + 1, false); fCY.insert(fCY.begin(), fM + 1, false); Seq(0, 0, fN, fM); } /* CDiff::CDiff */
void CFontStyle::InitTable() { int dc = mstrlen(kDefChars); f_unit_t *w; fEscapementsCount = 2 * dc; fEscapements = (EscapementEntry *)calloc(fEscapementsCount, sizeof(EscapementEntry)); FailNil(fEscapements); w = (f_unit_t *)malloc(dc * sizeof(f_unit_t)); FailNil(w); fFont.GetEscapements(kDefChars, mstrlen(kDefChars), w); const char *s = kDefChars; for (int i = 0; i < dc; i++) { int cl = mcharlen(s); int uc = municode(s); int ix = uc % fEscapementsCount; while (fEscapements[ix].unicode != 0) ix = (ix + 1) % fEscapementsCount; fEscapements[ix].unicode = uc; fEscapements[ix].escapement = w[i] * fFont.Size(); s += cl; } free(w); } /* CFontStyle::InitTable */
void CRunArray2::InsertValues(int inIndex, int inCount, int inValue) { int offset = GetOffset(inIndex); if (fData[offset].raValue != inValue) { if (fData[offset].raIndex == inIndex) { RAElement *t = (RAElement *) REALLOC(fData, sizeof(RAElement) * (fCount + 1)); FailNil(t); fData = t; memmove(fData + offset + 1, fData + offset, (fCount - offset) * sizeof(RAElement)); fCount++; fData[offset].raValue = inValue; } else { RAElement *t = (RAElement *) REALLOC(fData, sizeof(RAElement) * (fCount + 2)); FailNil(t); fData = t; memmove(fData + offset + 2, fData + offset, (fCount - offset) * sizeof(RAElement)); fCount += 2; offset++; fData[offset].raIndex = inIndex; fData[offset].raValue = inValue; fData[offset + 1].raIndex = inIndex; } } for (int i = offset + 1; i < fCount; i++) { fData[i].raIndex += inCount; if (fData[i].raIndex > fMax) { fCount = i; RAElement *t = (RAElement *) REALLOC(fData, sizeof(RAElement) * fCount); FailNil(t); fData = t; break; } } } /* CRunArray2::InsertValues */
void CRunArray2::SetValue(int inStart, int inStop, int inValue) { ASSERT(inStart <= inStop); SetValue(inStart, inValue); if (inStart != inStop) { SetValue(inStop, inValue); int start = GetOffset(inStart); int stop = GetOffset(inStop); if (start != stop) { int n = stop - start; memmove(fData + start + 1, fData + stop + 1, sizeof(RAElement) * (fCount - stop)); fCount -= n; RAElement *t = (RAElement *) REALLOC(fData, fCount * sizeof(RAElement)); FailNil(t); fData = t; } } } /* CRunArray2::SetValue */
URLData::URLData(const char *url) { fServer = fUsername = fPassword = fPath = fFile = NULL; if (strncasecmp(url, "ftp://", 6) == 0) { CAlloca buf(strlen(url) + 1); strcpy(buf, url); char *p = buf + 6, *r = p; do { if (*p == '%') { char s[4], *t; s[0] = *++p; s[1] = *++p; s[2] = 0; int c = strtoul(s, &t, 16); *r++ = c; p++; } else *r++ = *p++; } while (*p); *r = 0; p = strchr(buf + 6, '/'); if (p) { int l = p - buf - 6; fServer = (char *)malloc(l + 1); FailNil(fServer); strncpy(fServer, buf + 6, l); fServer[l] = 0; char *f = strrchr(p + 1, '/'); if (f) { l = f - p - 1; fPath = (char *)malloc(l + 1); strncpy(fPath, p + 1, l); fPath[l] = 0; } else f = p; l = strlen(f); fFile = (char *)malloc(l + 1); strncpy(fFile, f + 1, l); fFile[l] = 0; } } } /* CFTPOpen::ParseFtpUrl */
CDoc::CDoc(const char* mimetype, BLooper *target, const entry_ref *doc) : fDocIO(NULL) , fSavePanel(NULL) , fMimeType(mimetype ? mimetype : "") , fDirty(false) , fReadOnly(false) , fEncoding(B_UNICODE_UTF8) , fLineEndType(kle_LF) { fDocIO = new CLocalDocIO(this, doc, target); FailNil(fDocIO); if (doc) { BEntry e; FailOSErr(e.SetTo(doc, true)); FailOSErr(e.GetParent(&gCWD)); BNode node; FailOSErr(node.SetTo(doc)); struct stat st; FailOSErr(node.GetStat(&st)); fReadOnly = !((gUid == st.st_uid && (S_IWUSR & st.st_mode)) || (gGid == st.st_gid && (S_IWGRP & st.st_mode)) || (S_IWOTH & st.st_mode)); char s[NAME_MAX]; if (BNodeInfo(&node).GetType(s) == B_OK) fMimeType = s; } sfDocList.push_back(this); }
void CPrefsDialog::GetDefPageSetup() { BPrintJob prJob("a page setup job"); if (fPageSetup) { BMessage *s = new BMessage; if (s && s->Unflatten(fPageSetup) == B_NO_ERROR) prJob.SetSettings(s); } int result = prJob.ConfigPage(); if (result == B_NO_ERROR) { BMessage * s(prJob.Settings()); fPageSetupSize = s->FlattenedSize(); if (fPageSetup) FREE(fPageSetup); fPageSetup = (char *)MALLOC(fPageSetupSize); FailNil(fPageSetup); result = s->Flatten(fPageSetup, fPageSetupSize); FailOSErr(result, "error flattening (%d)"); UpdateFields(); } } /* CPrefsDialog::GetDefPageSetup */
CRunArray2::CRunArray2(CRunArray2& inArray) { fCount = inArray.fCount; fMax = inArray.fMax; fData = (RAElement *)MALLOC(fCount * sizeof(RAElement)); FailNil(fData); memcpy(fData, inArray.fData, fCount * sizeof(RAElement)); } /* CRunArray2::CRunArray2 */
CRunArray2::CRunArray2(int inMax, int inValue) { fData = (RAElement *)CALLOC(2, sizeof(RAElement)); FailNil(fData); fData[1].raIndex = 1; fData[1].raValue = inValue; fMax = inMax; fCount = 2; } /* CRunArray2::CRunArray2 */
CRunArray2& CRunArray2::operator= (const CRunArray2& inRunArray) { fCount = inRunArray.fCount; fMax = inRunArray.fMax; fData = (RAElement *)REALLOC(fData, fCount * sizeof(RAElement)); FailNil(fData); memcpy(fData, inRunArray.fData, fCount * sizeof(RAElement)); return *this; } /* CRunArray2::operator= */
PTextBuffer::PTextBuffer() : fText(strdup("")) , fLogicalSize(0) , fPhysicalSize(0) , fGap(0) , fGapSize(0) , fChangeCounter(0) { FailNil(fText); } /* PTextBuffer::PTextBuffer */
void HPreferences::WritePrefFile() { BLOCK; FILE *f; f = fopen(fFile, "w"); if (!f) throw HErr("Could not create settings file"); PrefMap::iterator pi; for (pi = fPrefs.begin(); pi != fPrefs.end(); pi++) { if (!(*pi).second) continue; else if (strchr((*pi).second, '\n') == NULL) fprintf(f, "%s=%s\n", (*pi).first.c_str(), (*pi).second); else { char *buf = (char *)malloc(2 * strlen((*pi).second)); FailNil(buf); char *a, *b; a = buf, b = (*pi).second; while (*b) { if (*b == '\n') { *a++ = 0x1b; *a++ = 'n'; b++; } else if (*b == 0x1b) { *a++ = 0x1b; *a++ = 0x1b; b++; } else *a++ = *b++; } *a = 0; fprintf(f, "%s=%s\n", (*pi).first.c_str(), buf); free(buf); } } fclose(f); } /* HPreferences::WritePrefFile */
void CPathsBox::AddClicked() { if (fPanel) delete fPanel; fPanel = new BFilePanel(B_OPEN_PANEL, new BMessenger(this), NULL, B_DIRECTORY_NODE, false, new BMessage(msg_AddPath)); FailNil(fPanel); fPanel->Window()->SetTitle("Add Directory"); fPanel->Show(); } /* CPathsBox::AddClicked */
HPreferences::HPreferences(const char *preffilename) { BPath settings; FailOSErr(find_directory(B_USER_SETTINGS_DIRECTORY, &settings, true)); char p[PATH_MAX]; strcpy(p, settings.Path()); strcat(p, "/"); strcat(p, preffilename); fFile = strdup(p); FailNil(fFile); } /* HPreferences::HPreferences */
void PProjectWindow::WriteAttr(BFile& file, const BMessage& settingsMsg) { char *fm = NULL; try { ssize_t s = settingsMsg.FlattenedSize(); fm = (char *)malloc(s); FailNil(fm); FailOSErr(settingsMsg.Flatten(fm, s)); FailIOErr(file.WriteAttr("pe-prj-info", 'info', 0, fm, s)); } catch (HErr& e) {} if (fm) free(fm); }
void CRunArray2::Read(int count, void *p) { short i, *sp = (short *)p; RAElement *t = (RAElement *)MALLOC(count * sizeof(RAElement)); FailNil(t); fCount = count; if (fData) FREE(fData); fData = t; for (i = 0; i < count; i++) { fData[i].raIndex = *sp++; fData[i].raValue = *sp++; } } /* CRunArray2::Read */
void CPathItem::SetPath(const char *path) { if (fPath) free(fPath); int len = strlen(path); fPath = (char *)malloc(len + 2); FailNil(fPath); strcpy(fPath, path); if (path[len - 1] != kDirectorySeparator) { fPath[len] = kDirectorySeparator; fPath[len + 1] = 0; } } // CPathItem::SetPath
PTextBuffer& PTextBuffer::operator=(const PTextBuffer& b) { if (fText) free(fText); fText = (char *)malloc(b.fPhysicalSize); FailNil(fText); memcpy(fText, b.fText, b.fPhysicalSize); fLogicalSize = b.fLogicalSize; fPhysicalSize = b.fPhysicalSize; fGap = b.fGap; fGapSize = b.fGapSize; fChangeCounter++; return *this; } /* PTextBuffer::operator= */
void CCalcStack::push(cell inCell, CFormulaIterator& inIterator, cell inDependsOn) { StackElement *newElement; if (++fPreIndx < kPreFlightStackSize) newElement = fPreFlight + fPreIndx; else newElement = new StackElement; FailNil(newElement); newElement->sCell = inCell; inIterator.GetData(newElement->sIter); newElement->sDependsOn = inDependsOn; newElement->sNext = fTop; fTop = newElement; } /* push */
void PProjectWindow::ReadAttr(BFile& file, BMessage& settingsMsg) { char *fm = NULL; try { attr_info ai; if (file.GetAttrInfo("pe-prj-info", &ai) == B_NO_ERROR) { fm = (char *)malloc(ai.size); FailNil(fm); FailIOErr(file.ReadAttr("pe-prj-info", ai.type, 0, fm, ai.size)); FailOSErr(settingsMsg.Unflatten(fm)); } } catch (HErr& e) {} if (fm) free(fm); }
CMessageItem::CMessageItem(const char *msg, int msg_size, EMessageKind kind, entry_ref *file, int line) { if (fInfoIcon == NULL) { fInfoIcon = (uchar *)HResources::GetResource('MICN', 502); fWarningIcon = (uchar *)HResources::GetResource('MICN', 500); fErrorIcon = (uchar *)HResources::GetResource('MICN', 501); } fMsg = (char *)malloc(msg_size + 1); FailNil(fMsg); memcpy(fMsg, msg, msg_size); fMsg[msg_size] = 0; if (file) fFile = new entry_ref(*file); else fFile = NULL; fLine = line; fKind = kind; } // CMessageItem::CMessageItem
void CDoc::SaveAs() { if (!fSavePanel) CreateFilePanel(); BWindow *w = fSavePanel->Window(); FailNil(w); w->Lock(); char s[256]; sprintf(s, "Save %s as:", Name()); w->SetTitle(s); fSavePanel->SetSaveText(Name()); if (EntryRef()) { BEntry e(EntryRef()), p; e.GetParent(&p); fSavePanel->SetPanelDirectory(&p); } else fSavePanel->SetPanelDirectory(&gCWD); fSavePanel->SetMessage(new BMessage(B_SAVE_REQUESTED)); if (fDocIO) fSavePanel->SetTarget(fDocIO->Target()); else fSavePanel->SetTarget(dynamic_cast<BLooper*>(this)); w->Unlock(); if (!fSavePanel->IsShowing()) fSavePanel->Show(); else fSavePanel->Window()->Activate(); }
void CPathsBox::ChangeClicked() { if (fList->CurrentSelection() < 0) return; if (fPanel) delete fPanel; BStringItem *item = static_cast<BStringItem*> (fList->ItemAt(fList->CurrentSelection())); entry_ref ref; get_ref_for_path(item->Text(), &ref); // may fail BMessage *msg = new BMessage(msg_ChangePath); msg->AddPointer("item", item); fPanel = new BFilePanel(B_OPEN_PANEL, new BMessenger(this), &ref, B_DIRECTORY_NODE, false, msg); FailNil(fPanel); fPanel->Window()->SetTitle("Change Directory"); fPanel->Show(); } /* CPathsBox::ChangeClicked */
void CFontStyle::ReHash() { int nc = std::max(1, fEscapementsCount * 2); EscapementEntry *n = (EscapementEntry *)calloc(nc, sizeof(EscapementEntry)); FailNil(n); for (int i = 0; i < fEscapementsCount; i++) { int ni = fEscapements[i].unicode % nc; ASSERT(ni >= 0); ASSERT(ni < nc); while (n[ni].unicode != 0) ni = (ni + 1) % nc; n[ni].unicode = fEscapements[i].unicode; n[ni].escapement = fEscapements[i].escapement; } if (fEscapements) free(fEscapements); fEscapements = n; fEscapementsCount = nc; } /* CFontStyle::ReHash */
CInfoDialog::CInfoDialog(BRect frame, const char *name, window_type type, int flags, BWindow *owner, BPositionIO* data) : HDialog(frame, name, type, flags, owner, data) { fDoc = dynamic_cast<PDoc*>(owner); FailNil(fDoc); SetText("name", owner->Title()); char s[32]; sprintf(s, "%d", fDoc->TextView()->Size()); SetText("docsize", s); sprintf(s, "%d", fDoc->TextView()->LineCount()); SetText("lines", s); BMenuField *mf = dynamic_cast<BMenuField*>(FindView("mime")); FailNil(mf); fTypes = mf->Menu(); const char *p; int i = 0; while ((p = gPrefs->GetIxPrefString(prf_X_Mimetype, i++)) != NULL) fTypes->AddItem(new BMenuItem(p, new BMessage(msg_FieldChanged))); if (i == 1) fTypes->AddItem(new BMenuItem("text/plain", new BMessage(msg_FieldChanged))); const char *mime = fDoc->MimeType(); if (mime && mime[0]) { for (i = 0; i < fTypes->CountItems(); i++) { BMenuItem *item = fTypes->ItemAt(i); if (strcmp(item->Label(), mime) == 0) { item->SetMarked(true); break; } } if (i == fTypes->CountItems()) { fTypes->AddSeparatorItem(); fTypes->AddItem(new BMenuItem(mime, new BMessage(msg_FieldChanged))); fTypes->ItemAt(fTypes->CountItems() - 1)->SetMarked(true); } } else { BMenuItem *item; fTypes->AddSeparatorItem(); fTypes->AddItem(item = new BMenuItem("<undefined>", new BMessage(msg_FieldChanged))); item->SetMarked(true); } if (fDoc->EntryRef()) { BNode node; FailOSErr(node.SetTo(fDoc->EntryRef())); time_t t; node.GetModificationTime(&t); char time[256]; strcpy(time, ctime(&t)); time[strlen(time) - 1] = 0; SetText("time", time); } else { //SetEnabled("mime", false); SetText("time", "Not Saved"); } mf = dynamic_cast<BMenuField*>(FindView("font")); FailNil(mf); fMenu = mf->Menu(); FailNil(fMenu); font_family ff; font_style fs; for (int i = 0; i < count_font_families(); i++) { get_font_family(i, &ff); BMenu *fontItem = new BMenu(ff); FailNil(fontItem); fMenu->AddItem(new BMenuItem(fontItem, new BMessage(msg_FieldChanged))); fontItem->SetFont(be_plain_font); for (int j = 0; j < count_font_styles(ff); j++) { get_font_style(ff, j, &fs); BMessage *msg = new BMessage(msg_FieldChanged); msg->AddString("family", ff); msg->AddString("style", fs); fontItem->AddItem(new BMenuItem(fs, msg)); } } fMenu->SetRadioMode(true); mf = dynamic_cast<BMenuField*>(FindView("encoding")); FailNil(mf); fEncoding = mf->Menu(); FailNil(fEncoding); fEncoding->SetRadioMode(true); mf = dynamic_cast<BMenuField*>(FindView("source encoding")); FailNil(mf); fSourceEncoding = mf->Menu(); FailNil(fSourceEncoding); fSourceEncoding->SetRadioMode(true); mf = dynamic_cast<BMenuField*>(FindView("linebreaks")); FailNil(mf); fLineBreaks = mf->Menu(); FailNil(fLineBreaks); fLineBreaks->SetRadioMode(true); BTextControl *tc = dynamic_cast<BTextControl*>(FindView("tabs")); if (tc) tc->SetDivider(be_plain_font->StringWidth(tc->Label()) + 4); BMenu *lang; mf = dynamic_cast<BMenuField*>(FindView("language")); FailNil(mf); lang = mf->Menu(); CLanguageInterface *intf; int cookie = 0; while ((intf = CLanguageInterface::NextIntf(cookie)) != NULL) lang->AddItem(new BMenuItem(intf->Name(), new BMessage(msg_FieldChanged))); lang->SetRadioMode(true); int curLang = fDoc->TextView()->Language(); SetValue("language", curLang >= 0 ? curLang + 3 : 1); CancelClicked(); } /* CInfoDialog::CInfoDialog */
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); } }
void CCellView::Write(BPositionIO& stream) { // Collect the information needed to write the file, fonts first... int *fontList, usedFonts; fontList = (int *)MALLOC(gFontSizeTable.Count() * sizeof(int)); FailNil(fontList); usedFonts = fContainer->CollectFontList(fontList); if (GetOffsetOf(fontList, fBorderFontID, usedFonts) == -1) fontList[usedFonts++] = fBorderFontID; // ...then the styles int *styleList, usedStyles; styleList = (int *)CALLOC(gStyleTable.Count(), sizeof(int)); FailNil(styleList); usedStyles = fContainer->CollectStyles(styleList); // Write the version number scVersion vers; vers.major = 3; vers.minor = 0; WriteChunk(stream, kscVersion, sizeof(vers), &vers); // Write some header info, global to the document scHeader head; head.defaultFormat = 0; head.flags = htonl(fAutoRecalc ? kscAutoRecalc : 0); head.functionCount = htons(gFuncCount); head.cellCount = htonl(fContainer->GetCellCount()); WriteChunk(stream, kscHeader, sizeof(head), &head); // Write a view scView view; view.windowRect = Window()->Frame(); swap_order(view.windowRect); view.position = fPosition; swap_order(view.position); view.frozen = fFrozen; swap_order(view.frozen); view.curCell = fCurCell; swap_order(view.curCell); view.selection = fSelection; swap_order(view.selection); view.headingFont = htons(GetOffsetOf(fontList, fBorderFontID, usedFonts)); if (fShowGrid) view.flags |= kscShowGrid; if (fShowBorders) view.flags |= kscShowHeadings; if (fDisplayZero) view.flags |= kscDisplayZero; swap_order(view.flags); WriteChunk(stream, kscView, sizeof(view), &view); short size; // Write the widths of the last view size = fCellWidths.Count() * sizeof(short) * 2; void *p = MALLOC(size); FailNil(p); fCellWidths.Write(p); WriteChunk(stream, kscWidths, size, p); FREE(p); // And the heights of course size = fCellHeights.Count() * sizeof(short) * 2; p = MALLOC(size); FailNil(p); fCellHeights.Write(p); WriteChunk(stream, kscHeights, size, p); FREE(p); // Then write the styles for the columns size = fContainer->GetColumnStyles().Count() * sizeof(short) * 2; p = MALLOC(size); FailNil(p); scCSElement *sp = (scCSElement *)p; fContainer->GetColumnStyles().Write(p); for (int i = 0; i < fContainer->GetColumnStyles().Count(); i++) { swap_order(sp[i].index); sp[i].style = htons(GetOffsetOf(styleList, sp[i].style, usedStyles)); } WriteChunk(stream, kscColStyles, size, p); FREE(p); // Continue with the names namemap::iterator ni; for (ni = fNames->begin(); ni != fNames->end(); ni++) { scName name; char c; ushort k = htons(kscName); memset(name.name, 0, 32); CHECKWRITE(stream, &k, 2); strcpy(name.name, (*ni).first); if ((*ni).second.BotRight() == (*ni).second.TopLeft()) { k = htons(32 + 6); CHECKWRITE(stream, &k, 2); CHECKWRITE(stream, name.name, 32); c = valCell; CHECKWRITE(stream, &c, sizeof(c)); cell C = (*ni).second.BotRight(); swap_order(C); CHECKWRITE(stream, &C, sizeof(cell)); } else { k = htons(32 + 10); CHECKWRITE(stream, &k, 2); CHECKWRITE(stream, name.name, 32); c = valRange; CHECKWRITE(stream, &c, sizeof(c)); range r = (*ni).second; swap_order(r); CHECKWRITE(stream, &r, sizeof(range)); } c = opEnd; CHECKWRITE(stream, &c, sizeof(c)); } // Then there are the functions used in this document CSet funcs; fContainer->CollectFunctionNrs(funcs); for (int i = kFunctionCount; i < gFuncCount; i++) if (funcs[i]) { scFunc func; memset(func.name, 0, 10); strcpy(func.name, gFuncArrayByNr[i].funcName); func.argCnt = htons(gFuncArrayByNr[i].argCnt); func.funcNr = htons(gFuncArrayByNr[i].funcNr); WriteChunk(stream, kscFunc, sizeof(func), &func); } // Followed by the formatting information. Fonts first for (int i = 0; i < usedFonts; i++) { CFontMetrics fm = gFontSizeTable[fontList[i]]; scFont *font; font_family fam; font_style sty; fm.Font().GetFamilyAndStyle(&fam, &sty); ulong size = sizeof(scFont) + strlen(fam) + strlen(sty) + 2; font = (scFont *)CALLOC(1, size); FailNil(font); font->size = B_HOST_TO_BENDIAN_FLOAT(fm.Font().Size()); font->color = fm.FontColor(); char *p = (char *)font + sizeof(scFont); strcpy(p, sty); p += strlen(sty) + 1; strcpy(p, fam); WriteChunk(stream, kscFont, size, font); FREE(font); } // Then we get the number formats int *formatList, usedFormats; formatList = (int *)MALLOC(gFormatTable.Count() * sizeof(int)); FailNil(formatList); usedFormats = fContainer->CollectFormats(formatList); for (int i = 0; i < usedFormats; i++) { CFormatter nf; if (formatList[i] < eFirstNewFormat) nf = CFormatter(formatList[i]); else nf = gFormatTable[formatList[i]]; scFormat format; format.nr = htons(nf.FormatID()); format.info[0] = 0; format.info[1] = 0; format.info[2] = 0; format.info[3] = 0; WriteChunk(stream, kscFormat, sizeof(format)-1, &format); } // The style table for (int i = 0; i < usedStyles; i++) { CellStyle cs = gStyleTable[styleList[i]]; scStyle style; memset(&style, 0, sizeof(style)); style.font = htons(GetOffsetOf(fontList, cs.fFont, usedFonts)); style.format = htons(GetOffsetOf(formatList, cs.fFormat, usedFormats)); style.align = cs.fAlignment; style.lowColor = cs.fLowColor; WriteChunk(stream, kscStyle, sizeof(style), &style); } FREE(fontList); FREE(formatList); int *t = (int *)CALLOC(gStyleTable.Count(), sizeof(int)); FailNil(t); for (int i = 0; i < usedStyles; i++) t[styleList[i]] = i; FREE(styleList); styleList = t; // And now its time for some data StProgress progress(this, fContainer->GetCellCount(), pColorYellow, false); CCellIterator iter(fContainer); cell c; while (iter.NextExisting(c)) { scCell cl; cl.loc = c; swap_order(cl.loc); cl.style = htons(styleList[fContainer->GetCellStyleNr(c)]); Value val; fContainer->GetValue(c, val); switch (val.fType) { case eNoData: WriteChunk(stream, kscCellEmpty, kscCellSize, &cl); break; case eNumData: { double d = B_HOST_TO_BENDIAN_DOUBLE(val.fDouble); memcpy(cl.num, &d, sizeof(double)); WriteChunk(stream, kscCellNumber, kscCellSize+sizeof(double), &cl); break; } case eBoolData: memcpy(cl.num, &val.fBool, sizeof(bool)); WriteChunk(stream, kscCellBool, kscCellSize+sizeof(bool), &cl); break; case eTimeData: { time_t t = htonl(val.fTime); memcpy(cl.num, &t, sizeof(time_t)); WriteChunk(stream, kscCellDateTime, kscCellSize+sizeof(time_t), &cl); break; } case eTextData: { WriteChunk(stream, kscCellText, kscCellSize, &cl); const char *t = val; WriteChunk(stream, kscString, strlen(t) + 1, t); break; } default: // there was a warning about not all enum values handled in // switch statement. break; } CFormula form = fContainer->GetCellFormula(c); if (form.IsFormula()) { BMallocIO buf; form.Write(buf); WriteChunk(stream, kscFormula, buf.BufferLength(), buf.Buffer()); } progress.Step(); } WriteCharts(stream); // cleanup the mess WriteChunk(stream, kscEnd, 0, NULL); FREE(styleList); }
void CFtpStream::Automaton(int action) { int ctrl = 0, data = 0, r; char msg[1024]; SOCK *csSock = NULL; try { if (! fURL.IsValid()) THROW(("The data to connect is not complete")); ctrl = socket(AF_INET, SOCK_STREAM, 0); if (ctrl < 0) THROW(("Failed to get socket: %s", strerror(errno))); data = socket(AF_INET, SOCK_STREAM, 0); if (data < 0) THROW(("Failed to get socket: %s", strerror(errno))); csSock = s_open(ctrl, "r+"); struct hostent *host; if ((host = gethostbyname(fURL.Server())) == NULL) THROW(("Failed to get server address: %s", strerror(errno))); struct sockaddr_in sa; sa.sin_family = AF_INET; sa.sin_port = htons(21); sa.sin_addr.s_addr = *(unsigned int *)host->h_addr; FailOSErr(connect(ctrl, (struct sockaddr *)&sa, sizeof(sa))); mail_pop_account pa; string username, password; username = strlen(fURL.Username()) ? fURL.Username() : "anonymous"; if (strlen(fURL.Password())) password = fURL.Password(); else if (get_pop_account(&pa) == B_OK) password = pa.reply_to; else password = "******"; struct sockaddr_in saData; memset(&saData, 0, sizeof(saData)); saData.sin_family = AF_INET; int state = 1; while (state) { GetReply(csSock, r, msg); switch (state) { case 1: if ((r / 100) != 2) THROW(("Connect failed: %s", msg)); s_printf(csSock, "user %s\r\n", username.c_str()); state = 2; break; case 2: if ((r / 100) == 3) { s_printf(csSock, "pass %s\r\n", password.c_str()); state = 3; break; } else if ((r / 100) != 2 && (r / 100) != 5) THROW(("Failed to login: %s", msg)); // fall thru case 3: if ((r / 100) == 5) { bool ok; CLogin *l = DialogCreator<CLogin>::CreateDialog(NULL); FailNil(l); l->Connect(fURL.Server(), username, password, &ok); if (ok) { s_printf(csSock, "noop\r\n"); state = 1; continue; } else THROW((0)); } else if (state == 3 && (r / 100) != 2) THROW(("Failed to login: %s", msg)); else if (strlen(fURL.Path())) { s_printf(csSock, "cwd %s\r\n", fURL.Path()); state = 4; break; } // fall thru case 4: { if (state == 4 && (r / 100) != 2) THROW(("Failed to change directory: %s", msg)); if (fPassive) { // switch to passive mode s_printf(csSock, "pasv\r\n"); } else { FailSockErr(bind(data, (struct sockaddr *)&saData, sizeof(saData))); FailSockErr(listen(data, 5)); // [zooey]: calling getsockname() on a socket that has been bound to // IN_ADDR_ANY (the wildcard-address) will *not* return any IP-address, // as this will only be setup by the system during connect or accept. // [refer to W.R. Stevens - Unix Network Programming, Vol 1, p. 92] // BeOS R5 however, *does* fill in the IP-address at this stage (that's // why this code worked for R5 but didn't work for BONE). // In order to fix this problem, we simply use the IP-address of the // command-socket for the PORT-command: socklen_t size = sizeof(saData); // fetch port from data-socket: FailSockErr(getsockname(data, (struct sockaddr *)&saData, &size)); unsigned char *pap = (unsigned char *)&saData.sin_port; // fetch ip-address from cmd-socket: FailSockErr(getsockname(csSock->sSocket, (struct sockaddr *)&sa, &size)); unsigned char *sap = (unsigned char *)&sa.sin_addr.s_addr; s_printf(csSock, "port %d,%d,%d,%d,%d,%d\r\n", sap[0], sap[1], sap[2], sap[3], pap[0], pap[1]); } state = 5; break; } case 5: if (fPassive) { unsigned int sap[4]; unsigned int pap[2]; if ((r / 100) != 2) THROW(("Pasv command failed: %s", msg)); char* pos = strchr(msg,'('); if (!pos) THROW(("Answer to Pasv has unknown format: %s", msg)); int cnt = sscanf(pos+1, "%u,%u,%u,%u,%u,%u", &sap[0], &sap[1], &sap[2], &sap[3], &pap[0], &pap[1]); if (cnt != 6) THROW(("Could not parse answer to Pasv (%d of 6): %s", cnt, msg)); char ipAddr[20]; sprintf(ipAddr, "%d.%d.%d.%d", sap[0], sap[1], sap[2], sap[3]); saData.sin_port = htons(pap[0]*256+pap[1]); saData.sin_addr.s_addr = inet_addr(ipAddr); FailOSErr(connect(data, (struct sockaddr *)&saData, sizeof(saData))); } else { if ((r / 100) != 2) THROW(("Port command failed: %s", msg)); } if (action == 1) { s_printf(csSock, "retr %s\r\n", fURL.File()); state = 6; } else { s_printf(csSock, "stor %s\r\n", fURL.File()); state = 7; } break; case 6: if ((r / 100) == 1) { int ds; socklen_t size = sizeof(sa); if (fPassive) ds = data; else FailSockErr(ds = accept(data, (struct sockaddr *)&sa, &size)); try { do { FailSockErr(r = recv(ds, msg, 1024, 0)); if (r) fData.Write(msg, r); } while (r); closesocket(ds); } catch (HErr& e) { closesocket(ds); throw; } state = 8; } else THROW(("Failed to retrieve file: %s", msg)); break; case 7: if ((r / 100) == 1) { int ds; socklen_t size = sizeof(sa); if (fPassive) ds = data; else FailSockErr(ds = accept(data, (struct sockaddr *)&sa, &size)); try { FailSockErr(send(ds, fData.Buffer(), fData.BufferLength(), 0)); closesocket(ds); } catch (HErr& e) { closesocket(ds); throw; } state = 8; } else THROW(("Failed to store file: %s", msg)); break; case 8: if ((r / 100) != 2) THROW(("Failed to %s file: %s", action == 1 ? "retrieve" : "store", msg)); s_printf(csSock, "quit\r\n"); state = 0; break; } } s_close(csSock); if (!fPassive) closesocket(data); closesocket(ctrl); } catch (HErr& e) { if (csSock) s_close(csSock); if (ctrl) closesocket(ctrl); if (data) closesocket(data); throw; } } /* CFtpStream::Automaton */
void CFtpDialog::Connect() { try { if (fSocket >= 0) { if (fSocketFD) s_close(fSocketFD); closesocket(fSocket); } fSocket = socket(AF_INET, SOCK_STREAM, 0); if (fSocket < 0) THROW(("Failed to get socket: %s", strerror(errno))); fSocketFD = s_open(fSocket, "r+"); FailNil(fSocketFD); SetDefaultButton(static_cast<BButton*>(FindView("ok "))); struct hostent *host; if ((host = gethostbyname(GetText("srvr"))) == NULL) { fServerName->MarkAsInvalid(true); THROW(("Failed to get server address: %s", strerror(errno))); } struct sockaddr_in sa; sa.sin_family = AF_INET; sa.sin_port = htons(21); sa.sin_addr.s_addr = *(unsigned int *)host->h_addr; if (connect(fSocket, (struct sockaddr *)&sa, sizeof(sa)) < 0) { if (!(errno == ECONNREFUSED || errno == ETIMEDOUT || errno == ENETUNREACH)) fServerName->MarkAsInvalid(true); THROW(("Failed to connect to the host: %s", strerror(errno))); } int state = 1; while (state) { GetReply(); switch (state) { case 1: if (*fReply != '2') THROW(("Connect failed: %s", fReply)); s_printf(fSocketFD, "user %s\r\n", GetText("user")); state = 2; break; case 2: if (*fReply == '3') { s_printf(fSocketFD, "pass %s\r\n", GetText("pass")); state = 3; break; } else if (*fReply != '2' && *fReply != '5') THROW(("Failed to login: %s", fReply)); // fall thru case 3: if (*fReply == '5') THROW(("Incorrect username/password.\n%s", fReply)); if (state == 3 && *fReply != '2') THROW(("Failed to login: %s", fReply)); state = 0; break; } } gPrefs->SetPrefString(prf_S_LastFtpUser, GetText("user")); gPrefs->SetPrefString(prf_S_LastFtpServer, GetText("srvr")); sfPassword = GetText("pass"); GetPWD(); } catch (HErr& e) { e.DoError(); s_close(fSocketFD); fSocketFD = NULL; closesocket(fSocket); fSocket = -1; SetDefaultButton(static_cast<BButton*>(FindView("cnct"))); } if (fSocketFD) ListDirectory(); } // CFtpDialog::Connect