1 /*
2  * Copyright (c) 2019, 2020, 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 RESOURCEEDITOR_H
27 #define RESOURCEEDITOR_H
28 
29 #include <windows.h>
30 #include <vector>
31 #include <string>
32 
33 
34 class ResourceEditor {
35 public:
36     class FileLock {
37     public:
38         explicit FileLock(const std::wstring& binaryPath);
39         explicit FileLock(HANDLE h);
40         ~FileLock();
41 
get()42         HANDLE get() const {
43             return h;
44         }
45 
46         void discard(bool v = true) {
47             theDiscard = v;
48         }
49 
ownHandle(bool v)50         FileLock& ownHandle(bool v) {
51             theOwnHandle = v;
52             return *this;
53         }
54 
55     private:
56         FileLock(const FileLock&);
57         FileLock& operator=(const FileLock&);
58     private:
59         HANDLE h;
60         bool theOwnHandle;
61         bool theDiscard;
62     };
63 
64 public:
65     ResourceEditor();
66 
67     /**
68      * Set the language identifier of the resource to be updated.
69      */
language(unsigned v)70     ResourceEditor& language(unsigned v) {
71         lang = v;
72         return *this;
73     }
74 
75     /**
76      * Set the resource type to be updated.
77      */
78     ResourceEditor& type(unsigned v);
79 
80     /**
81      * Set the resource type to be updated.
82      */
83     ResourceEditor& type(LPCWSTR v);
84 
85     /**
86      * Set resource ID.
87      */
88     ResourceEditor& id(unsigned v);
89 
90     /**
91      * Set resource ID.
92      */
93     ResourceEditor& id(LPCWSTR v);
94 
95     /**
96      * Relaces resource configured in the given binary with the given data stream.
97      */
98     ResourceEditor& apply(const FileLock& dstBinary, std::istream& srcStream, std::streamsize size=0);
99 
100     /**
101      * Relaces resource configured in the given binary with contents of
102      * the given binary file.
103      */
104     ResourceEditor& apply(const FileLock& dstBinary, const std::wstring& srcFile);
105 
106 private:
107     unsigned lang;
108     std::wstring theId;
109     LPCWSTR theIdPtr;
110     std::wstring theType;
111     LPCWSTR theTypePtr;
112 };
113 
114 #endif // #ifndef RESOURCEEDITOR_H
115