root/lang/c/NetworkUpdater/trunk/Build/Common.c @ 38243

Revision 38243, 4.7 kB (checked in by saturday06, 3 years ago)

sample

  • Property svn:mime-type set to text/plain
Line 
1#ifndef NUI_DISTRIBUTION
2#  include "Common.h"
3#endif
4
5int NUI_IsBadReadWritePointer(void* pointer, size_t bytes) {
6    return NUI_IsBadReadPointer(pointer, bytes) || NUI_IsBadWritePointer(pointer, bytes);
7}
8
9char* NUI_strdup(const char* s) {
10    size_t bytes = 0;
11    char* buf = NULL;
12    bytes = strlen(s) + 1;
13    buf = (char*)calloc(bytes, 1);
14    if (!buf) {
15        return NULL;
16    }
17    memcpy(buf, s, bytes);
18    return buf;
19}
20
21NU_Error NUI_Search(NUI_ThreadData* object) {
22    NU_Error error = NU_NO_ERROR;
23
24    if (!object->update_information) {
25        error = NUI_CreateDownloadContext(
26            &object->update_information,
27            object->releases_file_url,
28            object->releases_file_path);
29        if (error) {
30            goto clean_up;
31        }
32    }
33
34    if (!object->update_information->complete) {
35        error = NUI_Download(object->update_information);
36        NUI_Lock(object->common.shared);
37        object->common.shared->user.list.total = object->update_information->total;
38        object->common.shared->user.list.downloaded = object->update_information->downloaded;
39        NUI_Unlock(object->common.shared);
40        object->pending = object->update_information->pending;
41        if (error || object->pending) {
42            goto clean_up;
43        }
44    }
45
46    error = NUI_ReadReleasesFile(object);
47clean_up:
48    if (!object->pending && object->update_information) {
49                NUI_DestroyDownloadContext(object->update_information);
50        object->update_information = NULL;
51        }
52
53    return error;
54}
55
56NU_Error NUI_NetworkUpdaterToInternal(NUI_NetworkUpdater** internal_object_output, NU_NetworkUpdater* object) {
57    NU_Error error = NU_NO_ERROR;
58    NUI_NetworkUpdater* internal_object = NULL;
59
60    *internal_object_output = 0;
61
62    internal_object = (NUI_NetworkUpdater*)object->internal_data;
63
64    if (NUI_IsBadReadWritePointer(internal_object, sizeof(internal_object))) {
65        error = NU_ERROR_INVALID_UPDATER;
66        goto clean_up;
67    }
68
69        internal_object->user = (NU_NetworkUpdater*)object;
70
71        *internal_object_output = internal_object;
72
73clean_up:
74    return error;
75}
76
77NU_Error NUI_GetStatus(NUI_NetworkUpdater* object) {
78    NU_Error error = NU_NO_ERROR;
79    NUI_Lock(object->common.shared);
80    *object->user = object->common.shared->user;
81    error = object->common.shared->user.error;
82    NUI_Unlock(object->common.shared);
83    return error;
84}
85
86/* -----------------------------------------------------------------------
87 * Interfaces
88 * ----------------------------------------------------------------------- */
89
90#define NUI_API_PRECONDITION                                            \
91    NUI_NetworkUpdater* internal_context = NULL;                        \
92    object->error = NUI_NetworkUpdaterToInternal(&internal_context, object); \
93    if (object->error) {                                                \
94        return 0;                                                       \
95    }
96
97int NU_Create(NU_NetworkUpdater* object, const NU_Option* option) {
98    if (NUI_IsBadReadWritePointer(object, sizeof(*object))) {
99        return 0;
100    }
101
102    memset(object, 0, sizeof(*object));
103
104    object->internal_data = calloc(sizeof(NUI_NetworkUpdater), 1);
105    if (!object->internal_data) {
106        object->error = NU_ERROR_BAD_ALLOC;
107        return 0;
108    }
109   
110    {
111        NUI_API_PRECONDITION;
112        object->error = NUI_Create(internal_context, option);
113        return !object->error;
114    }
115}
116
117int NU_Destroy(NU_NetworkUpdater* object) {
118    NUI_API_PRECONDITION;
119    object->error = NUI_Destroy(internal_context);
120    return !object->error;
121}
122
123int NU_GetStatus(NU_NetworkUpdater* object) {
124    NUI_API_PRECONDITION;
125    object->error = NUI_GetStatus(internal_context);
126    return !object->error;
127}
128
129int NU_StartSearch(NU_NetworkUpdater* object) {
130    NUI_API_PRECONDITION;
131    object->error = NUI_StartSearch(internal_context);
132    return !object->error;
133}
134
135int NU_StartUpdate(NU_NetworkUpdater* object) {
136    NUI_API_PRECONDITION;
137    object->error = NUI_StartUpdate(internal_context);
138    return !object->error;
139}
140
141int NU_Cancel(NU_NetworkUpdater* object) {
142    NUI_API_PRECONDITION;
143    object->error = NUI_Cancel(internal_context);
144    return !object->error;
145}
146
147void InternalTest(void) {
148    NU_Error error = NU_NO_ERROR;
149    NUI_DownloadContext* object;
150
151    error = NUI_CreateDownloadContext(
152        &object,
153        _T("http://upload.wikimedia.org/wikipedia/commons/b/bd/Rondo_Alla_Turka.ogg"),
154        _T("C:\\temp\\bbb.ogg"));
155
156    if (error) {
157        abort();
158    }
159
160    do {
161        error = NUI_Download(object);
162    } while (object->pending);
163
164    NUI_DestroyDownloadContext(object);
165}
166
167
Note: See TracBrowser for help on using the browser.