1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef RESOURCES_H
  27 #define RESOURCES_H
  28 
  29 #include "WinSysInfo.h"
  30 
  31 
  32 /**
  33  * Classes for resource loading.
  34  * Common use cases:
  35  *  - check if resource is available and save it to file:
  36  *      Resource res(_T("MyResource"), _T("CustomResourceType"));
  37  *      if (res.available()) {
  38  *          res.saveToFile(_T("c:\\temp\\my_resource.bin"));
  39  *      }
  40  */
  41 
  42 class Resource {
  43 public:
  44     // name and type can be specified by string id,
  45     // by integer id (RT_* constants or MAKEINTRESOURCE)
  46     Resource(LPCWSTR name, LPCWSTR type,
  47             HINSTANCE module = SysInfo::getCurrentModuleHandle());
  48     Resource(UINT id, LPCWSTR type,
  49             HINSTANCE module = SysInfo::getCurrentModuleHandle());
  50 
  51     bool available() const;
  52 
  53     // all this methods throw exception if the resource is not available
  54     unsigned size() const;
  55     // gets raw pointer to the resource data
  56     LPCVOID rawData() const;
  57 
  58     // save the resource to a file
  59     void saveToFile(const std::wstring &filePath) const;
  60 
  61     typedef std::vector<BYTE> ByteArray;
  62     // returns the resource as byte array
  63     ByteArray binary() const;
  64 
  65 private:
  66     std::wstring nameStr;
  67     LPCWSTR namePtr;    // can be integer value or point to nameStr.c_str()
  68     std::wstring typeStr;
  69     LPCWSTR typePtr;    // can be integer value or point to nameStr.c_str()
  70     HINSTANCE instance;
  71 
  72     void init(LPCWSTR name, LPCWSTR type, HINSTANCE module);
  73 
  74     // generates error message
  75     std::string getErrMsg(const std::string &descr) const;
  76     HRSRC findResource() const;
  77     LPVOID getPtr(DWORD &size) const;
  78 
  79 private:
  80     // disable copying
  81     Resource(const Resource&);
  82     Resource& operator = (const Resource&);
  83 };
  84 
  85 #endif // RESOURCES_H