Skip to content

Commit

Permalink
Convert some helper tools to use FilePaths for file names.
Browse files Browse the repository at this point in the history
Add a "PRFilePath" macro to file_path.h for use in printf'ing
a FilePath.

These are some of the last users of deprecated file_util functions
(e.g. OpenFile(wstring, ...)).

BUG=24672

Review URL: http://codereview.chromium.org/2929002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52391 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
evan@chromium.org committed Jul 14, 2010
1 parent 66791e3 commit 6b1c0e1
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 48 deletions.
5 changes: 4 additions & 1 deletion base/file_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,14 @@ class FilePath {
StringType path_;
};

// Macros for string literal initialization of FilePath::CharType[].
// Macros for string literal initialization of FilePath::CharType[], and for
// using a FilePath::CharType[] in a printf-style format string.
#if defined(OS_POSIX)
#define FILE_PATH_LITERAL(x) x
#define PRFilePath "s"
#elif defined(OS_WIN)
#define FILE_PATH_LITERAL(x) L ## x
#define PRFilePath "ls"
#endif // OS_WIN

// Provide a hash function so that hash_sets and maps can contain FilePath
Expand Down
3 changes: 1 addition & 2 deletions chrome/tools/convert_dict/aff_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ void CollapseDuplicateSpaces(std::string* str) {

} // namespace

