|
| 1 | +#include "stdafx.h" |
| 2 | +#include "file_tmpl.h" |
| 3 | + |
| 4 | + |
| 5 | +file_tmpl::file_tmpl(void) |
| 6 | +{ |
| 7 | +} |
| 8 | + |
| 9 | + |
| 10 | +file_tmpl::~file_tmpl(void) |
| 11 | +{ |
| 12 | +} |
| 13 | + |
| 14 | +file_tmpl& file_tmpl::set_project_name(const char* name) |
| 15 | +{ |
| 16 | + project_name_ = name; |
| 17 | + path_to_ = name; |
| 18 | + return *this; |
| 19 | +} |
| 20 | + |
| 21 | +file_tmpl& file_tmpl::set_path_from(const char* path) |
| 22 | +{ |
| 23 | + path_from_ = path; |
| 24 | + return *this; |
| 25 | +} |
| 26 | + |
| 27 | +tpl_t* file_tmpl::open_tpl(const char* filename) |
| 28 | +{ |
| 29 | + tpl_t* tpl = tpl_alloc(); |
| 30 | + string filepath; |
| 31 | + filepath.format("%s/%s", path_from_.c_str(), filename); |
| 32 | + if (tpl_load(tpl, filepath.c_str()) != TPL_OK) |
| 33 | + { |
| 34 | + printf("load file %s error: %s\r\n", |
| 35 | + filepath.c_str(), last_serror()); |
| 36 | + tpl_free(tpl); |
| 37 | + return NULL; |
| 38 | + } |
| 39 | + return tpl; |
| 40 | +} |
| 41 | + |
| 42 | +bool file_tmpl::copy_and_replace(const char* from, |
| 43 | + const char* to, bool exec /* = false */) |
| 44 | +{ |
| 45 | + tpl_t* tpl = open_tpl(from); |
| 46 | + if (tpl == NULL) |
| 47 | + return false; |
| 48 | + |
| 49 | + tpl_set_field_fmt_global(tpl, "PROGRAM", "%s", project_name_.c_str()); |
| 50 | + |
| 51 | + string filepath; |
| 52 | + filepath << path_to_ << '/' << to; |
| 53 | + if (tpl_save_as(tpl, filepath.c_str()) != TPL_OK) |
| 54 | + { |
| 55 | + printf("save to %s error: %s\r\n", filepath.c_str(), |
| 56 | + last_serror()); |
| 57 | + tpl_free(tpl); |
| 58 | + return false; |
| 59 | + } |
| 60 | + printf("create %s ok.\r\n", filepath.c_str()); |
| 61 | + tpl_free(tpl); |
| 62 | + |
| 63 | + if (exec) |
| 64 | + { |
| 65 | +#ifndef WIN32 |
| 66 | + chmod(filepath.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | |
| 67 | + S_IRGRP | S_IWGRP | S_IXGRP); |
| 68 | +#endif |
| 69 | + } |
| 70 | + return true; |
| 71 | +} |
| 72 | + |
| 73 | +void file_tmpl::create_dirs() |
| 74 | +{ |
| 75 | + acl_make_dirs(project_name_.c_str(), 0755); |
| 76 | +} |
| 77 | + |
| 78 | +bool file_tmpl::create_common() |
| 79 | +{ |
| 80 | + if (copy_and_replace("Makefile", "Makefile") == false) |
| 81 | + return false; |
| 82 | + if (copy_and_replace("valgrind.sh", "valgrind.sh", true) == false) |
| 83 | + return false; |
| 84 | + |
| 85 | + string file; |
| 86 | + |
| 87 | + // for vc2003 |
| 88 | + file.format("%s.sln", project_name_.c_str()); |
| 89 | + if (copy_and_replace("master_service.sln", file.c_str()) == false) |
| 90 | + return false; |
| 91 | + file.format("%s.vcproj", project_name_.c_str()); |
| 92 | + if (copy_and_replace("master_service.vcproj", file.c_str()) == false) |
| 93 | + return false; |
| 94 | + |
| 95 | + // for vc2012 |
| 96 | + file.format("%s_vc2012.sln", project_name_.c_str()); |
| 97 | + if (copy_and_replace("master_service_vc2012.sln", file.c_str()) == false) |
| 98 | + return false; |
| 99 | + file.format("%s_vc2012.vcxproj", project_name_.c_str()); |
| 100 | + if (copy_and_replace("master_service_vc2012.vcxproj", file.c_str()) == false) |
| 101 | + return false; |
| 102 | + file.format("%s_vc2012.vcxproj.filters", project_name_.c_str()); |
| 103 | + if (copy_and_replace("master_service_vc2012.vcxproj.filters", file.c_str()) == false) |
| 104 | + return false; |
| 105 | + |
| 106 | + const char* name = "common_files"; |
| 107 | + const FILE_FROM_TO tab[] = { |
| 108 | + { "stdafx.h", "stdafx.h" }, |
| 109 | + { "stdafx.cpp", "stdafx.cpp" }, |
| 110 | + { NULL, NULL } |
| 111 | + }; |
| 112 | + |
| 113 | + return files_copy(name, tab); |
| 114 | +} |
| 115 | + |
| 116 | +bool file_tmpl::file_copy(const char* from, const char* to) |
| 117 | +{ |
| 118 | + if (strcasecmp(from, to) == 0) |
| 119 | + { |
| 120 | + printf("from(%s) == to(%s)\r\n", from, to); |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + ifstream in; |
| 125 | + if (in.open_read(from) == false) |
| 126 | + { |
| 127 | + printf("open %s error: %s\r\n", from, last_serror()); |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + ofstream out; |
| 132 | + if (out.open_write(to) == false) |
| 133 | + { |
| 134 | + printf("open %s error: %s\r\n", from, last_serror()); |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + string buf; |
| 139 | + |
| 140 | + while (!in.eof()) |
| 141 | + { |
| 142 | + if (in.gets(buf, false) == false) |
| 143 | + break; |
| 144 | + if (out.write(buf) < 0) |
| 145 | + { |
| 146 | + printf("write to %s error: %s\r\n", to, last_serror()); |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + printf("create %s ok\r\n", to); |
| 152 | + return true; |
| 153 | +} |
| 154 | + |
| 155 | +bool file_tmpl::files_copy(const char* name, const FILE_FROM_TO* tab) |
| 156 | +{ |
| 157 | + string from, to; |
| 158 | + |
| 159 | + from = "tmpl/Makefile.in"; |
| 160 | + to.format("%s/Makefile.in", path_to_.c_str()); |
| 161 | + if (file_copy(from.c_str(), to.c_str()) == false) |
| 162 | + return false; |
| 163 | + |
| 164 | + for (size_t i = 0; tab[i].from != NULL; i++) |
| 165 | + { |
| 166 | + from.format("%s/%s", path_from_.c_str(), tab[i].from); |
| 167 | + to.format("%s/%s", path_to_.c_str(), tab[i].to); |
| 168 | + if (file_copy(from, to) == false) |
| 169 | + { |
| 170 | + printf("create %s failed!\r\n", name); |
| 171 | + return false; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + printf("create %s ok!\r\n", name); |
| 176 | + return true; |
| 177 | +} |
0 commit comments