1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/parsers/metadata_parser_filebase.h" 6 7 #include "base/file_util.h" 8 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/utf_string_conversions.h" 10 11 FileMetadataParser::FileMetadataParser(const base::FilePath& path) 12 : MetadataParser(path), 13 path_(path) { 14 } 15 16 FileMetadataParser::~FileMetadataParser() {} 17 18 bool FileMetadataParser::Parse() { 19 std::string value; 20 int64 size; 21 if (base::GetFileSize(path_, &size)) { 22 properties_[MetadataParser::kPropertyFilesize] = base::Int64ToString(size); 23 } 24 #if defined(OS_WIN) 25 value = base::WideToUTF8(path_.BaseName().value()); 26 properties_[MetadataParser::kPropertyTitle] = value; 27 #elif defined(OS_POSIX) 28 properties_[MetadataParser::kPropertyTitle] = path_.BaseName().value(); 29 #endif 30 return true; 31 } 32 33 bool FileMetadataParser::GetProperty(const std::string& key, 34 std::string* value) { 35 PropertyMap::iterator it = properties_.find(key.c_str()); 36 if (it == properties_.end()) { 37 return false; 38 } 39 40 *value = properties_[key.c_str()]; 41 return true; 42 } 43 44 MetadataPropertyIterator* FileMetadataParser::GetPropertyIterator() { 45 return new FileMetadataPropertyIterator(properties_); 46 } 47 48 FileMetadataPropertyIterator::FileMetadataPropertyIterator( 49 PropertyMap& properties) : properties_(properties) { 50 it = properties_.begin(); 51 } 52 53 FileMetadataPropertyIterator::~FileMetadataPropertyIterator() {} 54 55 bool FileMetadataPropertyIterator::GetNext(std::string* key, 56 std::string* value) { 57 if (it == properties_.end()) { 58 return false; 59 } 60 *key = it->first; 61 *value = it->second; 62 it++; 63 return true; 64 } 65 66 int FileMetadataPropertyIterator::Length() { 67 return properties_.size(); 68 } 69 70 bool FileMetadataPropertyIterator::IsEnd() { 71 if (it == properties_.end()) { 72 return true; 73 } else { 74 return false; 75 } 76 } 77