-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAEDescText.cpp
More file actions
executable file
·132 lines (118 loc) · 3.54 KB
/
AEDescText.cpp
File metadata and controls
executable file
·132 lines (118 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* AEDescText.c
* Shortcuts
*
* Created by Tom on 5/28/05.
* Copyright 2005 Abracode_. All rights reserved.
*
*/
#include "AEDescText.h"
OSStatus
CreateUniTextDescFromCFString(CFStringRef inStringRef, AEDesc *outDesc)
{
if( (inStringRef == NULL) || (outDesc == NULL) )
return paramErr;
CFIndex uniCount = CFStringGetLength(inStringRef);
const UniChar *uniString = CFStringGetCharactersPtr(inStringRef);
if( uniString != NULL )
{
return AECreateDesc(typeUnicodeText, uniString, uniCount*sizeof(UniChar), outDesc);
}
UniChar* newString = (UniChar*)malloc(uniCount*sizeof(UniChar));
if(newString == NULL)
return memFullErr;
CFRange theRange;
theRange.location = 0;
theRange.length = uniCount;
CFStringGetCharacters( inStringRef, theRange, newString);
OSStatus err = AECreateDesc(typeUnicodeText, newString, uniCount*sizeof(UniChar), outDesc);
free(newString);
return err;
}
//returns null if descriptior does not contain text
CFStringRef
CreateCFStringFromAEDesc(const AEDesc *inDesc)
{
CFStringRef outString = NULL;
if(inDesc == NULL || inDesc->dataHandle == NULL)
return NULL;
Size byteCount = AEGetDescDataSize( inDesc );
if( inDesc->descriptorType == typeCFStringRef )
{
AEGetDescData( inDesc, &outString, sizeof(CFStringRef) );
//CFString ownership is passed to us, no need to retain
}
if( inDesc->descriptorType == typeUnicodeText )
{
UniChar *newBuffer = (UniChar *)calloc(byteCount, 1);
if(newBuffer != NULL)
{
if( AEGetDescData( inDesc, newBuffer, byteCount ) == noErr)
{
if( ((byteCount/sizeof(UniChar)) > 0) && (newBuffer[0] == 0xFEFF) )
{
newBuffer++;
byteCount -= sizeof(UniChar);
}
outString = CFStringCreateWithCharacters(kCFAllocatorDefault, newBuffer, byteCount/sizeof(UniChar) );
}
free(newBuffer);
}
}
else if( inDesc->descriptorType == typeChar )
{
char *newBuffer = (char *)calloc(byteCount, 1);
if(newBuffer != NULL)
{
if( AEGetDescData( inDesc, newBuffer, byteCount ) == noErr)
{
outString = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8*)newBuffer, byteCount, CFStringGetSystemEncoding(), true);
}
free(newBuffer);
}
}
else if( inDesc->descriptorType != typeNull )
{
AEDesc textDesc = {typeNull, NULL};
if( AECoerceDesc( inDesc, typeUnicodeText, &textDesc ) == noErr)
{
if( (textDesc.descriptorType == typeUnicodeText) && (textDesc.dataHandle != NULL) )
{
byteCount = AEGetDescDataSize( &textDesc );
UniChar *newBuffer = (UniChar *)calloc(byteCount, 1);
if(newBuffer != NULL)
{
if( AEGetDescData( &textDesc, newBuffer, byteCount ) == noErr)
{
if( ((byteCount/sizeof(UniChar)) > 0) && (newBuffer[0] == 0xFEFF) )
{
newBuffer++;
byteCount -= sizeof(UniChar);
}
outString = CFStringCreateWithCharacters(kCFAllocatorDefault, newBuffer, byteCount/sizeof(UniChar) );
}
free(newBuffer);
}
}
}
else if( AECoerceDesc( inDesc, typeChar, &textDesc ) == noErr)
{
if( (textDesc.descriptorType == typeChar) && (textDesc.dataHandle != NULL) )
{
byteCount = AEGetDescDataSize( &textDesc );
char *newBuffer = (char*)calloc(byteCount, 1);
if(newBuffer != NULL)
{
if( AEGetDescData( &textDesc, newBuffer, byteCount ) == noErr)
{
outString = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8*)newBuffer, byteCount, CFStringGetSystemEncoding(), true);
}
free(newBuffer);
}
}
}
if(textDesc.dataHandle != NULL)
AEDisposeDesc( &textDesc );
}
return outString;
}