File: oidinfo.c

package info (click to toggle)
librapi2 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,820 kB
  • ctags: 1,474
  • sloc: ansic: 14,036; sh: 10,572; cpp: 851; python: 338; makefile: 226
file content (199 lines) | stat: -rw-r--r-- 6,124 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* $Id: oidinfo.c 3645 2009-01-02 15:14:48Z mark_ellis $ */
#include <rapi.h>
#include <synce_log.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char* dev_name = NULL;

static void show_usage(const char* name)
{
	fprintf(stderr,
			"Syntax:\n"
			"\n"
			"\t%s [-d LEVEL] [-p DEVNAME] [-h] OID\n"
			"\n"
			"\t-d LEVEL     Set debug log level\n"
			"\t                 0 - No logging (default)\n"
			"\t                 1 - Errors only\n"
			"\t                 2 - Errors and warnings\n"
			"\t                 3 - Everything\n"
                        "\t-p DEVNAME   Mobile device name\n"
			"\t-h           Show this help message\n"
			"\tOID          The object identifier we want to know more about\n",
			name);
}

static bool handle_parameters(int argc, char** argv, CEOID* oid)
{
	int c;
	int log_level = SYNCE_LOG_LEVEL_LOWEST;

	while ((c = getopt(argc, argv, "d:p:h")) != -1)
	{
		switch (c)
		{
			case 'd':
				log_level = atoi(optarg);
				break;
			
                        case 'p':
                                dev_name = optarg;
                                break;

			case 'h':
			default:
				show_usage(argv[0]);
				return false;
		}
	}

	synce_log_set_level(log_level);

	if (optind == argc)
	{
		printf("Missing OID parameter\n\n");
		show_usage(argv[0]);
		return false;
	}

	*oid = strtol(argv[optind], NULL, 0);

	return true;
}

int main(int argc, char** argv)
{
	int result = 1;
	HRESULT hr;
	CEOID oid = 0;
	CEOIDINFO info;
        RapiConnection* connection = NULL;
	
	if (!handle_parameters(argc, argv, &oid))
		goto exit;

        if ((connection = rapi_connection_from_name(dev_name)) == NULL)
        {
          fprintf(stderr, "%s: Could not find configuration at path '%s'\n", 
                  argv[0],
                  dev_name?dev_name:"(Default)");
          goto exit;
        }
        rapi_connection_select(connection);
	hr = CeRapiInit();

	if (FAILED(hr))
	{
		fprintf(stderr, "%s: Unable to initialize RAPI: %s\n", 
				argv[0],
				synce_strerror(hr));
		goto exit;
	}

	memset(&info, 0, sizeof(info));

	if (CeOidGetInfo(oid, &info))
	{
		switch (info.wObjType)
		{
			case OBJTYPE_INVALID:
				printf("Invalid object\n");
				break;

			case OBJTYPE_FILE:
				printf("File object:\n"
						   "============\n");
				{
					char *name = wstr_to_current(info.u.infFile.szFileName);
                                        if (!name) {
                                                printf("Error: failed to convert object name to current encoding\n");
                                        } else {
                                                printf("Attributes:  %08x\n"
                                                       "Name:        \"%s\"\n"
                                                       , 
                                                       info.u.infFile.dwAttributes,
                                                       name);
                                                wstr_free_string(name);
                                        }
				}
				break;
				
			case OBJTYPE_DIRECTORY:
				printf("Directory object:\n"
						   "=================\n");
				{
					char *name = wstr_to_current(info.u.infDirectory.szDirName);
                                        if (!name) {
                                                printf("Error: failed to convert object name to current encoding\n");
                                        } else {
                                                printf("Attributes:  %08x\n"
                                                       "Parent OID:  %08x\n"
                                                       "Name:        \"%s\"\n"
                                                       , 
                                                       info.u.infDirectory.dwAttributes,
                                                       info.u.infDirectory.oidParent,
                                                       name);
                                                wstr_free_string(name);
					}
				}
				break;

			case OBJTYPE_DATABASE:
				printf(
						"Database:\n"
						"=========\n");
				{
					char *name = wstr_to_current(info.u.infDatabase.szDbaseName);
                                        if (!name) {
                                                printf("Error: failed to convert object name to current encoding\n");
                                        } else {
                                                printf(
                                                       "Flags: %08x\n"
                                                       "Name: \"%s\"\n"
                                                       "Type: %08x\n"
                                                       "Record count: %i\n"
                                                       "Sort order count: %i\n"
                                                       "Size: %i\n"
                                                       ,
                                                       info.u.infDatabase.dwFlags,
                                                       name,
                                                       info.u.infDatabase.dwDbaseType,
                                                       info.u.infDatabase.wNumRecords,
                                                       info.u.infDatabase.wNumSortOrder,
                                                       info.u.infDatabase.dwSize
                                                       );
                                                wstr_free_string(name);
					}
				}
				break;

			case OBJTYPE_RECORD:
				printf(
						"Database record:\n"
						"================\n"
						"Parent OID: %08x\n", 
						info.u.infRecord.oidParent);
				break;

			default:
				printf("Unknown object with type %i\n", info.wObjType);
				break;
		}
	}
	else
	{
		fprintf(stderr, "%s: Failed to get object information: %s\n", 
				argv[0],
				synce_strerror(CeGetLastError()));
		goto exit;
	}

	result = 0;

exit:
	CeRapiUninit();
	return result;
}