Imported Upstream version 0.0.0+git200150803
Balint Reczey
7 years ago
0 | ############################################################################### | |
1 | # Set default behavior to automatically normalize line endings. | |
2 | ############################################################################### | |
3 | * text=auto | |
4 | ||
5 | ############################################################################### | |
6 | # Set default behavior for command prompt diff. | |
7 | # | |
8 | # This is need for earlier builds of msysgit that does not have it on by | |
9 | # default for csharp files. | |
10 | # Note: This is only used by command line | |
11 | ############################################################################### | |
12 | #*.cs diff=csharp | |
13 | ||
14 | ############################################################################### | |
15 | # Set the merge driver for project and solution files | |
16 | # | |
17 | # Merging from the command prompt will add diff markers to the files if there | |
18 | # are conflicts (Merging from VS is not affected by the settings below, in VS | |
19 | # the diff markers are never inserted). Diff markers may cause the following | |
20 | # file extensions to fail to load in VS. An alternative would be to treat | |
21 | # these files as binary and thus will always conflict and require user | |
22 | # intervention with every merge. To do so, just uncomment the entries below | |
23 | ############################################################################### | |
24 | #*.sln merge=binary | |
25 | #*.csproj merge=binary | |
26 | #*.vbproj merge=binary | |
27 | #*.vcxproj merge=binary | |
28 | #*.vcproj merge=binary | |
29 | #*.dbproj merge=binary | |
30 | #*.fsproj merge=binary | |
31 | #*.lsproj merge=binary | |
32 | #*.wixproj merge=binary | |
33 | #*.modelproj merge=binary | |
34 | #*.sqlproj merge=binary | |
35 | #*.wwaproj merge=binary | |
36 | ||
37 | ############################################################################### | |
38 | # behavior for image files | |
39 | # | |
40 | # image files are treated as binary by default. | |
41 | ############################################################################### | |
42 | #*.jpg binary | |
43 | #*.png binary | |
44 | #*.gif binary | |
45 | ||
46 | ############################################################################### | |
47 | # diff behavior for common document formats | |
48 | # | |
49 | # Convert binary document formats to text before diffing them. This feature | |
50 | # is only available from the command line. Turn it on by uncommenting the | |
51 | # entries below. | |
52 | ############################################################################### | |
53 | #*.doc diff=astextplain | |
54 | #*.DOC diff=astextplain | |
55 | #*.docx diff=astextplain | |
56 | #*.DOCX diff=astextplain | |
57 | #*.dot diff=astextplain | |
58 | #*.DOT diff=astextplain | |
59 | #*.pdf diff=astextplain | |
60 | #*.PDF diff=astextplain | |
61 | #*.rtf diff=astextplain | |
62 | #*.RTF diff=astextplain |
0 | *.o | |
1 | *.so | |
2 | *.a | |
3 | *.dll | |
4 | *.DLL | |
5 | test | |
6 | testmain | |
7 | VisualStudio/*.user | |
8 | VisualStudio/Debug/ | |
9 | VisualStudio/Release/ | |
10 | *.opensdf | |
11 | *.sdf | |
12 | *.suo | |
13 | bin/ | |
14 | libs/ | |
15 | gen/ | |
16 | obj/ | |
17 | *.apk | |
18 | *.ap_ | |
19 | *.dex | |
20 | *.class | |
21 | local.properties | |
22 | *.pydevproject | |
23 | .project | |
24 | .metadata | |
25 | bin/** | |
26 | tmp/** | |
27 | tmp/**/* | |
28 | *.tmp | |
29 | *.bak | |
30 | *.swp | |
31 | *~.nib | |
32 | local.properties | |
33 | .classpath | |
34 | .settings/ | |
35 | .loadpath | |
36 | .externalToolBuilders/ | |
37 | *.launch | |
38 | .cproject | |
39 | .buildpath |
0 | The MIT License (MIT) | |
1 | ||
2 | Copyright (c) 2014 Graeme Hill (http://graemehill.ca) | |
3 | ||
4 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | of this software and associated documentation files (the "Software"), to deal | |
6 | in the Software without restriction, including without limitation the rights | |
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | copies of the Software, and to permit persons to whom the Software is | |
9 | furnished to do so, subject to the following conditions: | |
10 | ||
11 | The above copyright notice and this permission notice shall be included in | |
12 | all copies or substantial portions of the Software. | |
13 | ||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 | THE SOFTWARE. |
0 | # CrossGuid | |
1 | ||
2 | CrossGuid is a minimal, cross platform, C++ GUID library. It uses the best | |
3 | native GUID/UUID generator on the given platform and had a generic class for | |
4 | parsing, stringifying, and comparing IDs. The intention is that anyone who | |
5 | uses this code can simply copy `guid.h` and `guid.cpp` into their project and | |
6 | define one of the following preprocessor flags to control the implementation: | |
7 | ||
8 | * `GUID_LIBUUID` - Uses `libuuid` which is normally used on linux but possibly | |
9 | usable elsewhere as well. | |
10 | * `GUID_CFUUID` - Uses `CFCreateUUID` from Apple's `CoreFoundation` framework. | |
11 | This works on both Mac OSX and iOS. | |
12 | * `GUID_WINDOWS` - Uses the built in `CoCreateGuid` function in Windows. | |
13 | * `GUID_ANDROID` - Uses JNI to invoke Java functions from Android SDK. | |
14 | ||
15 | I recommend taking the time to actually look at the `guid.h` and `guid.cpp` so | |
16 | that you can see how simple they are. It should be pretty trivial to modify | |
17 | the code to match your naming conventions or drop it into a namespace so that it | |
18 | fits in nicely with your code base. | |
19 | ||
20 | ## Basic usage | |
21 | ||
22 | ### Tests | |
23 | ||
24 | The tests are a simple way to figure out how the library works. There is a file | |
25 | in the root of the repository called `test.cpp` that runs a simple set of tests | |
26 | and outputs a few guid strings for a sanity check. This file does not have a | |
27 | `main()` function entry point there, it is intended to be called from somewhere | |
28 | else, and it takes a `GuidGenerator` as input. All platforms except for Android | |
29 | use `testmain.cpp` to construct a `GuidGenerator` and run the tests. In Android | |
30 | there is a special file called `android/jni/jnitest.cpp` which invokes the | |
31 | tests. | |
32 | ||
33 | ### Creating a guid generator | |
34 | ||
35 | Creation of a guid generator is not exactly the same in every platform, but | |
36 | this is an intentional feature. In Android the guid generation function has to | |
37 | have access to a `JNIEnv` handle, but that handle is not necessarily the same | |
38 | all the time. Therefore, there is a `GuidGenerator` class whose construction is | |
39 | different depending on the platform, but client code can pass around a | |
40 | `GuidGenerator` object and then use it the same on every platform. On every | |
41 | platform except Android, you can create a guid generator like this: | |
42 | ||
43 | GuidGenerator generator; | |
44 | ||
45 | But on Android you need to pass a `JNIEnv *`: | |
46 | ||
47 | GuidGenerator generator(env); | |
48 | ||
49 | ### Creating guids | |
50 | ||
51 | On every platform guid creation is the same: | |
52 | ||
53 | void doGuidStuff(GuidGenerator generator) | |
54 | { | |
55 | auto myGuid = generator.newGuid(); | |
56 | } | |
57 | ||
58 | ### Converting guid to string | |
59 | ||
60 | First of all, there is normally no reason to convert a guid to a string except | |
61 | for in debugging or when serializing for API calls or whatever. You should | |
62 | definitely avoid storing guids as strings or using strings for any | |
63 | computations. If you do need to convert a guid to a string, then you can | |
64 | utilize strings because the `<<` operator is overloaded. To print a guid to | |
65 | `std::cout`: | |
66 | ||
67 | ||
68 | void doGuidStuff(GuidGenerator generator) | |
69 | { | |
70 | auto myGuid = generator.newGuid(); | |
71 | std::cout << "Here is a guid: " << myGuid << std::endl; | |
72 | } | |
73 | ||
74 | Or to store a guid in a `std::string`: | |
75 | ||
76 | void doGuidStuff(GuidGenerator generator) | |
77 | { | |
78 | auto myGuid = generator.newGuid(); | |
79 | std::stringstring stream; | |
80 | stream << myGuid; | |
81 | auto guidString = stream.str(); | |
82 | } | |
83 | ||
84 | ### Parsing a string into a guid | |
85 | ||
86 | There is a constructor that can be used to create a guid from a string without | |
87 | needing any reference to a `GuidGenerator`: | |
88 | ||
89 | void doGuidStuff() | |
90 | { | |
91 | Guid guid("e63e03a8-f3e5-4e0f-99bb-a3fc402d4fc8"); | |
92 | } | |
93 | ||
94 | ### Creating a guid from raw bytes | |
95 | ||
96 | It's unlikely that you will need this, but this is done within the library | |
97 | internally to construct a `Guid` object from the raw data given by the system's | |
98 | built-in guid generation function. There are two key constructors for this: | |
99 | ||
100 | Guid(const vector<unsigned char> &bytes); | |
101 | ||
102 | and | |
103 | ||
104 | Guid(const unsigned char *bytes); | |
105 | ||
106 | In both cases the constructor expects to receive exactly 16 bytes. | |
107 | ||
108 | ### Comparing guids | |
109 | ||
110 | `==` and `!=` are implemented, so the following works as expected: | |
111 | ||
112 | void doGuidStuff(GuidGenerator generator) | |
113 | { | |
114 | auto guid1 = generator.newGuid(); | |
115 | auto guid2 = generator.newGuid(); | |
116 | ||
117 | auto guidsAreEqual = guid1 == guid2; | |
118 | auto guidsAreNotEqual = guid1 != guid2; | |
119 | } | |
120 | ||
121 | ## Linux | |
122 | ||
123 | **The Linux version uses the proprocessor flag `GUID_LIBUUID`** | |
124 | ||
125 | On linux you can use libuuid which is pretty standard. On distros like Ubuntu | |
126 | it is available by default but to use it you need the header files so you have | |
127 | to do: | |
128 | ||
129 | sudo apt-get install uuid-dev | |
130 | ||
131 | Then you can compile and run tests with: | |
132 | ||
133 | ./linux.sh | |
134 | ||
135 | ## Mac/iOS | |
136 | ||
137 | **The Mac and iOS versions use the preprocessor flag `GUID_CFUUID`** | |
138 | ||
139 | On Mac or iOS you can use `CFUUIDCreate` from `CoreFoundation`. Since it's a | |
140 | plain C function you don't even need to compile as Objective-C++. If you have | |
141 | the command line tools that come with Xcode installed, then you can compile and | |
142 | run the tests like this: | |
143 | ||
144 | ./mac.sh | |
145 | ||
146 | ## Windows | |
147 | ||
148 | **The Windows version uses the preprocessor flag `GUID_WINDOWS`** | |
149 | ||
150 | On Windows we just the the built-in function `CoCreateGuid`. There is a Visual | |
151 | Studio 2013 solution in the `VisualStudio` directory which you can use to | |
152 | compile and run tests. | |
153 | ||
154 | ## Android | |
155 | ||
156 | **The Android version uses the preprocessor flag `GUID_ANDROID`** | |
157 | ||
158 | The Android version uses a handle to a `JNIEnv` object to invoke the | |
159 | `randomUUID()` function on `java.util.UUID` from C++. The Android specific code | |
160 | is all in the `android/` subdirectory. If you have an emulator already running, | |
161 | then you can run the `android.sh` script in the root directory. It has the | |
162 | following requirements: | |
163 | ||
164 | * Android emulator is already running (or you have physical device connected). | |
165 | * You're using bash. | |
166 | * adb is in your path. | |
167 | * ndk-build and other ndk cross-compile tools are in your path. | |
168 | * You have a jdk setup including `JAVA_HOME` environment variable. | |
169 | ||
170 | ## License | |
171 | ||
172 | The MIT License (MIT) | |
173 | ||
174 | Copyright (c) 2014 Graeme Hill (http://graemehill.ca) | |
175 | ||
176 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
177 | of this software and associated documentation files (the "Software"), to deal | |
178 | in the Software without restriction, including without limitation the rights | |
179 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
180 | copies of the Software, and to permit persons to whom the Software is | |
181 | furnished to do so, subject to the following conditions: | |
182 | ||
183 | The above copyright notice and this permission notice shall be included in | |
184 | all copies or substantial portions of the Software. | |
185 | ||
186 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
187 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
188 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
189 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
190 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
191 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
192 | THE SOFTWARE. |
0 | <?xml version="1.0" encoding="utf-8"?> | |
1 | <Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
2 | <ItemGroup Label="ProjectConfigurations"> | |
3 | <ProjectConfiguration Include="Debug|Win32"> | |
4 | <Configuration>Debug</Configuration> | |
5 | <Platform>Win32</Platform> | |
6 | </ProjectConfiguration> | |
7 | <ProjectConfiguration Include="Release|Win32"> | |
8 | <Configuration>Release</Configuration> | |
9 | <Platform>Win32</Platform> | |
10 | </ProjectConfiguration> | |
11 | </ItemGroup> | |
12 | <PropertyGroup Label="Globals"> | |
13 | <ProjectGuid>{C8233271-F319-4531-AEF6-3BD6420B55CD}</ProjectGuid> | |
14 | <Keyword>Win32Proj</Keyword> | |
15 | <RootNamespace>CrossGuid</RootNamespace> | |
16 | <ProjectName>CrossGuid-Test</ProjectName> | |
17 | </PropertyGroup> | |
18 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | |
19 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | |
20 | <ConfigurationType>Application</ConfigurationType> | |
21 | <UseDebugLibraries>true</UseDebugLibraries> | |
22 | <PlatformToolset>v120</PlatformToolset> | |
23 | <CharacterSet>Unicode</CharacterSet> | |
24 | </PropertyGroup> | |
25 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |
26 | <ConfigurationType>Application</ConfigurationType> | |
27 | <UseDebugLibraries>false</UseDebugLibraries> | |
28 | <PlatformToolset>v120</PlatformToolset> | |
29 | <WholeProgramOptimization>true</WholeProgramOptimization> | |
30 | <CharacterSet>Unicode</CharacterSet> | |
31 | </PropertyGroup> | |
32 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | |
33 | <ImportGroup Label="ExtensionSettings"> | |
34 | </ImportGroup> | |
35 | <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
36 | <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
37 | </ImportGroup> | |
38 | <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
39 | <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
40 | </ImportGroup> | |
41 | <PropertyGroup Label="UserMacros" /> | |
42 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
43 | <LinkIncremental>true</LinkIncremental> | |
44 | </PropertyGroup> | |
45 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
46 | <LinkIncremental>false</LinkIncremental> | |
47 | </PropertyGroup> | |
48 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
49 | <ClCompile> | |
50 | <PrecompiledHeader> | |
51 | </PrecompiledHeader> | |
52 | <WarningLevel>Level3</WarningLevel> | |
53 | <Optimization>Disabled</Optimization> | |
54 | <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;GUID_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
55 | </ClCompile> | |
56 | <Link> | |
57 | <SubSystem>Console</SubSystem> | |
58 | <GenerateDebugInformation>true</GenerateDebugInformation> | |
59 | <AdditionalDependencies>CrossGuidd.lib;%(AdditionalDependencies)</AdditionalDependencies> | |
60 | <AdditionalLibraryDirectories>$(SolutionDir)$(Configuration)</AdditionalLibraryDirectories> | |
61 | </Link> | |
62 | </ItemDefinitionGroup> | |
63 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
64 | <ClCompile> | |
65 | <WarningLevel>Level3</WarningLevel> | |
66 | <PrecompiledHeader> | |
67 | </PrecompiledHeader> | |
68 | <Optimization>MaxSpeed</Optimization> | |
69 | <FunctionLevelLinking>true</FunctionLevelLinking> | |
70 | <IntrinsicFunctions>true</IntrinsicFunctions> | |
71 | <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;GUID_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
72 | </ClCompile> | |
73 | <Link> | |
74 | <SubSystem>Console</SubSystem> | |
75 | <GenerateDebugInformation>true</GenerateDebugInformation> | |
76 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | |
77 | <OptimizeReferences>true</OptimizeReferences> | |
78 | <AdditionalDependencies>CrossGuid.lib;%(AdditionalDependencies)</AdditionalDependencies> | |
79 | <AdditionalLibraryDirectories>$(SolutionDir)$(Configuration)</AdditionalLibraryDirectories> | |
80 | </Link> | |
81 | </ItemDefinitionGroup> | |
82 | <ItemGroup> | |
83 | <ClInclude Include="..\test.h" /> | |
84 | </ItemGroup> | |
85 | <ItemGroup> | |
86 | <ClCompile Include="..\test.cpp" /> | |
87 | <ClCompile Include="..\testmain.cpp" /> | |
88 | </ItemGroup> | |
89 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |
90 | <ImportGroup Label="ExtensionTargets"> | |
91 | </ImportGroup> | |
92 | </Project>⏎ |
0 | <?xml version="1.0" encoding="utf-8"?> | |
1 | <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
2 | <ItemGroup> | |
3 | <Filter Include="Source Files"> | |
4 | <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | |
5 | <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | |
6 | </Filter> | |
7 | <Filter Include="Header Files"> | |
8 | <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | |
9 | <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | |
10 | </Filter> | |
11 | <Filter Include="Resource Files"> | |
12 | <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | |
13 | <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |
14 | </Filter> | |
15 | </ItemGroup> | |
16 | <ItemGroup> | |
17 | <ClCompile Include="..\test.cpp"> | |
18 | <Filter>Source Files</Filter> | |
19 | </ClCompile> | |
20 | <ClCompile Include="..\testmain.cpp"> | |
21 | <Filter>Source Files</Filter> | |
22 | </ClCompile> | |
23 | </ItemGroup> | |
24 | <ItemGroup> | |
25 | <ClInclude Include="..\test.h"> | |
26 | <Filter>Header Files</Filter> | |
27 | </ClInclude> | |
28 | </ItemGroup> | |
29 | </Project>⏎ |
0 | | |
1 | Microsoft Visual Studio Solution File, Format Version 12.00 | |
2 | # Visual Studio Express 2013 for Windows Desktop | |
3 | VisualStudioVersion = 12.0.40629.0 | |
4 | MinimumVisualStudioVersion = 10.0.40219.1 | |
5 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrossGuid-Test", "CrossGuid-Test.vcxproj", "{C8233271-F319-4531-AEF6-3BD6420B55CD}" | |
6 | ProjectSection(ProjectDependencies) = postProject | |
7 | {AF365E5F-E35D-40A6-958A-C7B6988C994D} = {AF365E5F-E35D-40A6-958A-C7B6988C994D} | |
8 | EndProjectSection | |
9 | EndProject | |
10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrossGuid", "CrossGuid.vcxproj", "{AF365E5F-E35D-40A6-958A-C7B6988C994D}" | |
11 | EndProject | |
12 | Global | |
13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution | |
14 | Debug|Win32 = Debug|Win32 | |
15 | Release|Win32 = Release|Win32 | |
16 | EndGlobalSection | |
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution | |
18 | {C8233271-F319-4531-AEF6-3BD6420B55CD}.Debug|Win32.ActiveCfg = Debug|Win32 | |
19 | {C8233271-F319-4531-AEF6-3BD6420B55CD}.Debug|Win32.Build.0 = Debug|Win32 | |
20 | {C8233271-F319-4531-AEF6-3BD6420B55CD}.Release|Win32.ActiveCfg = Release|Win32 | |
21 | {C8233271-F319-4531-AEF6-3BD6420B55CD}.Release|Win32.Build.0 = Release|Win32 | |
22 | {AF365E5F-E35D-40A6-958A-C7B6988C994D}.Debug|Win32.ActiveCfg = Debug|Win32 | |
23 | {AF365E5F-E35D-40A6-958A-C7B6988C994D}.Debug|Win32.Build.0 = Debug|Win32 | |
24 | {AF365E5F-E35D-40A6-958A-C7B6988C994D}.Release|Win32.ActiveCfg = Release|Win32 | |
25 | {AF365E5F-E35D-40A6-958A-C7B6988C994D}.Release|Win32.Build.0 = Release|Win32 | |
26 | EndGlobalSection | |
27 | GlobalSection(SolutionProperties) = preSolution | |
28 | HideSolutionNode = FALSE | |
29 | EndGlobalSection | |
30 | EndGlobal |
0 | <?xml version="1.0" encoding="utf-8"?> | |
1 | <Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
2 | <ItemGroup Label="ProjectConfigurations"> | |
3 | <ProjectConfiguration Include="Debug|Win32"> | |
4 | <Configuration>Debug</Configuration> | |
5 | <Platform>Win32</Platform> | |
6 | </ProjectConfiguration> | |
7 | <ProjectConfiguration Include="Release|Win32"> | |
8 | <Configuration>Release</Configuration> | |
9 | <Platform>Win32</Platform> | |
10 | </ProjectConfiguration> | |
11 | </ItemGroup> | |
12 | <ItemGroup> | |
13 | <ClCompile Include="..\guid.cpp" /> | |
14 | </ItemGroup> | |
15 | <ItemGroup> | |
16 | <ClInclude Include="..\guid.h" /> | |
17 | </ItemGroup> | |
18 | <PropertyGroup Label="Globals"> | |
19 | <ProjectGuid>{AF365E5F-E35D-40A6-958A-C7B6988C994D}</ProjectGuid> | |
20 | <RootNamespace>CrossGuid</RootNamespace> | |
21 | </PropertyGroup> | |
22 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | |
23 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | |
24 | <ConfigurationType>StaticLibrary</ConfigurationType> | |
25 | <UseDebugLibraries>true</UseDebugLibraries> | |
26 | <PlatformToolset>v120</PlatformToolset> | |
27 | <CharacterSet>Unicode</CharacterSet> | |
28 | </PropertyGroup> | |
29 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |
30 | <ConfigurationType>StaticLibrary</ConfigurationType> | |
31 | <UseDebugLibraries>false</UseDebugLibraries> | |
32 | <PlatformToolset>v120</PlatformToolset> | |
33 | <WholeProgramOptimization>true</WholeProgramOptimization> | |
34 | <CharacterSet>Unicode</CharacterSet> | |
35 | </PropertyGroup> | |
36 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | |
37 | <ImportGroup Label="ExtensionSettings"> | |
38 | </ImportGroup> | |
39 | <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
40 | <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
41 | </ImportGroup> | |
42 | <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
43 | <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
44 | </ImportGroup> | |
45 | <PropertyGroup Label="UserMacros" /> | |
46 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
47 | <TargetName>$(ProjectName)d</TargetName> | |
48 | </PropertyGroup> | |
49 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
50 | <ClCompile> | |
51 | <WarningLevel>Level3</WarningLevel> | |
52 | <Optimization>Disabled</Optimization> | |
53 | <SDLCheck>false</SDLCheck> | |
54 | <PreprocessorDefinitions>_MBCS;GUID_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
55 | </ClCompile> | |
56 | <Link> | |
57 | <GenerateDebugInformation>true</GenerateDebugInformation> | |
58 | </Link> | |
59 | </ItemDefinitionGroup> | |
60 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
61 | <ClCompile> | |
62 | <WarningLevel>Level3</WarningLevel> | |
63 | <Optimization>MaxSpeed</Optimization> | |
64 | <FunctionLevelLinking>true</FunctionLevelLinking> | |
65 | <IntrinsicFunctions>true</IntrinsicFunctions> | |
66 | <SDLCheck>true</SDLCheck> | |
67 | <PreprocessorDefinitions>_MBCS;GUID_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
68 | </ClCompile> | |
69 | <Link> | |
70 | <GenerateDebugInformation>true</GenerateDebugInformation> | |
71 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | |
72 | <OptimizeReferences>true</OptimizeReferences> | |
73 | </Link> | |
74 | </ItemDefinitionGroup> | |
75 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |
76 | <ImportGroup Label="ExtensionTargets"> | |
77 | </ImportGroup> | |
78 | </Project>⏎ |
0 | <?xml version="1.0" encoding="utf-8"?> | |
1 | <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
2 | <ItemGroup> | |
3 | <Filter Include="Source Files"> | |
4 | <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | |
5 | <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | |
6 | </Filter> | |
7 | <Filter Include="Header Files"> | |
8 | <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | |
9 | <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | |
10 | </Filter> | |
11 | <Filter Include="Resource Files"> | |
12 | <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | |
13 | <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |
14 | </Filter> | |
15 | </ItemGroup> | |
16 | <ItemGroup> | |
17 | <ClCompile Include="..\guid.cpp"> | |
18 | <Filter>Source Files</Filter> | |
19 | </ClCompile> | |
20 | </ItemGroup> | |
21 | <ItemGroup> | |
22 | <ClInclude Include="..\guid.h"> | |
23 | <Filter>Header Files</Filter> | |
24 | </ClInclude> | |
25 | </ItemGroup> | |
26 | </Project>⏎ |
0 | <?xml version="1.0" encoding="utf-8"?> | |
1 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
2 | package="ca.graemehill.crossguid.testapp" | |
3 | android:versionCode="1" | |
4 | android:versionName="1.0"> | |
5 | <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> | |
6 | <activity android:name="MainActivity" | |
7 | android:label="@string/app_name"> | |
8 | <intent-filter> | |
9 | <action android:name="android.intent.action.MAIN" /> | |
10 | <category android:name="android.intent.category.LAUNCHER" /> | |
11 | </intent-filter> | |
12 | </activity> | |
13 | </application> | |
14 | </manifest> |
0 | # This file is used to override default values used by the Ant build system. | |
1 | # | |
2 | # This file must be checked into Version Control Systems, as it is | |
3 | # integral to the build system of your project. | |
4 | ||
5 | # This file is only used by the Ant script. | |
6 | ||
7 | # You can use this to override default values such as | |
8 | # 'source.dir' for the location of your java source folder and | |
9 | # 'out.dir' for the location of your output folder. | |
10 | ||
11 | # You can also use it define how the release builds are signed by declaring | |
12 | # the following properties: | |
13 | # 'key.store' for the location of your keystore and | |
14 | # 'key.alias' for the name of the key to use. | |
15 | # The password will be asked during the build when you use the 'release' target. | |
16 |
0 | <?xml version="1.0" encoding="UTF-8"?> | |
1 | <project name="TestApp" default="help"> | |
2 | ||
3 | <!-- The local.properties file is created and updated by the 'android' tool. | |
4 | It contains the path to the SDK. It should *NOT* be checked into | |
5 | Version Control Systems. --> | |
6 | <property file="local.properties" /> | |
7 | ||
8 | <!-- The ant.properties file can be created by you. It is only edited by the | |
9 | 'android' tool to add properties to it. | |
10 | This is the place to change some Ant specific build properties. | |
11 | Here are some properties you may want to change/update: | |
12 | ||
13 | source.dir | |
14 | The name of the source directory. Default is 'src'. | |
15 | out.dir | |
16 | The name of the output directory. Default is 'bin'. | |
17 | ||
18 | For other overridable properties, look at the beginning of the rules | |
19 | files in the SDK, at tools/ant/build.xml | |
20 | ||
21 | Properties related to the SDK location or the project target should | |
22 | be updated using the 'android' tool with the 'update' action. | |
23 | ||
24 | This file is an integral part of the build system for your | |
25 | application and should be checked into Version Control Systems. | |
26 | ||
27 | --> | |
28 | <property file="ant.properties" /> | |
29 | ||
30 | <!-- if sdk.dir was not set from one of the property file, then | |
31 | get it from the ANDROID_HOME env var. | |
32 | This must be done before we load project.properties since | |
33 | the proguard config can use sdk.dir --> | |
34 | <property environment="env" /> | |
35 | <condition property="sdk.dir" value="${env.ANDROID_HOME}"> | |
36 | <isset property="env.ANDROID_HOME" /> | |
37 | </condition> | |
38 | ||
39 | <!-- The project.properties file is created and updated by the 'android' | |
40 | tool, as well as ADT. | |
41 | ||
42 | This contains project specific properties such as project target, and library | |
43 | dependencies. Lower level build properties are stored in ant.properties | |
44 | (or in .classpath for Eclipse projects). | |
45 | ||
46 | This file is an integral part of the build system for your | |
47 | application and should be checked into Version Control Systems. --> | |
48 | <loadproperties srcFile="project.properties" /> | |
49 | ||
50 | <!-- quick check on sdk.dir --> | |
51 | <fail | |
52 | message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable." | |
53 | unless="sdk.dir" | |
54 | /> | |
55 | ||
56 | <!-- | |
57 | Import per project custom build rules if present at the root of the project. | |
58 | This is the place to put custom intermediary targets such as: | |
59 | -pre-build | |
60 | -pre-compile | |
61 | -post-compile (This is typically used for code obfuscation. | |
62 | Compiled code location: ${out.classes.absolute.dir} | |
63 | If this is not done in place, override ${out.dex.input.absolute.dir}) | |
64 | -post-package | |
65 | -post-build | |
66 | -pre-clean | |
67 | --> | |
68 | <import file="custom_rules.xml" optional="true" /> | |
69 | ||
70 | <!-- Import the actual build file. | |
71 | ||
72 | To customize existing targets, there are two options: | |
73 | - Customize only one target: | |
74 | - copy/paste the target into this file, *before* the | |
75 | <import> task. | |
76 | - customize it to your needs. | |
77 | - Customize the whole content of build.xml | |
78 | - copy/paste the content of the rules files (minus the top node) | |
79 | into this file, replacing the <import> task. | |
80 | - customize to your needs. | |
81 | ||
82 | *********************** | |
83 | ****** IMPORTANT ****** | |
84 | *********************** | |
85 | In all cases you must update the value of version-tag below to read 'custom' instead of an integer, | |
86 | in order to avoid having your file be overridden by tools such as "android update project" | |
87 | --> | |
88 | <!-- version-tag: 1 --> | |
89 | <import file="${sdk.dir}/tools/ant/build.xml" /> | |
90 | ||
91 | </project> |
0 | LOCAL_PATH := $(call my-dir) | |
1 | ||
2 | include $(CLEAR_VARS) | |
3 | ||
4 | LOCAL_MODULE := crossguidtest | |
5 | LOCAL_CFLAGS := -Wall | |
6 | LOCAL_SRC_FILES := ../../guid.cpp ../../test.cpp jnitest.cpp | |
7 | LOCAL_CPP_FLAGS := -std=c++11 | |
8 | LOCAL_CPPFLAGS := -DGUID_ANDROID -Wno-c++11-extensions | |
9 | ||
10 | include $(BUILD_SHARED_LIBRARY) |
0 | APP_STL := stlport_static | |
1 | NDK_TOOLCHAIN_VERSION := clang | |
2 | LOCAL_CPP_FLAGS := -DGUID_ANDROID | |
3 | APP_ABI := all |
0 | #include <string> | |
1 | #include <sstream> | |
2 | #include <jni.h> | |
3 | #include <iostream> | |
4 | #include "../../test.h" | |
5 | #include "../../guid.h" | |
6 | ||
7 | extern "C" | |
8 | { | |
9 | ||
10 | jstring Java_ca_graemehill_crossguid_testapp_MainActivity_test(JNIEnv *env, jobject thiz) | |
11 | { | |
12 | std::stringstream resultStream; | |
13 | test(GuidGenerator(env), resultStream); | |
14 | return env->NewStringUTF(resultStream.str().c_str()); | |
15 | } | |
16 | ||
17 | } |
0 | # To enable ProGuard in your project, edit project.properties | |
1 | # to define the proguard.config property as described in that file. | |
2 | # | |
3 | # Add project specific ProGuard rules here. | |
4 | # By default, the flags in this file are appended to flags specified | |
5 | # in ${sdk.dir}/tools/proguard/proguard-android.txt | |
6 | # You can edit the include path and order by changing the ProGuard | |
7 | # include property in project.properties. | |
8 | # | |
9 | # For more details, see | |
10 | # http://developer.android.com/guide/developing/tools/proguard.html | |
11 | ||
12 | # Add any project specific keep options here: | |
13 | ||
14 | # If your project uses WebView with JS, uncomment the following | |
15 | # and specify the fully qualified class name to the JavaScript interface | |
16 | # class: | |
17 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | |
18 | # public *; | |
19 | #} |
0 | # This file is automatically generated by Android Tools. | |
1 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! | |
2 | # | |
3 | # This file must be checked in Version Control Systems. | |
4 | # | |
5 | # To customize properties used by the Ant build system edit | |
6 | # "ant.properties", and override values to adapt the script to your | |
7 | # project structure. | |
8 | # | |
9 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): | |
10 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt | |
11 | ||
12 | # Project target. | |
13 | target=android-19 |
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown
0 | <?xml version="1.0" encoding="utf-8"?> | |
1 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
2 | android:orientation="vertical" | |
3 | android:layout_width="fill_parent" | |
4 | android:layout_height="fill_parent" | |
5 | > | |
6 | <TextView | |
7 | android:id="@+id/mainTextView" | |
8 | android:layout_width="fill_parent" | |
9 | android:layout_height="wrap_content" | |
10 | android:text="Graeme says hi" | |
11 | /> | |
12 | </LinearLayout> | |
13 |
0 | <?xml version="1.0" encoding="utf-8"?> | |
1 | <resources> | |
2 | <string name="app_name">MainActivity</string> | |
3 | </resources> |
0 | package ca.graemehill.crossguid.testapp; | |
1 | ||
2 | import android.app.Activity; | |
3 | import android.os.Bundle; | |
4 | import android.widget.TextView; | |
5 | import java.util.UUID; | |
6 | ||
7 | public class MainActivity extends Activity { | |
8 | ||
9 | @Override | |
10 | public void onCreate(Bundle savedInstanceState) { | |
11 | super.onCreate(savedInstanceState); | |
12 | setContentView(R.layout.main); | |
13 | ||
14 | final TextView textView = (TextView)findViewById(R.id.mainTextView); | |
15 | textView.setText(test()); | |
16 | } | |
17 | ||
18 | public native String test(); | |
19 | ||
20 | static { | |
21 | System.loadLibrary("crossguidtest"); | |
22 | } | |
23 | } |
0 | #!/usr/bin/env bash | |
1 | ||
2 | pushd android | |
3 | ndk-build clean || { exit 1; } | |
4 | ndk-build || { exit 1; } | |
5 | ant debug || { exit 1; } | |
6 | adb uninstall ca.graemehill.crossguid.testapp || { exit 1; } | |
7 | adb install bin/TestApp-debug.apk || { exit 1; } | |
8 | adb shell am start -n ca.graemehill.crossguid.testapp/ca.graemehill.crossguid.testapp.MainActivity | |
9 | popd |
0 | /* | |
1 | The MIT License (MIT) | |
2 | ||
3 | Copyright (c) 2014 Graeme Hill (http://graemehill.ca) | |
4 | ||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | of this software and associated documentation files (the "Software"), to deal | |
7 | in the Software without restriction, including without limitation the rights | |
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | copies of the Software, and to permit persons to whom the Software is | |
10 | furnished to do so, subject to the following conditions: | |
11 | ||
12 | The above copyright notice and this permission notice shall be included in | |
13 | all copies or substantial portions of the Software. | |
14 | ||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | THE SOFTWARE. | |
22 | */ | |
23 | ||
24 | #include "guid.h" | |
25 | ||
26 | #ifdef GUID_LIBUUID | |
27 | #include <uuid/uuid.h> | |
28 | #endif | |
29 | ||
30 | #ifdef GUID_CFUUID | |
31 | #include <CoreFoundation/CFUUID.h> | |
32 | #endif | |
33 | ||
34 | #ifdef GUID_WINDOWS | |
35 | #include <objbase.h> | |
36 | #endif | |
37 | ||
38 | #ifdef GUID_ANDROID | |
39 | #include <jni.h> | |
40 | #endif | |
41 | ||
42 | using namespace std; | |
43 | ||
44 | // overload << so that it's easy to convert to a string | |
45 | ostream &operator<<(ostream &s, const Guid &guid) | |
46 | { | |
47 | return s << hex << setfill('0') | |
48 | << setw(2) << (int)guid._bytes[0] | |
49 | << setw(2) << (int)guid._bytes[1] | |
50 | << setw(2) << (int)guid._bytes[2] | |
51 | << setw(2) << (int)guid._bytes[3] | |
52 | << "-" | |
53 | << setw(2) << (int)guid._bytes[4] | |
54 | << setw(2) << (int)guid._bytes[5] | |
55 | << "-" | |
56 | << setw(2) << (int)guid._bytes[6] | |
57 | << setw(2) << (int)guid._bytes[7] | |
58 | << "-" | |
59 | << setw(2) << (int)guid._bytes[8] | |
60 | << setw(2) << (int)guid._bytes[9] | |
61 | << "-" | |
62 | << setw(2) << (int)guid._bytes[10] | |
63 | << setw(2) << (int)guid._bytes[11] | |
64 | << setw(2) << (int)guid._bytes[12] | |
65 | << setw(2) << (int)guid._bytes[13] | |
66 | << setw(2) << (int)guid._bytes[14] | |
67 | << setw(2) << (int)guid._bytes[15]; | |
68 | } | |
69 | ||
70 | // create a guid from vector of bytes | |
71 | Guid::Guid(const vector<unsigned char> &bytes) | |
72 | { | |
73 | _bytes = bytes; | |
74 | } | |
75 | ||
76 | // create a guid from array of bytes | |
77 | Guid::Guid(const unsigned char *bytes) | |
78 | { | |
79 | _bytes.assign(bytes, bytes + 16); | |
80 | } | |
81 | ||
82 | // converts a single hex char to a number (0 - 15) | |
83 | unsigned char hexDigitToChar(char ch) | |
84 | { | |
85 | if (ch > 47 && ch < 58) | |
86 | return ch - 48; | |
87 | ||
88 | if (ch > 96 && ch < 103) | |
89 | return ch - 87; | |
90 | ||
91 | if (ch > 64 && ch < 71) | |
92 | return ch - 55; | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
97 | // converts the two hexadecimal characters to an unsigned char (a byte) | |
98 | unsigned char hexPairToChar(char a, char b) | |
99 | { | |
100 | return hexDigitToChar(a) * 16 + hexDigitToChar(b); | |
101 | } | |
102 | ||
103 | // create a guid from string | |
104 | Guid::Guid(const string &fromString) | |
105 | { | |
106 | _bytes.clear(); | |
107 | ||
108 | char charOne, charTwo; | |
109 | bool lookingForFirstChar = true; | |
110 | ||
111 | for (const char &ch : fromString) | |
112 | { | |
113 | if (ch == '-') | |
114 | continue; | |
115 | ||
116 | if (lookingForFirstChar) | |
117 | { | |
118 | charOne = ch; | |
119 | lookingForFirstChar = false; | |
120 | } | |
121 | else | |
122 | { | |
123 | charTwo = ch; | |
124 | auto byte = hexPairToChar(charOne, charTwo); | |
125 | _bytes.push_back(byte); | |
126 | lookingForFirstChar = true; | |
127 | } | |
128 | } | |
129 | ||
130 | } | |
131 | ||
132 | // create empty guid | |
133 | Guid::Guid() | |
134 | { | |
135 | _bytes = vector<unsigned char>(16, 0); | |
136 | } | |
137 | ||
138 | // copy constructor | |
139 | Guid::Guid(const Guid &other) | |
140 | { | |
141 | _bytes = other._bytes; | |
142 | } | |
143 | ||
144 | // overload assignment operator | |
145 | Guid &Guid::operator=(const Guid &other) | |
146 | { | |
147 | _bytes = other._bytes; | |
148 | return *this; | |
149 | } | |
150 | ||
151 | // overload equality operator | |
152 | bool Guid::operator==(const Guid &other) const | |
153 | { | |
154 | return _bytes == other._bytes; | |
155 | } | |
156 | ||
157 | // overload inequality operator | |
158 | bool Guid::operator!=(const Guid &other) const | |
159 | { | |
160 | return !((*this) == other); | |
161 | } | |
162 | ||
163 | // This is the linux friendly implementation, but it could work on other | |
164 | // systems that have libuuid available | |
165 | #ifdef GUID_LIBUUID | |
166 | Guid GuidGenerator::newGuid() | |
167 | { | |
168 | uuid_t id; | |
169 | uuid_generate(id); | |
170 | return id; | |
171 | } | |
172 | #endif | |
173 | ||
174 | // this is the mac and ios version | |
175 | #ifdef GUID_CFUUID | |
176 | Guid GuidGenerator::newGuid() | |
177 | { | |
178 | auto newId = CFUUIDCreate(NULL); | |
179 | auto bytes = CFUUIDGetUUIDBytes(newId); | |
180 | CFRelease(newId); | |
181 | ||
182 | const unsigned char byteArray[16] = | |
183 | { | |
184 | bytes.byte0, | |
185 | bytes.byte1, | |
186 | bytes.byte2, | |
187 | bytes.byte3, | |
188 | bytes.byte4, | |
189 | bytes.byte5, | |
190 | bytes.byte6, | |
191 | bytes.byte7, | |
192 | bytes.byte8, | |
193 | bytes.byte9, | |
194 | bytes.byte10, | |
195 | bytes.byte11, | |
196 | bytes.byte12, | |
197 | bytes.byte13, | |
198 | bytes.byte14, | |
199 | bytes.byte15 | |
200 | }; | |
201 | return byteArray; | |
202 | } | |
203 | #endif | |
204 | ||
205 | // obviously this is the windows version | |
206 | #ifdef GUID_WINDOWS | |
207 | Guid GuidGenerator::newGuid() | |
208 | { | |
209 | GUID newId; | |
210 | CoCreateGuid(&newId); | |
211 | ||
212 | const unsigned char bytes[16] = | |
213 | { | |
214 | (newId.Data1 >> 24) & 0xFF, | |
215 | (newId.Data1 >> 16) & 0xFF, | |
216 | (newId.Data1 >> 8) & 0xFF, | |
217 | (newId.Data1) & 0xff, | |
218 | ||
219 | (newId.Data2 >> 8) & 0xFF, | |
220 | (newId.Data2) & 0xff, | |
221 | ||
222 | (newId.Data3 >> 8) & 0xFF, | |
223 | (newId.Data3) & 0xFF, | |
224 | ||
225 | newId.Data4[0], | |
226 | newId.Data4[1], | |
227 | newId.Data4[2], | |
228 | newId.Data4[3], | |
229 | newId.Data4[4], | |
230 | newId.Data4[5], | |
231 | newId.Data4[6], | |
232 | newId.Data4[7] | |
233 | }; | |
234 | ||
235 | return bytes; | |
236 | } | |
237 | #endif | |
238 | ||
239 | // android version that uses a call to a java api | |
240 | #ifdef GUID_ANDROID | |
241 | GuidGenerator::GuidGenerator(JNIEnv *env) | |
242 | { | |
243 | _env = env; | |
244 | _uuidClass = env->FindClass("java/util/UUID"); | |
245 | _newGuidMethod = env->GetStaticMethodID(_uuidClass, "randomUUID", "()Ljava/util/UUID;"); | |
246 | _mostSignificantBitsMethod = env->GetMethodID(_uuidClass, "getMostSignificantBits", "()J"); | |
247 | _leastSignificantBitsMethod = env->GetMethodID(_uuidClass, "getLeastSignificantBits", "()J"); | |
248 | } | |
249 | ||
250 | Guid GuidGenerator::newGuid() | |
251 | { | |
252 | jobject javaUuid = _env->CallStaticObjectMethod(_uuidClass, _newGuidMethod); | |
253 | jlong mostSignificant = _env->CallLongMethod(javaUuid, _mostSignificantBitsMethod); | |
254 | jlong leastSignificant = _env->CallLongMethod(javaUuid, _leastSignificantBitsMethod); | |
255 | ||
256 | unsigned char bytes[16] = | |
257 | { | |
258 | (mostSignificant >> 56) & 0xFF, | |
259 | (mostSignificant >> 48) & 0xFF, | |
260 | (mostSignificant >> 40) & 0xFF, | |
261 | (mostSignificant >> 32) & 0xFF, | |
262 | (mostSignificant >> 24) & 0xFF, | |
263 | (mostSignificant >> 16) & 0xFF, | |
264 | (mostSignificant >> 8) & 0xFF, | |
265 | (mostSignificant) & 0xFF, | |
266 | (leastSignificant >> 56) & 0xFF, | |
267 | (leastSignificant >> 48) & 0xFF, | |
268 | (leastSignificant >> 40) & 0xFF, | |
269 | (leastSignificant >> 32) & 0xFF, | |
270 | (leastSignificant >> 24) & 0xFF, | |
271 | (leastSignificant >> 16) & 0xFF, | |
272 | (leastSignificant >> 8) & 0xFF, | |
273 | (leastSignificant) & 0xFF, | |
274 | }; | |
275 | return bytes; | |
276 | } | |
277 | #endif |
0 | /* | |
1 | The MIT License (MIT) | |
2 | ||
3 | Copyright (c) 2014 Graeme Hill (http://graemehill.ca) | |
4 | ||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | of this software and associated documentation files (the "Software"), to deal | |
7 | in the Software without restriction, including without limitation the rights | |
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | copies of the Software, and to permit persons to whom the Software is | |
10 | furnished to do so, subject to the following conditions: | |
11 | ||
12 | The above copyright notice and this permission notice shall be included in | |
13 | all copies or substantial portions of the Software. | |
14 | ||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | THE SOFTWARE. | |
22 | */ | |
23 | ||
24 | #pragma once | |
25 | ||
26 | #include <iostream> | |
27 | #include <vector> | |
28 | #include <sstream> | |
29 | #include <string> | |
30 | #include <iomanip> | |
31 | ||
32 | #ifdef GUID_ANDROID | |
33 | #include <jni.h> | |
34 | #endif | |
35 | ||
36 | // Class to represent a GUID/UUID. Each instance acts as a wrapper around a | |
37 | // 16 byte value that can be passed around by value. It also supports | |
38 | // conversion to string (via the stream operator <<) and conversion from a | |
39 | // string via constructor. | |
40 | class Guid | |
41 | { | |
42 | public: | |
43 | ||
44 | // create a guid from vector of bytes | |
45 | Guid(const std::vector<unsigned char> &bytes); | |
46 | ||
47 | // create a guid from array of bytes | |
48 | Guid(const unsigned char *bytes); | |
49 | ||
50 | // create a guid from string | |
51 | Guid(const std::string &fromString); | |
52 | ||
53 | // create empty guid | |
54 | Guid(); | |
55 | ||
56 | // copy constructor | |
57 | Guid(const Guid &other); | |
58 | ||
59 | // overload assignment operator | |
60 | Guid &operator=(const Guid &other); | |
61 | ||
62 | // overload equality and inequality operator | |
63 | bool operator==(const Guid &other) const; | |
64 | bool operator!=(const Guid &other) const; | |
65 | ||
66 | private: | |
67 | ||
68 | // actual data | |
69 | std::vector<unsigned char> _bytes; | |
70 | ||
71 | // make the << operator a friend so it can access _bytes | |
72 | friend std::ostream &operator<<(std::ostream &s, const Guid &guid); | |
73 | }; | |
74 | ||
75 | // Class that can create new guids. The only reason this exists instead of | |
76 | // just a global "newGuid" function is because some platforms will require | |
77 | // that there is some attached context. In the case of android, we need to | |
78 | // know what JNIEnv is being used to call back to Java, but the newGuid() | |
79 | // function would no longer be cross-platform if we parameterized the android | |
80 | // version. Instead, construction of the GuidGenerator may be different on | |
81 | // each platform, but the use of newGuid is uniform. | |
82 | class GuidGenerator | |
83 | { | |
84 | public: | |
85 | #ifdef GUID_ANDROID | |
86 | GuidGenerator(JNIEnv *env); | |
87 | #else | |
88 | GuidGenerator() { } | |
89 | #endif | |
90 | ||
91 | Guid newGuid(); | |
92 | ||
93 | #ifdef GUID_ANDROID | |
94 | private: | |
95 | JNIEnv *_env; | |
96 | jclass _uuidClass; | |
97 | jmethodID _newGuidMethod; | |
98 | jmethodID _mostSignificantBitsMethod; | |
99 | jmethodID _leastSignificantBitsMethod; | |
100 | #endif | |
101 | }; |
0 | #!/usr/bin/env bash | |
1 | ||
2 | ./clean.sh | |
3 | ||
4 | g++ -c guid.cpp -o guid.o -Wall -std=c++11 -DGUID_LIBUUID | |
5 | g++ -c test.cpp -o test.o -Wall -std=c++11 | |
6 | g++ -c testmain.cpp -o testmain.o -Wall | |
7 | g++ test.o guid.o testmain.o -o test -luuid | |
8 | chmod +x test | |
9 | ./test |
0 | #!/usr/bin/env bash | |
1 | ||
2 | ./clean.sh | |
3 | ||
4 | clang++ -c guid.cpp -o guid.o -Wall -std=c++11 -DGUID_CFUUID | |
5 | clang++ -c test.cpp -o test.o -Wall -std=c++11 | |
6 | clang++ -c testmain.cpp -o testmain.o -Wall -std=c++11 | |
7 | clang++ test.o guid.o testmain.o -o test -framework CoreFoundation | |
8 | chmod +x test | |
9 | ./test |
0 | #include "guid.h" | |
1 | ||
2 | using namespace std; | |
3 | ||
4 | int test(GuidGenerator generator, std::ostream &outStream) | |
5 | { | |
6 | auto r1 = generator.newGuid(); | |
7 | auto r2 = generator.newGuid(); | |
8 | auto r3 = generator.newGuid(); | |
9 | ||
10 | outStream << r1 << endl << r2 << endl << r3 << endl; | |
11 | ||
12 | Guid s1("7bcd757f-5b10-4f9b-af69-1a1f226f3b3e"); | |
13 | Guid s2("16d1bd03-09a5-47d3-944b-5e326fd52d27"); | |
14 | Guid s3("fdaba646-e07e-49de-9529-4499a5580c75"); | |
15 | Guid s4("7bcd757f-5b10-4f9b-af69-1a1f226f3b3e"); | |
16 | ||
17 | if (r1 == r2 || r1 == r3 || r2 == r3) | |
18 | { | |
19 | outStream << "FAIL - not all random guids are different" << endl; | |
20 | return 1; | |
21 | } | |
22 | ||
23 | if (s1 == s2) | |
24 | { | |
25 | outStream << "FAIL - s1 and s2 should be different" << endl; | |
26 | return 1; | |
27 | } | |
28 | ||
29 | if (s1 != s4) | |
30 | { | |
31 | outStream << "FAIL - s1 and s4 should be equal" << endl; | |
32 | return 1; | |
33 | } | |
34 | ||
35 | stringstream ss1; | |
36 | ss1 << s1; | |
37 | if (ss1.str() != "7bcd757f-5b10-4f9b-af69-1a1f226f3b3e") | |
38 | { | |
39 | outStream << "FAIL - string generated from s1 is wrong" << endl; | |
40 | return 1; | |
41 | } | |
42 | ||
43 | stringstream ss2; | |
44 | ss2 << s2; | |
45 | if (ss2.str() != "16d1bd03-09a5-47d3-944b-5e326fd52d27") | |
46 | { | |
47 | outStream << "FAIL - string generated from s2 is wrong" << endl; | |
48 | return 1; | |
49 | } | |
50 | ||
51 | stringstream ss3; | |
52 | ss3 << s3; | |
53 | if (ss3.str() != "fdaba646-e07e-49de-9529-4499a5580c75") | |
54 | { | |
55 | outStream << "FAIL - string generated from s3 is wrong" << endl; | |
56 | return 1; | |
57 | } | |
58 | ||
59 | outStream << "All tests passed!" << endl; | |
60 | ||
61 | return 0; | |
62 | } |