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/string_number_conversions.h"
9#include "base/utf_string_conversions.h"
10
11FileMetadataParser::FileMetadataParser(const FilePath& path)
12    : MetadataParser(path),
13      path_(path) {
14}
15
16FileMetadataParser::~FileMetadataParser() {}
17
18bool FileMetadataParser::Parse() {
19  std::string value;
20  int64 size;
21  if (file_util::GetFileSize(path_, &size)) {
22    properties_[MetadataParser::kPropertyFilesize] = base::Int64ToString(size);
23  }
24#if defined(OS_WIN)
25  value = 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
33bool 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
44MetadataPropertyIterator* FileMetadataParser::GetPropertyIterator() {
45  return new FileMetadataPropertyIterator(properties_);
46}
47
48FileMetadataPropertyIterator::FileMetadataPropertyIterator(
49    PropertyMap& properties) : properties_(properties) {
50  it = properties_.begin();
51}
52
53FileMetadataPropertyIterator::~FileMetadataPropertyIterator() {}
54
55bool 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
66int FileMetadataPropertyIterator::Length() {
67  return properties_.size();
68}
69
70bool FileMetadataPropertyIterator::IsEnd() {
71  if (it == properties_.end()) {
72    return true;
73  } else {
74    return false;
75  }
76}
77