this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

CoreServices: First code for CarbonCore Resources support

+231 -1
+4 -1
src/frameworks/CoreServices/CMakeLists.txt
··· 1 1 project(CoreServices) 2 2 3 - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions") 3 + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 4 5 5 include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src/external/libcxx/include) 6 6 include_directories( ··· 35 35 LSApplicationWorkspace.m 36 36 constants.m 37 37 ComponentManager.cpp 38 + Files.cpp 39 + Resources.cpp 40 + ResourcesImpl.cpp 38 41 ) 39 42 40 43 set(DYLIB_COMPAT_VERSION "1.0.0")
+37
src/frameworks/CoreServices/Files.cpp
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2020 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + #include <CoreServices/Files.h> 20 + #include <CoreServices/MacErrors.h> 21 + 22 + OSErr FSGetDataForkName(HFSUniStr255* dataForkName) 23 + { 24 + dataForkName->length = 0; 25 + return noErr; 26 + } 27 + 28 + OSErr FSGetResourceForkName(HFSUniStr255* rsrcForkName) 29 + { 30 + const char name[] = "RESOURCE_FORK"; 31 + rsrcForkName->length = sizeof(name) - 1; 32 + 33 + for (int i = 0; i < sizeof(name) - 1; i++) 34 + rsrcForkName->unicode[i] = name[i]; 35 + 36 + return noErr; 37 + }
+100
src/frameworks/CoreServices/Resources.cpp
··· 18 18 */ 19 19 20 20 #include <CoreServices/Resources.h> 21 + #include <unordered_map> 22 + #include <mutex> 23 + #include <limits> 24 + #include "ResourcesImpl.h" 25 + 26 + // Documentation: 27 + // http://web.archive.org/web/20040603192835/http://developer.apple.com/documentation/Carbon/Reference/Resource_Manager/index.html 21 28 22 29 // Usage example: 23 30 // https://github.com/nathanday/ndalias/blob/master/Classes/NDResourceFork.m 31 + 32 + static std::unordered_map<ResFileRefNum, Resources*> g_resources; 33 + static std::mutex g_resourcesLock; 34 + static ResFileRefNum g_currentResFile = kResFileNotOpened; 35 + static ResFileRefNum g_nextRefNum = 1; 36 + 37 + ResFileRefNum FSOpenResFile(const FSRef* ref, SInt8 permission) 38 + { 39 + HFSUniStr255 forkName; 40 + FSGetDataForkName(&forkName); 41 + 42 + ResFileRefNum refNum; 43 + OSErr err = FSOpenResourceFile(ref, forkName.length, forkName.unicode, permission, &refNum); 44 + 45 + if (err != noErr) 46 + return kResFileNotOpened; 47 + return refNum; 48 + } 49 + 50 + // Should return eofErr if given fork doesn't exist / is empty 51 + OSErr FSOpenResourceFile(const FSRef* ref, UniCharCount forkNameLength, const UniChar* forkName, SInt8 permissions, ResFileRefNum* refNum) 52 + { 53 + bool resourceFork = false; 54 + const bool readOnly = permissions == fsRdPerm; 55 + 56 + if (forkNameLength != 0) 57 + { 58 + HFSUniStr255 rsrcForkName; 59 + FSGetResourceForkName(&rsrcForkName); 60 + 61 + if (rsrcForkName.length == forkNameLength && memcmp(forkName, rsrcForkName.unicode, 2 * forkNameLength) == 0) 62 + resourceFork = true; 63 + } 64 + 65 + try 66 + { 67 + char path[4096]; 68 + 69 + OSErr rv = FSRefMakePath(ref, (UInt8*) path, sizeof(path) - 1); 70 + if (rv != noErr) 71 + throw rv; 72 + 73 + Resources* r = new Resources(path, readOnly, resourceFork); 74 + 75 + std::unique_lock<std::mutex> l(g_resourcesLock); 76 + while (g_resources.find(g_nextRefNum) != g_resources.end()) 77 + { 78 + if (g_nextRefNum == std::numeric_limits<short>::max()) 79 + g_nextRefNum = 1; 80 + else 81 + g_nextRefNum++; 82 + } 83 + 84 + g_resources.insert(std::make_pair(g_nextRefNum, r)); 85 + *refNum = g_nextRefNum; 86 + g_nextRefNum++; 87 + 88 + return noErr; 89 + } 90 + catch (OSErr e) 91 + { 92 + *refNum = kResFileNotOpened; 93 + return e; 94 + } 95 + } 96 + 97 + ResFileRefNum CurResFile(void) 98 + { 99 + return g_currentResFile; 100 + } 101 + 102 + void UseResFile(ResFileRefNum ref) 103 + { 104 + std::unique_lock<std::mutex> l(g_resourcesLock); 105 + 106 + if (g_resources.find(ref) != g_resources.end()) 107 + g_currentResFile = ref; 108 + } 109 + 110 + void CloseResFile(ResFileRefNum refNum) 111 + { 112 + if (g_currentResFile == refNum) 113 + g_currentResFile = kResFileNotOpened; 114 + 115 + std::unique_lock<std::mutex> l(g_resourcesLock); 116 + auto it = g_resources.find(refNum); 117 + 118 + if (it != g_resources.end()) 119 + { 120 + delete it->second; 121 + g_resources.erase(it); 122 + } 123 + }
+29
src/frameworks/CoreServices/ResourcesImpl.cpp
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2020 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + #include "ResourcesImpl.h" 20 + #include <stdexcept> 21 + 22 + // File format documentation: 23 + // https://developer.apple.com/library/archive/documentation/mac/MoreToolbox/MoreToolbox-99.html 24 + 25 + Resources::Resources(const char* file, bool readOnly, bool resourceFork) 26 + : m_file(file), m_readOnly(readOnly), m_resourceFork(resourceFork) 27 + { 28 + 29 + }
+36
src/frameworks/CoreServices/ResourcesImpl.h
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2020 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #ifndef _CS_RESOURCES_IMPL_H 21 + #define _CS_RESOURCES_IMPL_H 22 + #include <CoreServices/Resources.h> 23 + #include <string> 24 + 25 + class Resources 26 + { 27 + public: 28 + 29 + Resources(const char* file, bool readOnly, bool resourceFork); 30 + private: 31 + std::string m_file; 32 + const bool m_readOnly, m_resourceFork; 33 + }; 34 + 35 + #endif 36 +
+1
src/frameworks/CoreServices/include/CoreServices/FileManager.h
··· 45 45 uint16_t length; 46 46 uint16_t unicode[255]; 47 47 }; 48 + typedef struct HFSUniStr255 HFSUniStr255; 48 49 struct FSSpec; 49 50 50 51 typedef struct FSSpec* FSSpecPtr;
+20
src/frameworks/CoreServices/include/CoreServices/Files.h
··· 21 21 #ifndef _CS_FILES_H 22 22 #define _CS_FILES_H 23 23 #include <CoreServices/MacTypes.h> 24 + #include <CoreServices/FileManager.h> 25 + 26 + #ifdef __cplusplus 27 + extern "C" { 28 + #endif 24 29 25 30 #if __LP64__ 26 31 typedef int FSIORefNum; 27 32 #else 28 33 typedef SInt16 FSIORefNum; 34 + #endif 35 + 36 + enum { 37 + fsCurPerm = 0, 38 + fsRdPerm = 1, 39 + fsWrPerm = 2, 40 + fsRdWrPerm = 3, 41 + dsRdWrShPerm = 4, 42 + }; 43 + 44 + OSErr FSGetDataForkName(HFSUniStr255* dataForkName); 45 + OSErr FSGetResourceForkName(HFSUniStr255* rsrcForkName); 46 + 47 + #ifdef __cplusplus 48 + } 29 49 #endif 30 50 31 51 #endif
+4
src/frameworks/CoreServices/include/CoreServices/Resources.h
··· 46 46 ResFileRefNum HomeResFile(Handle resource); 47 47 void UseResFile(ResFileRefNum ref); 48 48 49 + // NOTE: What's the difference between CountTypes() and Count1Types()? 50 + // CountTypes() applies to *all* open resource files. 51 + // Count1Types() applies to CurResFile() only. 52 + 49 53 ResourceCount CountTypes(void); 50 54 ResourceCount Count1Types(void); 51 55