Patchwork [c-hglib] level0.c: log command example, implemented using level 0

login
register
mail settings
Submitter Iulian Stana
Date Sept. 23, 2013, 7:55 p.m.
Message ID <fdb234af921ba5288ba3.1379966140@doppler>
Download mbox | patch
Permalink /patch/2612/
State Deferred
Headers show

Comments

Iulian Stana - Sept. 23, 2013, 7:55 p.m.
# HG changeset patch
# User Iulian Stana <julian.stana@gmail.com>
# Date 1379682875 -10800
#      Fri Sep 20 16:14:35 2013 +0300
# Node ID fdb234af921ba5288ba30a4159aa7ff062f30875
# Parent  0f979fbe86d000fad5f1807cfe7008d034c71483
level0.c: log command example, implemented using level 0

This is a illustative implementation for log command. The data is unparsed and
the implementation is using just level0 functions.
Matt Mackall - Sept. 24, 2013, 6:21 p.m.
On Mon, 2013-09-23 at 22:55 +0300, Iulian Stana wrote:
> # HG changeset patch
> # User Iulian Stana <julian.stana@gmail.com>
> # Date 1379682875 -10800
> #      Fri Sep 20 16:14:35 2013 +0300
> # Node ID fdb234af921ba5288ba30a4159aa7ff062f30875
> # Parent  0f979fbe86d000fad5f1807cfe7008d034c71483
> level0.c: log command example, implemented using level 0
> 
> This is a illustative implementation for log command. The data is unparsed and
> the implementation is using just level0 functions.
> 
> diff --git a/examples/level0.c b/examples/level0.c
> new file mode 100644
> --- /dev/null
> +++ b/examples/level0.c
> @@ -0,0 +1,126 @@
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +
> +#include <unistd.h>
> +
> +#include "../hglib/client.h"
> +#include "../hglib/utils.h"

Let's pass a -I to the compiler instead of relative paths here.

> +
> +#define BUFF_SIZE 4096
> +
> +/****** Convenience functions. *******/
> +
> +/**
> + * \brief Create and setup the tmp directory where the acction will happends.

You might invest in enabling a spell-checker in your editor. For
instance, flyspell in emacs.

> + * \brief Fill the current repository with commits for log command.

It's sufficient to just work on an existing repo rather than build one.
Let's aim to make an example that's as short and simple as possible.

Patch

diff --git a/examples/level0.c b/examples/level0.c
new file mode 100644
--- /dev/null
+++ b/examples/level0.c
@@ -0,0 +1,126 @@ 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <unistd.h>
+
+#include "../hglib/client.h"
+#include "../hglib/utils.h"
+
+#define BUFF_SIZE 4096
+
+/****** Convenience functions. *******/
+
+/**
+ * \brief Create and setup the tmp directory where the acction will happends.
+ * */
+void setup_tmp()
+{
+	system("hg init tmp");
+	chdir("tmp");
+}
+
+/**
+ * \brief Remove the tmp directory and all his files.
+ * */
+void clean_tmp()
+{
+	chdir("..");
+	system("rm -rf tmp");
+}
+
+/**
+ * \brief Fill the current repository with commits for log command.
+ * */
+void setup_log()
+{
+	system("touch foo ; hg add foo ; hg commit -m foo");
+	system("echo baloo > foo ; hg commit -m 'baloo text'");
+	system("touch voodoo ; hg add voodoo ; hg commit -m voodoo");
+	system("echo voodoo > voodoo ; hg commit -m 'voodoo text'");
+}
+
+/**
+ * \brief Log command example.
+ *
+ * Will read a huge mass of data in chunks and then will print this data on
+ * stdout.
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \retval exitcode
+ * */
+int hg_log_by_hand(hg_handle *handle)
+{
+	char buff[BUFF_SIZE];
+	char *comm[] = {"log", "-v", NULL};
+	int exitcode;
+	int ns;
+
+	hg_rawcommand(handle, comm);
+	hg_header *head;
+	while (head = hg_read_header(handle), head->channel != r) {
+		if (head->channel == o) {
+			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+				printf("%s", buff);
+			}
+		} else if (head->channel == e) {
+			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+				printf("error data: %s", buff);
+			}
+		}
+	}
+
+	exitcode = hg_exitcode(handle);
+	printf("exitcode = %d\n", exitcode);
+
+	return exitcode;
+}
+
+
+/**
+ * \brief Printing the welcome message.
+ *
+ * Will print the options that you will have in this example.
+ **/
+void print_select_case()
+{
+	printf("Select test case to run:\n");
+	printf("1) log \n");
+	printf("\n");
+	printf("Your choice: ");
+}
+
+
+/***** Main function. *******/
+/**
+ * \brief The main function
+ * */
+int main(int argc, char **argv)
+{
+	int select_case;
+	hg_handle *handle;
+
+	print_select_case();
+	scanf("%d", &select_case);
+	printf("\n");
+
+	if (select_case < 1 || select_case > 1) {
+		printf("Your choice is not an option...\n");
+		return -1;
+	}
+
+	switch(select_case){
+		case 1:
+			setup_tmp();
+			setup_log();
+			handle = hg_open(NULL, NULL);
+			hg_log_by_hand(handle);
+
+			hg_close(&handle);
+			clean_tmp();
+			break;
+		default:
+			break;
+	}
+
+	return 0;
+}