Skip to content

Commit

Permalink
Merge pull request #3 from shumin1027/escape_character
Browse files Browse the repository at this point in the history
escape character
  • Loading branch information
adamsla authored May 12, 2020
2 parents c2f5a5e + 50b3ec7 commit 875ab60
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/lsfeventsparser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NTLIB = kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
NTINCLUDE = ${LSF_INCLUDE} ${JNI_INC} -I. ${COMM_INC} -I $(HOME)/includeNT

# 4 obj files
OBJS = lsbevent_parse.$(OEXT) job_array.$(OEXT) json4c.$(OEXT)
OBJS = lsbevent_parse.$(OEXT) job_array.$(OEXT) json4c.$(OEXT) strreplace.$(OEXT)

# 5 build obj file and lib file

Expand Down Expand Up @@ -41,6 +41,9 @@ lsbevent_parse.o: lsbevent_parse.c
lsbevent_parse_test: lsbevent_parse_test.c libreadlsbevents.so
@$(CC) -D${LSF_VERSION} ${JNI_INC} ${LSF_INCLUDE} ${COMM_INC} -I. -L. -lreadlsbevents -o $@ ${EXTRA_CFLAGS} lsbevent_parse_test.c; \

strreplace.o:strreplace.c
@$(CC) -D${LSF_VERSION} ${JNI_INC} ${LSF_INCLUDE} ${COMM_INC} -I. -c -o $@ ${EXTRA_CFLAGS} $^; \

json4c.o:json4c.c
@$(CC) -D${LSF_VERSION} ${JNI_INC} ${LSF_INCLUDE} ${COMM_INC} -I. -c -o $@ ${EXTRA_CFLAGS} $^; \

Expand Down
9 changes: 7 additions & 2 deletions src/lsfeventsparser/json4c.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
************************************************************************/

#include "strreplace.h"
#include "json4c.h"
#include <math.h>
#include <stdarg.h>
Expand Down Expand Up @@ -121,10 +122,14 @@ void addStringToObject(Json4c *object, const char *key, char *value) {
return;
}

char *str = strreplace(value, "\"", "\\\"");
// str = strreplace(str, "\'", "\\\'");

ksnprintf(&string->key, "%s", key);
ksnprintf(&string->valuestring, "%s", value);
ksnprintf(&string->valuestring, "%s", str);

addChild(object, string);
free(str);
}

void addInstanceToObject(Json4c *object, const char *key, Json4c *instance) {
Expand Down Expand Up @@ -433,7 +438,7 @@ int ksnprintf(char **target, char *format, ...) {
FREEUP(*target);
realLen = initLen * index++;
*target = malloc(realLen * sizeof(char));
memset(*target, 0, realLen * sizeof(char));
memset(*target, 0, realLen * sizeof(char));

va_start(ap, format);
len = vsnprintf(*target, realLen, format, ap);
Expand Down
46 changes: 46 additions & 0 deletions src/lsfeventsparser/strreplace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by shumin on 2019/10/29.
//

#include <string.h>
#include <stdlib.h>

char *strreplace(char const *const original,
char const *const pattern, char const *const replacement) {
size_t const replen = strlen(replacement);
size_t const patlen = strlen(pattern);
size_t const orilen = strlen(original);

size_t patcnt = 0;
const char *oriptr;
const char *patloc;

// find how many times the pattern occurs in the original string
for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen) {
patcnt++;
}

{
// allocate memory for the new string
size_t const retlen = orilen + patcnt * (replen - patlen);
char *const returned = (char *) malloc(sizeof(char) * (retlen + 1));

if (returned != NULL) {
// copy the original string,
// replacing all the instances of the pattern
char *retptr = returned;
for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen) {
size_t const skplen = patloc - oriptr;
// copy the section until the occurence of the pattern
strncpy(retptr, oriptr, skplen);
retptr += skplen;
// copy the replacement
strncpy(retptr, replacement, replen);
retptr += replen;
}
// copy the rest of the string.
strcpy(retptr, oriptr);
}
return returned;
}
}
6 changes: 6 additions & 0 deletions src/lsfeventsparser/strreplace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//
// Created by shumin on 2019/10/29.
//

char *strreplace(char const *const original,
char const *const pattern, char const *const replacement);

0 comments on commit 875ab60

Please sign in to comment.