Commit ff61b792 authored by lahiker42's avatar lahiker42

noticed this bug... services not sorted alphabetically.


git-svn-id: https://protobuf-c.googlecode.com/svn/trunk@164 00440858-1255-0410-a3e6-75ea37f81c3a
parent 5df45e91
......@@ -29,3 +29,12 @@
- don't segfault when packing NULL strings and messages. (issue 13)
0.9 [NOT YET RELEASED]
- build issue: needed $(EXEEXT) in dependency lists for cygwin
- bug fix: protobuf_c_service_get_method_by_name() was not correct b/c
the service's methods were not sorted by name (the header file
used to incorrectly state that they were).
Now we correctly implement protobuf_c_service_get_method_by_name()
(using a bsearch indexed by separate array).
- generated source incompatibility: we added a new
member to ProtobufCServiceDescriptor (method_indices_by_name).
You will have to run the latest protobuf
to generate those structures.
......@@ -1751,9 +1751,9 @@ protobuf_c_service_descriptor_get_method_by_name
while (count > 1)
{
unsigned mid = start + count / 2;
int rv = strcmp (desc->methods[mid].name, name);
int rv = strcmp (desc->methods[desc->method_indices_by_name[mid]].name, name);
if (rv == 0)
return desc->methods + mid;
return desc->methods + desc->method_indices_by_name[mid];
if (rv < 0)
{
count = start + count - (mid - 1);
......@@ -1766,7 +1766,7 @@ protobuf_c_service_descriptor_get_method_by_name
}
if (count == 0)
return NULL;
if (strcmp (desc->methods[start].name, name) == 0)
return desc->methods + start;
if (strcmp (desc->methods[desc->method_indices_by_name[start]].name, name) == 0)
return desc->methods + desc->method_indices_by_name[start];
return NULL;
}
......@@ -230,7 +230,8 @@ struct _ProtobufCServiceDescriptor
const char *c_name;
const char *package;
unsigned n_methods;
const ProtobufCMethodDescriptor *methods; // sorted by name
const ProtobufCMethodDescriptor *methods; /* in order from .proto file */
unsigned *method_indices_by_name;
};
typedef struct _ProtobufCService ProtobufCService;
......
......@@ -150,9 +150,20 @@ void ServiceGenerator::GenerateInit(io::Printer* printer)
"}\n");
}
struct MethodIndexAndName { unsigned i; const char *name; };
static int
compare_method_index_and_name_by_name (const void *a, const void *b)
{
const MethodIndexAndName *ma = (const MethodIndexAndName *) a;
const MethodIndexAndName *mb = (const MethodIndexAndName *) b;
return strcmp (ma->name, mb->name);
}
void ServiceGenerator::GenerateServiceDescriptor(io::Printer* printer)
{
int n_methods = descriptor_->method_count();
MethodIndexAndName *mi_array = new MethodIndexAndName[n_methods];
vars_["n_methods"] = SimpleItoa(n_methods);
printer->Print(vars_, "static const ProtobufCMethodDescriptor $lcfullname$__method_descriptors[$n_methods$] =\n"
"{\n");
......@@ -163,8 +174,22 @@ void ServiceGenerator::GenerateServiceDescriptor(io::Printer* printer)
vars_["output_descriptor"] = "&" + FullNameToLower(method->output_type()->full_name()) + "__descriptor";
printer->Print(vars_,
" { \"$method$\", $input_descriptor$, $output_descriptor$ },\n");
mi_array[i].i = i;
mi_array[i].name = method->name().c_str();
}
printer->Print(vars_, "};\n");
qsort ((void*)mi_array, n_methods, sizeof (MethodIndexAndName),
compare_method_index_and_name_by_name);
printer->Print(vars_, "const unsigned $lcfullname$__method_indices_by_name[] = {\n");
for (int i = 0; i < n_methods; i++) {
vars_["i"] = SimpleItoa(mi_array[i].i);
vars_["name"] = mi_array[i].name;
vars_["comma"] = (i + 1 < n_methods) ? "," : " ";
printer->Print(vars_, " $i$$comma$ /* $name$ */\n");
}
printer->Print(vars_, "};\n");
printer->Print(vars_, "const ProtobufCServiceDescriptor $lcfullname$__descriptor =\n"
"{\n"
" PROTOBUF_C_SERVICE_DESCRIPTOR_MAGIC,\n"
......@@ -173,7 +198,8 @@ void ServiceGenerator::GenerateServiceDescriptor(io::Printer* printer)
" \"$cname$\",\n"
" \"$package$\",\n"
" $n_methods$,\n"
" $lcfullname$__method_descriptors\n"
" $lcfullname$__method_descriptors,\n"
" $lcfullname$__method_indices_by_name\n"
"};\n");
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment