gecko-dev/cmd/macfe/central/BookmarksFile.cp

109 lines
2.3 KiB
C++
Raw Normal View History

1998-03-28 02:44:41 +00:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "BookmarksFile.h"
#include "resgui.h"
1998-06-23 01:36:59 +00:00
#include "fe_proto.h"
1998-03-28 02:44:41 +00:00
#include <LStream.h>
#include <algorithm>
//
// ReadBookmarksFile
//
// Given a file containing a single URL (probably dropped on the Finder), open it and
1998-06-23 01:36:59 +00:00
// read the URL so we can load it.
1998-03-28 02:44:41 +00:00
//
OSErr
ReadBookmarksFile ( vector<char> & oURL, FSSpec & inSpec )
{
FInfo info;
OSErr err = ::FSpGetFInfo (&inSpec, &info);
if (err != noErr)
return err;
if (info.fdType != emBookmarkFile)
return fnfErr;
try {
LFileStream stream(inSpec);
stream.OpenDataFork(fsRdPerm);
Int32 howMuch;
// Read in the URL, which is in the form URL\rTITLE
1998-09-29 17:16:24 +00:00
howMuch = stream.ReadData(&(*oURL.begin()), oURL.size());
char* where = &(*find(oURL.begin(), oURL.end(), '\r'));
1998-03-28 02:44:41 +00:00
ThrowIfNil_(where);
*where = 0;
}
catch ( Uint32 inErr )
{
return inErr;
}
return noErr;
}
1998-06-23 01:36:59 +00:00
//
// WriteBookmarksFile
//
// Given an URL (probably dropped on the Finder), create a file and store the URL
//
OSErr WriteBookmarksFile(char * url, FSSpec & spec)
{
if (!url)
return noErr;
LFileStream stream(spec);
try
{
stream.CreateNewDataFile(emSignature, emBookmarkFile, 0);
}
catch (OSErr inErr)
{
if (inErr != dupFNErr)
return inErr;
}
catch (...)
{
return memFullErr;
}
try
{
// Set up the file
stream.OpenDataFork(fsRdWrPerm);
stream.SetMarker(0, streamFrom_Start);
char lineEnding = CR;
// Write the URL
stream.WriteData((void*)url, XP_STRLEN(url) );
stream.WriteData(&lineEnding, 1);
}
catch (OSErr err)
{
return err;
}
catch (...)
{
return memFullErr;
}
return noErr;
}