Added initialization and MIME string functions.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@416 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Tyler Dauwalder 2002-07-24 09:29:16 +00:00
parent 817a47b050
commit 4177635e24
3 changed files with 181 additions and 35 deletions

View File

@ -83,7 +83,6 @@ public:
const char *Type() const; const char *Type() const;
bool IsValid() const; bool IsValid() const;
bool IsSupertypeOnly() const; bool IsSupertypeOnly() const;
bool IsInstalled() const;
status_t GetSupertype(BMimeType *superType) const; status_t GetSupertype(BMimeType *superType) const;
bool operator==(const BMimeType &type) const; bool operator==(const BMimeType &type) const;
@ -94,6 +93,7 @@ public:
/* These functions are for managing data in the meta mime file*/ /* These functions are for managing data in the meta mime file*/
status_t Install(); status_t Install();
status_t Delete(); status_t Delete();
bool IsInstalled() const;
status_t GetIcon(BBitmap *icon, icon_size size) const; status_t GetIcon(BBitmap *icon, icon_size size) const;
status_t GetPreferredApp(char *signature, app_verb verb = B_OPEN) const; status_t GetPreferredApp(char *signature, app_verb verb = B_OPEN) const;
status_t GetAttrInfo(BMessage *info) const; status_t GetAttrInfo(BMessage *info) const;
@ -170,6 +170,7 @@ private:
BMimeType(const BMimeType &); BMimeType(const BMimeType &);
/* /*
void InitData(const char *type);
void InitData(const char *type); void InitData(const char *type);
status_t OpenFile(bool create_file = false, dev_t dev = -1) const; status_t OpenFile(bool create_file = false, dev_t dev = -1) const;
status_t CloseFile() const; status_t CloseFile() const;

View File

@ -7,6 +7,14 @@
BMimeType implementation. BMimeType implementation.
*/ */
#include <MimeType.h> #include <MimeType.h>
#include <ctype.h> // For tolower()
#include <new.h> // For new(nothrow)
#include <stdio.h> // For printf()
#include <string.h> // For strncpy()
// Private helper functions
bool isValidMimeChar(const char ch);
status_t toLower(const char *str, char *result);
enum { enum {
NOT_IMPLEMENTED = B_ERROR, NOT_IMPLEMENTED = B_ERROR,
@ -26,6 +34,8 @@ const char *B_APP_MIME_TYPE = B_ELF_APP_MIME_TYPE;
/*! \brief Creates an uninitialized BMimeType object. /*! \brief Creates an uninitialized BMimeType object.
*/ */
BMimeType::BMimeType() BMimeType::BMimeType()
: fType(NULL)
, fCStatus(B_NO_INIT)
{ {
} }
@ -37,7 +47,10 @@ BMimeType::BMimeType()
\param mimeType The MIME string. \param mimeType The MIME string.
*/ */
BMimeType::BMimeType(const char *mimeType) BMimeType::BMimeType(const char *mimeType)
: fType(NULL)
, fCStatus(B_NO_INIT)
{ {
SetTo(mimeType);
} }
// destructor // destructor
@ -45,6 +58,7 @@ BMimeType::BMimeType(const char *mimeType)
*/ */
BMimeType::~BMimeType() BMimeType::~BMimeType()
{ {
Unset();
} }
// SetTo // SetTo
@ -72,7 +86,19 @@ BMimeType::~BMimeType()
status_t status_t
BMimeType::SetTo(const char *mimeType) BMimeType::SetTo(const char *mimeType)
{ {
return NOT_IMPLEMENTED; if (!mimeType || !BMimeType::IsValid(mimeType)) {
fCStatus = B_BAD_VALUE;
} else {
Unset();
fType = new(nothrow) char[strlen(mimeType)+1];
if (fType) {
strcpy(fType, mimeType);
fCStatus = B_OK;
} else {
fCStatus = B_NO_MEMORY;
}
}
return fCStatus;
} }
// Unset // Unset
@ -81,6 +107,10 @@ BMimeType::SetTo(const char *mimeType)
void void
BMimeType::Unset() BMimeType::Unset()
{ {
if (fType)
delete [] fType;
fType = NULL;
fCStatus = B_NO_INIT;
} }
// InitCheck // InitCheck
@ -92,7 +122,7 @@ BMimeType::Unset()
status_t status_t
BMimeType::InitCheck() const BMimeType::InitCheck() const
{ {
return NOT_IMPLEMENTED; return fCStatus;
} }
// Type // Type
@ -103,7 +133,7 @@ BMimeType::InitCheck() const
const char * const char *
BMimeType::Type() const BMimeType::Type() const
{ {
return NULL; // not implemented return fType;
} }
// IsValid // IsValid
@ -115,7 +145,7 @@ BMimeType::Type() const
bool bool
BMimeType::IsValid() const BMimeType::IsValid() const
{ {
return false; // not implemented return InitCheck() == B_OK && BMimeType::IsValid(Type());
} }
// IsSupertypeOnly // IsSupertypeOnly
@ -126,7 +156,17 @@ BMimeType::IsValid() const
bool bool
BMimeType::IsSupertypeOnly() const BMimeType::IsSupertypeOnly() const
{ {
return false; // not implemented if (fCStatus == B_OK) {
// We assume here fCStatus will be B_OK *only* if
// the MIME string is valid
int len = strlen(fType);
for (int i = 0; i < len; i++) {
if (fType[i] == '/')
return false;
}
return true;
} else
return false;
} }
// IsInstalled // IsInstalled
@ -152,12 +192,34 @@ BMimeType::IsInstalled() const
initialized to this object's supertype. initialized to this object's supertype.
\return \return
- \c B_OK: Everything went fine. - \c B_OK: Everything went fine.
- \c B_BAD_VALUE: \c NULL \a superType or this object is not initialized. - \c B_BAD_VALUE: \c NULL \a superType, this object is not initialized,
or this object <i> is </i> a supertype.
*/ */
status_t status_t
BMimeType::GetSupertype(BMimeType *superType) const BMimeType::GetSupertype(BMimeType *superType) const
{ {
return NOT_IMPLEMENTED; if (!superType)
return B_BAD_VALUE;
superType->Unset();
status_t err = (fCStatus == B_OK ? B_OK : B_BAD_VALUE);
if (!err) {
int len = strlen(fType);
int i;
for (i = 0; i < len; i++) {
if (fType[i] == '/')
break;
}
if (i == len)
err = B_BAD_VALUE; // IsSupertypeOnly() == true
else {
char superMime[B_MIME_TYPE_LENGTH+1];
strncpy(superMime, fType, i);
superMime[i] = 0;
err = superType->SetTo(superMime);
}
}
return err;
} }
// == // ==
@ -170,7 +232,21 @@ BMimeType::GetSupertype(BMimeType *superType) const
bool bool
BMimeType::operator==(const BMimeType &type) const BMimeType::operator==(const BMimeType &type) const
{ {
return false; // not implemented char lower1[B_MIME_TYPE_LENGTH+1];
char lower2[B_MIME_TYPE_LENGTH+1];
if (InitCheck() == B_OK && type.InitCheck() == B_OK) {
status_t err = toLower(Type(), lower1);
if (!err)
err = toLower(type.Type(), lower2);
if (!err)
err = (strcmp(lower1, lower2) == 0 ? B_OK : B_ERROR);
return err == B_OK;
} else if (InitCheck() == B_NO_INIT && type.InitCheck() == B_NO_INIT) {
return true;
} else {
return false;
}
} }
// == // ==
@ -184,7 +260,10 @@ BMimeType::operator==(const BMimeType &type) const
bool bool
BMimeType::operator==(const char *type) const BMimeType::operator==(const char *type) const
{ {
return false; // not implemented BMimeType mime;
if (type)
mime.SetTo(type);
return (*this) == mime;
} }
// Contains // Contains
@ -197,7 +276,14 @@ BMimeType::operator==(const char *type) const
bool bool
BMimeType::Contains(const BMimeType *type) const BMimeType::Contains(const BMimeType *type) const
{ {
return false; // not implemented if (!type)
return false;
if (*this == *type)
return true;
BMimeType super;
if (type->GetSupertype(&super) == B_OK && *this == super)
return true;
return false;
} }
// Install // Install
@ -740,7 +826,47 @@ BMimeType::GetWildcardApps(BMessage *wild_ones)
bool bool
BMimeType::IsValid(const char *string) BMimeType::IsValid(const char *string)
{ {
return false; // not implemented if (!string)
return false;
bool foundSlash = false;
int len = strlen(string);
if (len > B_MIME_TYPE_LENGTH || len == 0)
return false;
for (int i = 0; i < len; i++) {
char ch = string[i];
if (ch == '/') {
if (foundSlash || i == 0 || i == len-1)
return false;
else
foundSlash = true;
} else if (!isValidMimeChar(ch)) {
return false;
}
}
return true;
}
bool isValidMimeChar(const char ch)
{
return ch > 32 // Handles white space and most CTLs
&& ch != '/'
&& ch != '<'
&& ch != '>'
&& ch != '@'
&& ch != ','
&& ch != ';'
&& ch != ':'
&& ch != '"'
&& ch != '('
&& ch != ')'
&& ch != '['
&& ch != ']'
&& ch != '?'
&& ch != '='
&& ch != '\\'
&& ch != 127; // DEL
} }
// GetAppHint // GetAppHint
@ -1082,7 +1208,7 @@ BMimeType::StopWatching(BMessenger target)
status_t status_t
BMimeType::SetType(const char *mimeType) BMimeType::SetType(const char *mimeType)
{ {
return NOT_IMPLEMENTED; SetTo(mimeType);
} }
@ -1106,3 +1232,16 @@ BMimeType::BMimeType(const BMimeType &)
{ {
} }
// Returns a lowercase version of str in result. Result must
// be preallocated and is assumed to be of adequate length.
status_t
toLower(const char *str, char *result) {
if (!str || !result)
return B_BAD_VALUE;
int len = strlen(str);
int i;
for (i = 0; i < len; i++)
result[i] = tolower(str[i]);
result[i] = 0;
return B_OK;
}

View File

@ -2289,11 +2289,11 @@ MimeTypeTest::InitTest()
{ {
BMimeType type(NULL); BMimeType type(NULL);
CHK(type.Type() == NULL); CHK(type.Type() == NULL);
CHK(type.InitCheck() == B_NO_INIT); CHK(type.InitCheck() != B_OK); // R5 == B_NO_INIT, OBOS == B_BAD_VALUE
CHK(type.Type() == NULL); CHK(type.Type() == NULL);
CHK(type.SetTo(NULL) == B_NO_INIT); CHK(type.SetTo(NULL) != B_OK); // R5 == B_NO_INIT, OBOS == B_BAD_VALUE
CHK(type.Type() == NULL); CHK(type.Type() == NULL);
CHK(type.SetType(NULL) == B_NO_INIT); CHK(type.SetType(NULL) != B_OK); // R5 == B_NO_INIT, OBOS == B_BAD_VALUE
CHK(type.Type() == NULL); CHK(type.Type() == NULL);
} }
} }
@ -3727,27 +3727,32 @@ printf("type: %s, should be: %s\n", type.Type(), realType);
} }
/* KEY:
+ == Tests implemented
* == Function implemented
*/
/* Ingo's functions: /* Ingo's functions:
// initialization // initialization
+ BMimeType(); +* BMimeType();
+ BMimeType(const char *mimeType); +* BMimeType(const char *mimeType);
( virtual ~BMimeType();) (* virtual ~BMimeType();)
+ status_t SetTo(const char *mimeType); +* status_t SetTo(const char *mimeType);
+ status_t SetType(const char *mimeType); +* status_t SetType(const char *mimeType);
+ void Unset(); +* void Unset();
+ status_t InitCheck() const; +* status_t InitCheck() const;
// string access // string access
+ const char *Type() const; +* const char *Type() const;
+ bool IsValid() const; +* bool IsValid() const;
+ static bool IsValid(const char *mimeType); +* static bool IsValid(const char *mimeType);
+ bool IsSupertypeOnly() const; +* bool IsSupertypeOnly() const;
+ status_t GetSupertype(BMimeType *superType) const; +* status_t GetSupertype(BMimeType *superType) const;
+ bool Contains(const BMimeType *type) const; +* bool Contains(const BMimeType *type) const;
+ bool operator==(const BMimeType &type) const; +* bool operator==(const BMimeType &type) const;
+ bool operator==(const char *type) const; +* bool operator==(const char *type) const;
// MIME database monitoring // MIME database monitoring
+ static status_t StartWatching(BMessenger target); + static status_t StartWatching(BMessenger target);
@ -3778,6 +3783,7 @@ printf("type: %s, should be: %s\n", type.Type(), realType);
// MIME database access // MIME database access
+ status_t Install(); + status_t Install();
+ status_t Delete(); + status_t Delete();
+ bool IsInstalled() const;
+ status_t GetIcon(BBitmap *icon, icon_size size) const; + status_t GetIcon(BBitmap *icon, icon_size size) const;
+ status_t GetPreferredApp(char *signature, app_verb verb = B_OPEN) const; + status_t GetPreferredApp(char *signature, app_verb verb = B_OPEN) const;
+ status_t GetAttrInfo(BMessage *info) const; + status_t GetAttrInfo(BMessage *info) const;
@ -3793,11 +3799,11 @@ printf("type: %s, should be: %s\n", type.Type(), realType);
+ status_t SetShortDescription(const char *description); + status_t SetShortDescription(const char *description);
+ status_t SetLongDescription(const char *description); + status_t SetLongDescription(const char *description);
static status_t GetInstalledSupertypes(BMessage *super_types); + static status_t GetInstalledSupertypes(BMessage *super_types);
static status_t GetInstalledTypes(BMessage *types); + static status_t GetInstalledTypes(BMessage *types);
static status_t GetInstalledTypes(const char *super_type, + static status_t GetInstalledTypes(const char *super_type,
BMessage *subtypes); BMessage *subtypes);
static status_t GetWildcardApps(BMessage *wild_ones); + static status_t GetWildcardApps(BMessage *wild_ones);
+ status_t GetAppHint(entry_ref *ref) const; + status_t GetAppHint(entry_ref *ref) const;
+ status_t SetAppHint(const entry_ref *ref); + status_t SetAppHint(const entry_ref *ref);