AffReader::AffReader(const std::string& filename)
AffReader::AffReader(const FilePath& path)
: has_indexed_affixes_(false) {
FilePath path = FilePath::FromWStringHack(ASCIIToWide(filename));
file_ = file_util::OpenFile(path, "r");

// Default to Latin1 in case the file doesn't specify it.
Expand Down
4 changes: 3 additions & 1 deletion chrome/tools/convert_dict/aff_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include <string>
#include <vector>

class FilePath;

namespace convert_dict {

class AffReader {
public:
explicit AffReader(const std::string& filename);
explicit AffReader(const FilePath& path);
~AffReader();

bool Read();
Expand Down
25 changes: 15 additions & 10 deletions chrome/tools/convert_dict/convert_dict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <stdio.h>

#include "base/at_exit.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/i18n/icu_util.h"
#include "base/logging.h"
Expand Down Expand Up @@ -82,27 +83,31 @@ int PrintHelp() {

} // namespace

#if defined(OS_WIN)
int wmain(int argc, wchar_t* argv[]) {
#else
int main(int argc, char* argv[]) {
#endif
base::EnableTerminationOnHeapCorruption();
if (argc != 2)
return PrintHelp();

base::AtExitManager exit_manager;
icu_util::Initialize();

std::string file_base = argv[1];
FilePath file_base = FilePath(argv[1]);

std::string aff_name = file_base + ".aff";
printf("Reading %s ...\n", aff_name.c_str());
convert_dict::AffReader aff_reader(aff_name.c_str());
FilePath aff_path = file_base.ReplaceExtension(FILE_PATH_LITERAL(".aff"));
printf("Reading %" PRFilePath " ...\n", aff_path.value().c_str());
convert_dict::AffReader aff_reader(aff_path);
if (!aff_reader.Read()) {
printf("Unable to read the aff file.\n");
return 1;
}

std::string dic_name = file_base + ".dic";
printf("Reading %s ...\n", dic_name.c_str());
convert_dict::DicReader dic_reader(dic_name.c_str());
FilePath dic_path = file_base.ReplaceExtension(FILE_PATH_LITERAL(".dic"));
printf("Reading %" PRFilePath " ...\n", dic_path.value().c_str());
convert_dict::DicReader dic_reader(dic_path);
if (!dic_reader.Read(&aff_reader)) {
printf("Unable to read the dic file.\n");
return 1;
Expand All @@ -125,9 +130,9 @@ int main(int argc, char* argv[]) {
return 1;
}

std::string out_name = file_base + ".bdic";
printf("Writing %s ...\n", out_name.c_str());
FILE* out_file = file_util::OpenFile(out_name, "wb");
FilePath out_path = file_base.ReplaceExtension(FILE_PATH_LITERAL(".bdic"));
printf("Writing %" PRFilePath " ...\n", out_path.value().c_str());
FILE* out_file = file_util::OpenFile(out_path, "wb");
if (!out_file) {
printf("ERROR writing file\n");
return 1;
Expand Down
11 changes: 2 additions & 9 deletions chrome/tools/convert_dict/convert_dict_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,10 @@ void RunDictionaryTest(const char* codepage,
{
// Read the above affix file with AffReader and read the dictionary file
// with DicReader, respectively.
#if defined(OS_WIN)
std::string aff_path = WideToUTF8(aff_file.value());
std::string dic_path = WideToUTF8(dic_file.value());
#else
std::string aff_path = aff_file.value();
std::string dic_path = dic_file.value();
#endif
convert_dict::AffReader aff_reader(aff_path);
convert_dict::AffReader aff_reader(aff_file);
EXPECT_TRUE(aff_reader.Read());

convert_dict::DicReader dic_reader(dic_path);
convert_dict::DicReader dic_reader(dic_file);
EXPECT_TRUE(dic_reader.Read(&aff_reader));

// Verify this DicReader includes all the input words.
Expand Down
3 changes: 1 addition & 2 deletions chrome/tools/convert_dict/dic_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ bool PopulateWordSet(WordSet* word_set, FILE* file, AffReader* aff_reader,

} // namespace

DicReader::DicReader(const std::string& filename) {
FilePath path = FilePath::FromWStringHack(ASCIIToWide(filename));
DicReader::DicReader(const FilePath& path) {
file_ = file_util::OpenFile(path, "r");

FilePath additional_path =
Expand Down
4 changes: 3 additions & 1 deletion chrome/tools/convert_dict/dic_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <string>
#include <vector>

class FilePath;

namespace convert_dict {

class AffReader;
Expand All @@ -23,7 +25,7 @@ class DicReader {
typedef std::pair<std::string, std::vector<int> > WordEntry;
typedef std::vector<WordEntry> WordList;

explicit DicReader(const std::string& filename);
explicit DicReader(const FilePath& path);
~DicReader();

// Non-numeric affixes will be added to the given AffReader and converted into
Expand Down
61 changes: 39 additions & 22 deletions tools/imagediff/image_diff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/process_util.h"
Expand Down Expand Up @@ -88,8 +89,7 @@ class Image {

// Creates the image from the given filename on disk, and returns true on
// success.
bool CreateFromFilename(const char* filename) {
FilePath path = FilePath::FromWStringHack(ASCIIToWide(filename));
bool CreateFromFilename(const FilePath& path) {
FILE* f = file_util::OpenFile(path, "rb");
if (!f)
return false;
Expand Down Expand Up @@ -191,16 +191,18 @@ void PrintHelp() {
*/
}

int CompareImages(const char* file1, const char* file2) {
int CompareImages(const FilePath& file1, const FilePath& file2) {
Image actual_image;
Image baseline_image;

if (!actual_image.CreateFromFilename(file1)) {
fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1);
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file1.value().c_str());
return kStatusError;
}
if (!baseline_image.CreateFromFilename(file2)) {
fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2);
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file2.value().c_str());
return kStatusError;
}

Expand Down Expand Up @@ -290,16 +292,19 @@ bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
return same;
}

int DiffImages(const char* file1, const char* file2, const char* out_file) {
int DiffImages(const FilePath& file1, const FilePath& file2,
const FilePath& out_file) {
Image actual_image;
Image baseline_image;

if (!actual_image.CreateFromFilename(file1)) {
fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1);
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file1.value().c_str());
return kStatusError;
}
if (!baseline_image.CreateFromFilename(file2)) {
fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2);
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file2.value().c_str());
return kStatusError;
}

Expand All @@ -312,53 +317,65 @@ int DiffImages(const char* file1, const char* file2, const char* out_file) {
gfx::PNGCodec::Encode(diff_image.data(), gfx::PNGCodec::FORMAT_RGBA,
diff_image.w(), diff_image.h(), diff_image.w() * 4,
false, &png_encoding);
FilePath out_path = FilePath::FromWStringHack(ASCIIToWide(out_file));
if (file_util::WriteFile(out_path,
if (file_util::WriteFile(out_file,
reinterpret_cast<char*>(&png_encoding.front()), png_encoding.size()) < 0)
return kStatusError;

return kStatusDifferent;
}

// It isn't strictly correct to only support ASCII paths, but this
// program reads paths on stdin and the program that spawns it outputs
// paths as non-wide strings anyway.
FilePath FilePathFromASCII(const std::string& str) {
#if defined(OS_WIN)
return FilePath(ASCIIToWide(str));
#else
return FilePath(str);
#endif
}

int main(int argc, const char* argv[]) {
base::EnableTerminationOnHeapCorruption();
CommandLine::Init(argc, argv);
const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
if (parsed_command_line.HasSwitch(kOptionPollStdin)) {
// Watch stdin for filenames.
std::string stdin_buffer;
std::string filename1_buffer;
bool have_filename1 = false;
FilePath filename1;
while (std::getline(std::cin, stdin_buffer)) {
if (stdin_buffer.empty())
continue;

if (have_filename1) {
if (!filename1.empty()) {
// CompareImages writes results to stdout unless an error occurred.
if (CompareImages(filename1_buffer.c_str(), stdin_buffer.c_str()) ==
kStatusError)
FilePath filename2 = FilePathFromASCII(stdin_buffer);
if (CompareImages(filename1, filename2) == kStatusError)
printf("error\n");
fflush(stdout);
have_filename1 = false;
filename1 = FilePath();
} else {
// Save the first filename in another buffer and wait for the second
// filename to arrive via stdin.
filename1_buffer = stdin_buffer;
have_filename1 = true;
filename1 = FilePathFromASCII(stdin_buffer);
}
}
return 0;
}

// TODO: CommandLine::GetLooseValues() should eventually return
// CommandLine::StringType (which is the same as
// FilePath::StringType and can convert to FilePaths directly).
std::vector<std::wstring> values = parsed_command_line.GetLooseValues();
if (parsed_command_line.HasSwitch(kOptionGenerateDiff)) {
if (values.size() == 3) {
return DiffImages(WideToUTF8(values[0]).c_str(),
WideToUTF8(values[1]).c_str(),
WideToUTF8(values[2]).c_str());
return DiffImages(FilePath::FromWStringHack(values[0]),
FilePath::FromWStringHack(values[1]),
FilePath::FromWStringHack(values[2]));
}
} else if (values.size() == 2) {
return CompareImages(argv[1], argv[2]);
return CompareImages(FilePath::FromWStringHack(values[0]),
FilePath::FromWStringHack(values[1]));
}

PrintHelp();
Expand Down

0 comments on commit 6b1c0e1

Please sign in to comment.