home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Asuka / source / utils.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  6.8 KB  |  268 lines

  1. //    Asuka - VirtualDub Build/Post-Mortem Utility
  2. //    Copyright (C) 2005-2007 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "stdafx.h"
  19. #include <vd2/system/vdtypes.h>
  20. #include <vd2/system/file.h>
  21.  
  22. #include <windows.h>
  23.  
  24. #include <stdio.h>
  25. #include <map>
  26. #include <string>
  27.  
  28. #include "utils.h"
  29. #include "resource.h"
  30.  
  31. using namespace std;
  32.  
  33. typedef map<string, uint32> tVersionMap;
  34. tVersionMap        g_versionMap;
  35. int                g_version;
  36. string        g_machineName;
  37.  
  38.  
  39.  
  40.  
  41. void help() {
  42.     puts("VirtualDub Build/Post-Mortem Utility Version 1.9.3 for "
  43. #if VD_CPU_AMD64
  44.             "AMD64"
  45. #else
  46.             "80x86"
  47. #endif
  48.             );
  49.     puts("Copyright (C) Avery Lee 2005-2007. Licensed under GNU General Public License");
  50.     puts("");
  51.     puts("Usage: Asuka <command> [args...]");
  52.     puts("");
  53.     puts("Asuka fontencode   Extract TrueType font glyph outlines");
  54.     puts("Asuka fxc          Compile shaders for Direct3D");
  55.     puts("Asuka glc          Compile shaders for OpenGL");
  56.     puts("Asuka lookup       Look up address in link map");
  57.     puts("Asuka makearray    Convert binary file to C array");
  58.     puts("Asuka maketable    Regenerate precomputed tables");
  59.     puts("Asuka mapconv      Generate runtime symbol database");
  60.     puts("Asuka snapsetup    Temporarily change windows settings for screencaps");
  61.     puts("Asuka verinc       Increment version file");
  62.     puts("Asuka psa          Assemble Direct3D pixel shader");
  63.     exit(5);
  64. }
  65.  
  66. void fail(const char *format, ...) {
  67.     va_list val;
  68.     va_start(val, format);
  69.     fputs("Asuka: Failed: ", stdout);
  70.     vprintf(format, val);
  71.     fputc('\n', stdout);
  72.     va_end(val);
  73.     exit(10);
  74. }
  75.  
  76.  
  77.  
  78. void canonicalize_name(string& name) {
  79.     string::iterator it(name.begin());
  80.  
  81.     *it = toupper(*it);
  82.     ++it;
  83.     transform(it, name.end(), it, name.find('-') != string::npos ? toupper : tolower);
  84. }
  85.  
  86. void canonicalize_name(VDStringA& name) {
  87.     VDStringA::iterator it(name.begin());
  88.  
  89.     *it = toupper(*it);
  90.     ++it;
  91.     transform(it, name.end(), it, name.find('-') != VDStringA::npos ? toupper : tolower);
  92. }
  93.  
  94. string get_name() {
  95.     char buf[256];
  96.     DWORD siz = sizeof buf;
  97.  
  98.     if (!GetComputerName(buf, &siz))        // hostname would probably work on a Unix platform
  99.         buf[0] = 0;
  100.  
  101.     string name(buf);
  102.  
  103.     if (name.empty())
  104.         name = "Anonymous";
  105.     else
  106.         canonicalize_name(name);
  107.  
  108.     return name;
  109. }
  110.  
  111. int get_version() {
  112.     return g_version;
  113. }
  114.  
  115. bool read_version() {
  116.     g_machineName = get_name();
  117.     g_versionMap.clear();
  118.     g_version = 0;
  119.  
  120.     FILE *f = fopen("version2.bin","r");
  121.  
  122.     if (!f) {
  123.         printf("    warning: can't open version2.bin for read, starting new version series\n");
  124.         return false;
  125.     }
  126.  
  127.     char linebuf[2048];
  128.  
  129.     while(fgets(linebuf, sizeof linebuf, f)) {
  130.         int local_builds, local_name_start, local_name_end;
  131.         if (1==sscanf(linebuf, "host: \"%n%*[^\"]%n\" builds: %d", &local_name_start, &local_name_end, &local_builds)) {
  132.             string name(linebuf+local_name_start, local_name_end - local_name_start);
  133.  
  134.             canonicalize_name(name);
  135.  
  136.             g_versionMap[name] = local_builds;
  137.  
  138.             g_version += local_builds;
  139.         } else if (linebuf[0] != '\n')
  140.             printf("    warning: line ignored: %s", linebuf);
  141.     }
  142.  
  143.     return true;
  144. }
  145.  
  146. void inc_version(const char *tag = NULL) {
  147.     ++g_version;
  148.     ++g_versionMap[tag ? string(tag) : g_machineName];
  149. }
  150.  
  151. INT_PTR CALLBACK VerincErrorDlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) {
  152.     switch(msg) {
  153.     case WM_COMMAND:
  154.         switch(LOWORD(wParam)) {
  155.         case IDCANCEL:
  156.         case IDC_CHECKOUT:
  157.         case IDC_STRIP_READONLY:
  158.             EndDialog(hdlg, LOWORD(wParam));
  159.             return TRUE;
  160.         }
  161.     }
  162.  
  163.     return FALSE;
  164. }
  165.  
  166. bool write_version(const char *tag) {
  167.     if (!tag)
  168.         tag = g_machineName.c_str();
  169.  
  170.     printf("    incrementing to build %d (builds on '%s': %d)\n", g_version, tag, g_versionMap[tag]);
  171.  
  172.     for(;;) {
  173.         if (FILE *f = fopen("version2.bin","w")) {
  174.             tVersionMap::const_iterator it(g_versionMap.begin()), itEnd(g_versionMap.end());
  175.  
  176.             for(; it!=itEnd; ++it) {
  177.                 const tVersionMap::value_type val(*it);
  178.                 int pad = 20-val.first.length();
  179.  
  180.                 if (pad < 1)
  181.                     pad = 1;
  182.  
  183.                 fprintf(f, "host: \"%s\"%*cbuilds: %d\n", val.first.c_str(), pad, ' ', val.second);
  184.             }
  185.  
  186.             fclose(f);
  187.             return true;
  188.         } else {
  189.             DWORD attr = GetFileAttributes("version2.bin");
  190.             if (attr != 0xFFFFFFFF && (attr & FILE_ATTRIBUTE_READONLY)) {
  191.                 LRESULT rv = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_VERINC_ERROR), NULL, VerincErrorDlgProc);
  192.  
  193.                 if (rv == IDC_STRIP_READONLY) {
  194.                     SetFileAttributes("version2.bin", attr & ~FILE_ATTRIBUTE_READONLY);
  195.                     continue;
  196.                 } else if (rv == IDC_CHECKOUT) {
  197.                     system("p4 edit version2.bin");
  198.                     continue;
  199.                 }
  200.             }
  201.  
  202.             fail("Can't open version2.bin for write.");
  203.             return false;
  204.         }
  205.     }
  206. }
  207.  
  208. ProjectSetup::ProjectSetup()
  209.     : mCounterTag(get_name().c_str())
  210. {
  211. }
  212.  
  213. ProjectSetup::~ProjectSetup() {
  214. }
  215.  
  216. INT_PTR CALLBACK ProjectSetupDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  217.     ProjectSetup *ps;
  218.  
  219.     switch(msg) {
  220.         case WM_INITDIALOG:
  221.             SetWindowLongPtr(hwnd, DWLP_USER, lParam);
  222.             ps = (ProjectSetup *)lParam;
  223.             SetDlgItemText(hwnd, IDC_COUNTER_TAG, ps->mCounterTag.c_str());
  224.             return TRUE;
  225.  
  226.         case WM_COMMAND:
  227.             switch(LOWORD(wParam)) {
  228.                 case IDOK:
  229.                 case IDCANCEL:
  230.                     ps = (ProjectSetup *)GetWindowLongPtr(hwnd, DWLP_USER);
  231.                     if (ps) {
  232.                         char buf[256];
  233.                         GetDlgItemText(hwnd, IDC_COUNTER_TAG, buf, 256);
  234.                         ps->mCounterTag = buf;
  235.                     }
  236.                     EndDialog(hwnd, 0);
  237.                     return TRUE;
  238.             }
  239.             break;
  240.     }
  241.  
  242.     return FALSE;
  243. }
  244.  
  245. void ProjectSetup::Query() {
  246.     DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_SETUP), NULL, ProjectSetupDlgProc, (LPARAM)this);
  247. }
  248.  
  249. void ProjectSetup::Read(const wchar_t *filename) {
  250.     VDFileStream fs(filename);
  251.     VDTextStream ts(&fs);
  252.  
  253.     while(const char *s = ts.GetNextLine()) {
  254.         if (!strncmp(s, "counter-tag:", 12)) {
  255.             mCounterTag = VDStringSpanA(s + 12).trim(" \t\r\n");
  256.             canonicalize_name(mCounterTag);
  257.         }
  258.     }
  259. }
  260.  
  261. void ProjectSetup::Write(const wchar_t *filename) {
  262.     VDFileStream fs(filename, nsVDFile::kWrite | nsVDFile::kDenyAll | nsVDFile::kCreateAlways);
  263.     VDStringA s;
  264.  
  265.     s.sprintf("counter-tag: %s\r\n", mCounterTag.c_str());
  266.     fs.write(s.data(), s.size());
  267. }
  268.