Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

26.3. C Library

26.3.1. lib

26.3.1.1. syslog.h

			
# cat syslog.c
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>

int main(void) {

 openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
 syslog(LOG_INFO, "A different kind of Hello world ... ");
 closelog();

 return 0;
}
			
			
[root@dev1 test]# gcc syslog.c
[root@dev1 test]# ls
a.out  syslog.c
[root@dev1 test]# ./a.out

[root@dev1 test]# tail /var/log/messages
Jan 11 23:52:27 dev1 slog[5056]: A different kind of Hello world ...
			

26.3.1.2. stdio.h

fscanf/fprintf
			
#include<stdio.h>
int main()
{
	FILE *file;
	char name[20][20];

	int a[10]={0};

	int i,j;
	if((file=fopen("1.txt","rt"))==NULL)
	{
		printf("Cannot open file strike any key exit!");
		return 0;
	}
	i=0;
	while(fscanf(file,"%s %d\n",name[i],&a[i])!=EOF)
	{
		i++;
	}
	i=0;
	while(a[i]!=0)
	{
		printf("%s %d\n",name[i],a[i]);
		i++;
	}
	fclose(file);
}
			
			
			
#include <stdio.h>
#include <string.h>

typedef struct _Address
{
	char *name;
	int age;
}Address;

int main()
{
	FILE *file;
	int num;
	char str[256];
	int i;
	char *p;
	Address addr[10]={0};

	if((file=fopen("1.txt","rt"))==NULL)
	{
		printf("Cannot open file strike any key exit!");
		return 0;
	}
	i=0;

	while(fscanf(file,"%s %d\n",str,&num)!=EOF)
	{
		asprintf(&addr[i].name, "%s", str);
		addr[i].age = num;
		i++;
	}
	fclose(file);
	addr[i].name = NULL;

	i=0;
	while(1){
		if(addr[i].name == NULL) break;
		printf("%d: %s %d\n",i, addr[i].name,addr[i].age);
		i++;
	}
}
			
			

26.3.2. libssh2

http://www.libssh2.org/

26.3.3. libconfig – C/C++ Configuration File Library

http://www.hyperrealm.com/main.php?s=libconfig

libconfig 可用於處理 *.conf 配置檔案

26.3.4. libuv

提供Socket,進程綫程處理等等

http://nikhilm.github.io/uvbook/

26.3.5. newt

https://fedorahosted.org/newt/

http://sourcecodebrowser.com/newt/0.52.10/windows_8c.html

http://gnewt.sourceforge.net/tutorial-4.html#ss4.1

26.3.6. Spdylay - SPDY C Library

http://spdylay.sourceforge.net/

26.3.7. libPhenom

http://facebook.github.io/libphenom/

libPhenom is an eventing framework for building high performance and high scalability systems in C

26.3.8. curl

	
#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
	
	

get content

	
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct string {
  char *ptr;
  size_t len;
};

void init_string(struct string *s) {
  s->len = 0;
  s->ptr = malloc(s->len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "malloc() failed\n");
    exit(EXIT_FAILURE);
  }
  s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
  size_t new_len = s->len + size*nmemb;
  s->ptr = realloc(s->ptr, new_len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "realloc() failed\n");
    exit(EXIT_FAILURE);
  }
  memcpy(s->ptr+s->len, ptr, size*nmemb);
  s->ptr[new_len] = '\0';
  s->len = new_len;

  return size*nmemb;
}

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    struct string s;
    init_string(&s);

    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
    res = curl_easy_perform(curl);

    printf("%s\n", s.ptr);
    free(s.ptr);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
	
	

26.3.8.1. url encode / decode

encode

char *subject= curl_easy_escape(curl, "測試主題", 0);		
		

decode

char *subject= curl_easy_unescape(curl, "測試主題", 0);		
		

26.3.9. libxml

26.3.9.1. example

創建xml

		
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

int main(int argc, char **argv)
{
	xmlDocPtr doc = NULL;
	xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;

	doc = xmlNewDoc(BAD_CAST "1.0"); // create a new xml document.
	root_node = xmlNewNode(NULL, BAD_CAST "root"); // create a root node.
	xmlDocSetRootElement(doc, root_node);

	xmlNewChild(root_node, NULL, BAD_CAST "node1", BAD_CAST "content of node1");
	//xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);

	node = xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST "node3 has attributes");
	xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");

	node = xmlNewNode(NULL, BAD_CAST "node4");
	node1 = xmlNewText(BAD_CAST
	           "other way to create content (which is also a node)");
	xmlAddChild(node, node1);
	xmlAddChild(root_node, node);

	xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);

	xmlFreeDoc(doc);

	xmlCleanupParser();

	xmlMemoryDump();
	return(0);
}
		
		

26.3.9.2. Creating string with libxml2

		
	xmlChar *s;
	int size;
	xmlDocDumpMemory(doc, &s, &size);
	xmlFree(s);

	printf("xml: %s", (char *)s);