SayoriOS  0.3.3
variable.c
См. документацию.
1 
11 #include <mem/vmm.h>
12 #include "lib/php/str_contains.h"
13 #include <io/ports.h>
14 #include <sys/variable.h>
15 
16 VARIABLE G_VARIABLE[128] = {0};
17 size_t C_VARIABLE = 0;
18 
19 size_t variable_freeID(char* Key){
20  bool Key404 = false;
21  for (int i = 0; i < 512;i++){
23  if (strcmpn(G_VARIABLE[i].Key,Key)) return i;
24  if (G_VARIABLE[i].Ready == 0 && Key404) return i;
25  if (i == 511 && !Key404){
26  Key404 = true;
27  i = 0;
28  }
29  }
30  return -1;
31 }
32 
33 int variable_write(char* Key, char* Value){
34  qemu_log("[Variable] [Write] %s=%s",Key,Value);
35  size_t inx = variable_freeID(Key);
36  if (inx == -1){
37  qemu_log("[Variable] [Error] Return index: %d",inx);
38  return 0;
39  }
40  if (G_VARIABLE[inx].Ready == 1 && strlen(Value) == 0){
41  C_VARIABLE--;
42  } else if (G_VARIABLE[inx].Ready == 0 && strlen(Value) != 0){
43  C_VARIABLE++;
44  }
45  G_VARIABLE[inx].Ready = (strlen(Value) == 0?0:1);
46  memcpy(G_VARIABLE[inx].Key,Key,strlen(Key));
47  memcpy(G_VARIABLE[inx].Value,Value,strlen(Value));
48  return 1;
49 }
50 
51 char* variable_read(char* Key){
52  qemu_log("[Variable] [Read] %s",Key);
53  for (int i = 0; i < 512;i++){
54  if (G_VARIABLE[i].Ready && strcmpn(G_VARIABLE[i].Key,Key)){
55  qemu_log("[Variable] [Read] %s=%s",G_VARIABLE[i].Key,G_VARIABLE[i].Value);
56  return G_VARIABLE[i].Value;
57  }
58  }
59  qemu_log("[Variable] [Read] %s=NULL",Key);
60  return NULL;
61 }
62 
63 VARIABLE* variable_list(char* Search){
64  qemu_log("[Variable] [List] [ALL=%d] Search: %s",C_VARIABLE,Search);
65  VARIABLE* list = kmalloc(sizeof(VARIABLE)*(C_VARIABLE+1));
66  size_t inx = 0;
67  for (int i = 0; i < 512;i++){
68  if (G_VARIABLE[i].Ready == 0) continue;
69  qemu_log("[%d] Ready: %d | Search:%d | Key:%s",inx, G_VARIABLE[i].Ready, str_contains(G_VARIABLE[i].Key,Search), G_VARIABLE[i].Key);
70  if (str_contains(G_VARIABLE[i].Key,Search) == 0) continue;
71  list[inx].Ready = 1;
72  memcpy(list[inx].Key,G_VARIABLE[i].Key,strlen(G_VARIABLE[i].Key));
73  memcpy(list[inx].Value,G_VARIABLE[i].Value,strlen(G_VARIABLE[i].Value));
74  inx++;
75  }
76  list[inx].Ready = 0;
77  return list;
78 }
size_t strlen(const char *str)
Возращает длину строки
Definition: string.c:88
bool strcmpn(const char *str1, const char *str2)
Сравнение строк
Definition: string.c:270
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
size_t variable_freeID(char *Key)
Definition: variable.c:19