Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions bindings/python/pycpslib_build.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
# pycpslib.py

import os

import sys
from cffi import FFI

ffi = FFI()
project_root = os.path.abspath("../../")
if sys.platform == "win32":
ffi.set_source("pycpslib",
"""#include "pslib.h"
""",
sources=["../../pslib_windows.c", "../../common.c"],
library_dirs = [project_root],
include_dirs = [project_root])

ffi.cdef('''
typedef int32_t pid_t;
typedef int32_t bool;
#define BELOW_NORMAL_PRIORITY_CLASS 0x00004000
#define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
#define NORMAL_PRIORITY_CLASS 0x00000020
#define IDLE_PRIORITY_CLASS 0x00000040
#define HIGH_PRIORITY_CLASS 0x00000080
#define REALTIME_PRIORITY_CLASS 0x00000100
''',override = True)

ffi.set_source("pycpslib",
lines = open(project_root+"/pslib.h").readlines()
altered_lines = []
flag = 1
for line in lines:
if line.startswith('#include') or line.startswith('#define'):
altered_lines.append("")
elif line.startswith('#ifdef _WIN32'):
flag = 2
elif line.startswith('#else') :
flag = 0
elif line.startswith('#endif'):
flag = 1
else:
if (flag != 0):
altered_lines.append(line)
ffi.cdef(''.join(altered_lines),override = True)

else:
ffi.set_source("pycpslib",
"""#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
Expand All @@ -18,16 +54,16 @@
library_dirs = [project_root],
include_dirs = [project_root])

ffi.cdef('''
typedef int32_t pid_t;
typedef int32_t bool;
''')
ffi.cdef('''
typedef int32_t pid_t;
typedef int32_t bool;
''')

lines = open(project_root+"/pslib.h").readlines()
lines = open(project_root+"/pslib.h").readlines()

altered_lines = ['' if line.startswith('#include ') else line for line in lines]
altered_lines = ['' if line.startswith('#include ') else line for line in lines]

ffi.cdef(''.join(altered_lines))
ffi.cdef(''.join(altered_lines))

if __name__ == '__main__':
ffi.compile()
151 changes: 78 additions & 73 deletions common.c
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "common.h"

float percentage(uint64_t n, uint64_t d) {
/* TODO: Error check here */
float percent = ((float)n / (float)d) * 100.0;
return percent;
}

int str_comp(const void *key, const void *memb) {
const char **a = (const char **)key;
const char **b = (const char **)memb;
return strcmp(*a, *b);
}

int int_comp(const void *key, const void *memb) {
const int a = *(int *)key;
const int b = *(int *)memb;
if (a == b) {
return 0;
} else {
return -1;
}
}

char *grep_awk(FILE *fp, const char *fstr, int nfield, const char *delim) {
char *ret = NULL;
int i;
char *line = (char *)calloc(500, sizeof(char));
check_mem(line);
while (fgets(line, 400, fp) != NULL) {
if (strncasecmp(line, fstr, strlen(fstr)) == 0) {
ret = strtok(line, delim);
for (i = 0; i < nfield; i++) {
ret = strtok(NULL, delim);
}
if (ret) {
ret = strdup(ret);
check_mem(ret);
free(line);
return ret;
}
}
}
free(line);
return NULL;
error:
free(line);
return NULL;
}

char *squeeze(char *string, const char *chars) {
char *src = string;
char *target = string;
char ch;
for (; *chars; chars++) {
ch = *chars;
src = string;
target = string;
while (*src != '\0') {
if (*src != ch) {
*target = *src;
target++;
}
src++;
}
*target = '\0';
}
return string;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "common.h"
#ifdef _WIN32
#define strncasecmp strnicmp
#endif

float percentage(uint64_t n, uint64_t d) {
/* TODO: Error check here */
float percent = ((float)n / (float)d) * 100.0;
return percent;
}

int str_comp(const void *key, const void *memb) {
const char **a = (const char **)key;
const char **b = (const char **)memb;
return strcmp(*a, *b);
}

int int_comp(const void *key, const void *memb) {
const int a = *(int *)key;
const int b = *(int *)memb;
if (a == b) {
return 0;
} else {
return -1;
}
}

char *grep_awk(FILE *fp, const char *fstr, int nfield, const char *delim) {
char *ret = NULL;
int i;
char *line = (char *)calloc(500, sizeof(char));
check_mem(line);
while (fgets(line, 400, fp) != NULL) {
if (strncasecmp(line, fstr, strlen(fstr)) == 0) {
ret = strtok(line, delim);
for (i = 0; i < nfield; i++) {
ret = strtok(NULL, delim);
}
if (ret) {
ret = strdup(ret);
check_mem(ret);
free(line);
return ret;
}
}
}
free(line);
return NULL;
error:
free(line);
return NULL;
}

char *squeeze(char *string, const char *chars) {
char *src = string;
char *target = string;
char ch;
for (; *chars; chars++) {
ch = *chars;
src = string;
target = string;
while (*src != '\0') {
if (*src != ch) {
*target = *src;
target++;
}
src++;
}
*target = '\0';
}
return string;
}

119 changes: 64 additions & 55 deletions common.h
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
#pragma once

#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

float percentage(uint64_t, uint64_t);
int str_comp(const void *, const void *);
int int_comp(const void *, const void *);
char *grep_awk(FILE *, const char *, int, const char *);
char *squeeze(char *, const char *);

#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) \
fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#endif

#define clean_errno() (errno == 0 ? "None" : strerror(errno))

#define log_err(M, ...) \
fprintf(stderr, "[ERROR] (%s:%d:%s: errno: %d, %s) " M "\n", __FILE__, \
__LINE__, __FUNCTION__, errno, clean_errno(), ##__VA_ARGS__)

#define log_warn(M, ...) \
fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
clean_errno(), ##__VA_ARGS__)

#define log_info(M, ...) \
fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

#define check(A, M, ...) \
if (!(A)) { \
log_err(M, ##__VA_ARGS__); \
errno = 0; \
goto error; \
}

#define sentinel(M, ...) \
{ \
log_err(M, ##__VA_ARGS__); \
errno = 0; \
goto error; \
}

#define check_mem(A) check((A), "Out of memory.")

#define check_debug(A, M, ...) \
if (!(A)) { \
debug(M, ##__VA_ARGS__); \
errno = 0; \
goto error; \
}
#pragma once

#include <errno.h>
#include <stdio.h>
//#include <stdint.h>
#include <string.h>

typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

float percentage(uint64_t, uint64_t);
int str_comp(const void *, const void *);
int int_comp(const void *, const void *);
char *grep_awk(FILE *, const char *, int, const char *);
char *squeeze(char *, const char *);

#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) \
fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#endif

#define clean_errno() (errno == 0 ? "None" : strerror(errno))

#define log_err(M, ...) \
fprintf(stderr, "[ERROR] (%s:%d:%s: errno: %d, %s) " M "\n", __FILE__, \
__LINE__, __FUNCTION__, errno, clean_errno(), ##__VA_ARGS__)

#define log_warn(M, ...) \
fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
clean_errno(), ##__VA_ARGS__)

#define log_info(M, ...) \
fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

#define check(A, M, ...) \
if (!(A)) { \
log_err(M, ##__VA_ARGS__); \
errno = 0; \
goto error; \
}

#define sentinel(M, ...) \
{ \
log_err(M, ##__VA_ARGS__); \
errno = 0; \
goto error; \
}

#define check_mem(A) check((A), "Out of memory.")

#define check_debug(A, M, ...) \
if (!(A)) { \
debug(M, ##__VA_ARGS__); \
errno = 0; \
goto error; \
}
Loading