xref: /haiku/src/apps/text_search/FileIterator.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2008, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 1998-2007, Matthijs Hollemans.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 #include "FileIterator.h"
10 
11 #include <string.h>
12 
13 #include <Entry.h>
14 #include <NodeInfo.h>
15 #include <Path.h>
16 
17 
18 FileIterator::FileIterator()
19 {
20 }
21 
22 
23 FileIterator::~FileIterator()
24 {
25 }
26 
27 
28 bool
29 FileIterator::_ExamineFile(BEntry& entry, char* buffer, bool textFilesOnly)
30 {
31 	BPath path;
32 	if (entry.GetPath(&path) != B_OK)
33 		return false;
34 
35 	strcpy(buffer, path.Path());
36 
37 	BNode node(&entry);
38 	if (!node.IsFile())
39 		return false;
40 
41 	if (!textFilesOnly)
42 		return true;
43 
44 	BMimeType mimeType;
45 	BNodeInfo nodeInfo(&node);
46 	char mimeTypeString[B_MIME_TYPE_LENGTH];
47 
48 	if (nodeInfo.GetType(mimeTypeString) != B_OK) {
49 		// try to get a MIME type before failing
50 		if (BMimeType::GuessMimeType(path.Path(), &mimeType) != B_OK)
51 			return false;
52 
53 		nodeInfo.SetType(mimeType.Type());
54 	} else
55 		mimeType.SetTo(mimeTypeString);
56 
57 	BMimeType superType;
58 	if (mimeType.GetSupertype(&superType) == B_OK) {
59 		if (strcmp("text", superType.Type()) == 0
60 			|| strcmp("message", superType.Type()) == 0) {
61 			return true;
62 		}
63 	}
64 	// Make an exception for XHTML files
65 	if (strcmp("application/xhtml+xml", mimeTypeString) == 0)
66 		return true;
67 
68 	return false;
69 }
70 
71