monotone

monotone Mtn Source Tree

Root/localized_file_io.cc

1#include "lua_hooks.hh"
2#include "file_io.hh"
3#include "localized_file_io.hh"
4
5#include "botan/botan.h"
6
7#include "transforms.hh"
8#include "simplestring_xform.hh"
9#include "charset.hh"
10#include "paths.hh"
11#include "platform.hh"
12
13using std::string;
14
15bool
16ident_existing_file(file_path const & p, file_id & ident, lua_hooks & lua)
17{
18 switch (get_path_status(p))
19 {
20 case path::nonexistent:
21 return false;
22 case path::file:
23 break;
24 case path::directory:
25 W(F("expected file '%s', but it is a directory.") % p);
26 return false;
27 }
28
29 hexenc<id> id;
30 calculate_ident(p, id, lua);
31 ident = file_id(id);
32
33 return true;
34}
35
36void
37read_localized_data(file_path const & path,
38 data & dat,
39 lua_hooks & lua)
40{
41 string db_linesep, ext_linesep;
42 string db_charset, ext_charset;
43
44 bool do_lineconv = (lua.hook_get_linesep_conv(path, db_linesep, ext_linesep)
45 && db_linesep != ext_linesep);
46
47 bool do_charconv = (lua.hook_get_charset_conv(path, db_charset, ext_charset)
48 && db_charset != ext_charset);
49
50 data tdat;
51 read_data(path, tdat);
52
53 string tmp1, tmp2;
54 tmp2 = tdat();
55 if (do_charconv) {
56 tmp1 = tmp2;
57 charset_convert(ext_charset, db_charset, tmp1, tmp2);
58 }
59 if (do_lineconv) {
60 tmp1 = tmp2;
61 line_end_convert(db_linesep, tmp1, tmp2);
62 }
63 dat = tmp2;
64}
65
66void
67write_localized_data(file_path const & path,
68 data const & dat,
69 lua_hooks & lua)
70{
71 string db_linesep, ext_linesep;
72 string db_charset, ext_charset;
73
74 bool do_lineconv = (lua.hook_get_linesep_conv(path, db_linesep, ext_linesep)
75 && db_linesep != ext_linesep);
76
77 bool do_charconv = (lua.hook_get_charset_conv(path, db_charset, ext_charset)
78 && db_charset != ext_charset);
79
80 string tmp1, tmp2;
81 tmp2 = dat();
82 if (do_lineconv) {
83 tmp1 = tmp2;
84 line_end_convert(ext_linesep, tmp1, tmp2);
85 }
86 if (do_charconv) {
87 tmp1 = tmp2;
88 charset_convert(db_charset, ext_charset, tmp1, tmp2);
89 }
90
91 write_data(path, data(tmp2));
92}
93
94void
95calculate_ident(file_path const & file,
96 hexenc<id> & ident,
97 lua_hooks & lua)
98{
99 string db_linesep, ext_linesep;
100 string db_charset, ext_charset;
101
102 bool do_lineconv = (lua.hook_get_linesep_conv(file, db_linesep, ext_linesep)
103 && db_linesep != ext_linesep);
104
105 bool do_charconv = (lua.hook_get_charset_conv(file, db_charset, ext_charset)
106 && db_charset != ext_charset);
107
108 if (do_charconv || do_lineconv)
109 {
110 data dat;
111 read_localized_data(file, dat, lua);
112 calculate_ident(dat, ident);
113 }
114 else
115 {
116 // no conversions necessary, use streaming form
117 // Best to be safe and check it isn't a dir.
118 assert_path_is_file(file);
119 Botan::Pipe p(new Botan::Hash_Filter("SHA-160"), new Botan::Hex_Encoder());
120 Botan::DataSource_Stream infile(file.as_external(), true);
121 p.process_msg(infile);
122
123 ident = lowercase(p.read_all_as_string());
124 }
125}
126
127// Local Variables:
128// mode: C++
129// fill-column: 76
130// c-file-style: "gnu"
131// indent-tabs-mode: nil
132// End:
133// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status