Permalink
Browse files

Add support for Windows

  • Loading branch information...
1 parent 602d3db commit 164d0aaad384eae24428f3c9f359115030da55e2 @trondn committed Jan 27, 2012
Showing with 469 additions and 95 deletions.
  1. 0 cJSON.h
  2. +114 −0 getopt.c
  3. +48 −0 getopt.h
  4. +160 −95 vacuum.c
  5. +20 −0 vacuum.sln
  6. +83 −0 vacuum.vcxproj
  7. +41 −0 vacuum.vcxproj.filters
  8. +3 −0 vacuum.vcxproj.user
View
0 cJSON.h 100755 → 100644
No changes.
View
114 getopt.c
@@ -0,0 +1,114 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ * Copyright 2011 Couchbase, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "getopt.h"
+#include <ctype.h>
+#include <string.h>
+#include <stdio.h>
+
+char *optarg;
+int opterr;
+int optind;
+int optopt;
+
+
+static int parse_longopt(int argc, char **argv,
+ const struct option *longopts, int *longindex)
+{
+ const struct option *p;
+ char *name = argv[optind] + 2;
+ if (*name == '\0') {
+ ++optind;
+ return -1;
+ }
+
+ optarg = strchr(name, '=');
+ if (optarg == NULL) {
+ // value comes next!
+ optarg = argv[optind + 1];
+ } else {
+ *optarg = '\0';
+ ++optarg;
+ }
+
+ for (p = longopts; p != NULL; ++p) {
+ if (strcmp(name, p->name) == 0) {
+ // This is it :)
+ if (p->has_arg) {
+ // we need a value!
+ if (optarg == argv[optind + 1]) {
+ ++optind;
+ }
+ if (optarg == NULL || optind >= argc) {
+ fprintf(stderr, "%s: option requires an argument -- %s\n",
+ argv[0], name);
+ return '?';
+ }
+
+ } else {
+ optarg = NULL;
+ }
+ return p->val;
+ }
+ }
+
+ return '?';
+}
+
+
+int getopt_long(int argc, char **argv, const char *optstring,
+ const struct option *longopts, int *longindex) {
+
+ if (optind + 1 >= argc) {
+ // EOF
+ return -1;
+ }
+ ++optind;
+
+ if (argv[optind][0] != '-') {
+ return -1;
+ }
+
+ if (argv[optind][1] == '-') {
+ // this is a long option
+ return parse_longopt(argc, argv, longopts, longindex);
+ } else if (argv[optind][2] != '\0') {
+ fprintf(stderr, "You can't specify multiple options with this implementation\n");
+ return '?';
+ } else {
+ // this is a short option
+ const char *p = strchr(optstring, argv[optind][1]);
+ int idx = optind;
+
+ if (p == NULL) {
+ return '?';
+ }
+
+ if (*(p + 1) == ':') {
+ optind++;
+ optarg = argv[optind];
+ if (optarg == NULL || optind >= argc) {
+ fprintf(stderr, "%s: option requires an argument -- %s\n",
+ argv[0], argv[idx] + 1);
+ return '?';
+ }
+ } else {
+ optarg = NULL;
+ }
+ return argv[idx][1];
+ }
+}
View
@@ -0,0 +1,48 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ * Copyright 2011 Couchbase, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef WIN32_GETOPT_H
+#define WIN32_GETOPT_H 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define no_argument 0
+#define required_argument 1
+#define optional_argument 2
+
+struct option {
+ const char *name;
+ int has_arg;
+ int *flag;
+ int val;
+};
+
+extern char *optarg;
+extern int opterr;
+extern int optind;
+extern int optopt;
+
+extern int getopt_long(int argc, char **argv, const char *optstring,
+ const struct option *longopts, int *longindex);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif
Oops, something went wrong.

0 comments on commit 164d0aa

Please sign in to comment.