-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.c
742 lines (596 loc) · 15.4 KB
/
service.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#include "service.h"
#include <string.h>
#include <stdlib.h>
#pragma warning(disable:4996)
//显示管理菜单
int ShowLinkmanManagerMenu(DTYPE *dOffset, DTYPE *dData)
{
char input = 0;
int nDataIndexCount = GetDataIndexCount(dOffset);
Clear();
puts("\tWellcome to Linkman Mannger System");
puts("1.Add Linkman");
puts("2.Dellete Linkman");
puts("3.Change Linkman");
puts("4.Find Linkman");
puts("5.Totol Linkman");
puts("6.Show Memory Infomation");
puts("0.Exit");
printf("\nLinkmanNum : %d\tPageNum : %d\tCurrentPage : %d\n", nDataIndexCount, PAGE_NUM, 1);
puts("--------------------------------------------------------------");
for (int i = 0; i < nDataIndexCount; i++)
{
TAGLINKMAN tempLinkman = { 0 };
char gName[STRING_LENGTH] = { 0 };
char gPhone[STRING_LENGTH] = { 0 };
GetDataIndex(dOffset, i, &tempLinkman);
GetDataBin(dData, tempLinkman.name.nOffset, tempLinkman.name.nLen, gName);
GetDataBin(dData, tempLinkman.phone.nOffset, tempLinkman.phone.nLen, gPhone);
printf("%d\t%-20s%s\r\n", i + 1, gName, gPhone);
}
puts("--------------------------------------------------------------");
printf("\nPlease input option number:");
input = getchar();
getchar();
return (int) (input - 0x30);
}
//清屏
void Clear()
{
system("cls");
}
//添加联系人
void AddLinkman(DTYPE *dOffset, DTYPE *dData)
{
char gName[STRING_LENGTH] = { 0 };
char gPhone[STRING_LENGTH] = { 0 };
TAGLINKMAN tempLinkman = { 0 };
Clear();
//姓名
puts("Please input additional linkman name:");
scanf(FORMAT_STRING(STRING_LENGTH), gName);
getchar();
//电话号码
puts("Please input additional linkman Phone Number:");
scanf(FORMAT_STRING(STRING_LENGTH), gPhone);
getchar();
int nNameLen = 0;
int nPhoneLen = 0;
int nOffset = 0;
//写入名字长度
nNameLen = strlen(gName) + 1;
nPhoneLen = strlen(gPhone) + 1;
//检查碎片区
int nRecLen = GetRecoverableDataIndexCount(dOffset);
for (int i = 0; i < nRecLen; i++)
{
GetRecoverableDataIndex(dOffset, i, &tempLinkman);
//碎片区是否能存下联系人
if ((tempLinkman.name.nLen + tempLinkman.phone.nLen) >= (nNameLen + nPhoneLen))
{
//填充名字长度
if (device_write(dData, OFFSETSTART_DATA + tempLinkman.name.nOffset, gName, nNameLen) == false)
{
printf("AddLinkman write error!");
getchar();
return;
}
tempLinkman.name.nLen = nNameLen;
//填充号码长度
if (device_write(dData, OFFSETSTART_DATA + tempLinkman.phone.nOffset, gPhone, nPhoneLen) == false)
{
printf("AddLinkman write error!");
getchar();
return;
}
tempLinkman.phone.nLen = nPhoneLen;
//添加到分配表
AddDataIndex(dOffset, &tempLinkman, sizeof(tempLinkman));
//删除碎片索引
DeleteRecoverableByIndex(dOffset, i);
//存完返回
return;
}
}
//添加
nOffset = AddDataBin(dData, gName, nNameLen);
if (-1 == nOffset)
{
printf("AddLinkman write error!");
getchar();
return;
}
tempLinkman.name.nOffset = nOffset;
tempLinkman.name.nLen = nNameLen;
//填充号码长度
nOffset = AddDataBin(dData, gPhone, nPhoneLen);
if (-1 == nOffset)
{
printf("AddLinkman write error!");
getchar();
return;
}
tempLinkman.phone.nOffset = nOffset;
tempLinkman.phone.nLen = nPhoneLen;
//写入索引分配表进行管理
AddDataIndex(dOffset, &tempLinkman, sizeof(tempLinkman));
}
/*
删除联系人
*/
void DeleteLinkman(DTYPE *dOffset, DTYPE *dData)
{
int index = 0;
Clear();
printf("Please input index :");
scanf("%d", &index);
int nOperationPoint = GetDataIndexCount(dOffset);
puts("\n-------------------------------------------------------");
if (index > nOperationPoint || index < 1)
{
puts("Invalid index!");
}
else
{
DeleteDataIndex(dOffset, index - 1);
//打印信息
printf("Delete suceess! index : %d\n", index);
}
puts("-------------------------------------------------------");
getchar();
getchar();
}
/*
查找联系人
*/
void FindLinkman(DTYPE *dOffset, DTYPE *dData)
{
char szBuffer[STRING_LENGTH] = { 0 };
int option = 0;
int nOperationPoint = GetDataIndexCount(dOffset);
Clear();
puts("-------------------------------------------------------");
puts(" 1.Find linkman by name");
puts(" 2.Find linkman by phone");
puts(" 3.Find linkman by index");
puts("-------------------------------------------------------");
printf("\nPlease input number : ");
scanf("%d", &option);
getchar();
switch (option)
{
case 1: //根据字符串查找
{
int tLen = 0;
bool flag = false;
puts("Please input find name :");
scanf(FORMAT_STRING(STRING_LENGTH), szBuffer);
getchar();
tLen = strlen(szBuffer);
puts("-------------------------------------------------------");
for (int i = 0; i < nOperationPoint; i++)
{
TAGLINKMAN tagTemp = { 0 };
GetDataIndex(dOffset, i, &tagTemp);
char szName[STRING_LENGTH] = { 0 };
char szPhone[STRING_LENGTH] = { 0 };
GetDataBin(dData, tagTemp.name.nOffset, tagTemp.name.nLen, szName);
GetDataBin(dData, tagTemp.phone.nOffset, tagTemp.phone.nLen, szPhone);
for (size_t j = 0; j < strlen(szName) - tLen + 1; j++)
{
if (strncmp(szName + j, szBuffer, tLen) == 0)
{
printf("%d\t%-20s%s\r\n", i + 1, szName, szPhone);
flag = true;
break;
}
}
}
if (!flag)
{
puts("Not Found");
}
puts("-------------------------------------------------------");
}
getchar();
break;
case 2://根据号码查找
{
int tLen = 0;
bool flag = false;
puts("Please input find phone :");
scanf(FORMAT_STRING(STRING_LENGTH), szBuffer);
getchar();
tLen = strlen(szBuffer);
puts("-------------------------------------------------------");
for (int i = 0; i < nOperationPoint; i++)
{
TAGLINKMAN tagTemp = { 0 };
GetDataIndex(dOffset, i, &tagTemp);
char szName[STRING_LENGTH] = { 0 };
char szPhone[STRING_LENGTH] = { 0 };
GetDataBin(dData, tagTemp.name.nOffset, tagTemp.name.nLen, szName);
GetDataBin(dData, tagTemp.phone.nOffset, tagTemp.phone.nLen, szPhone);
for (size_t j = 0; j < strlen(szPhone) - tLen + 1; j++)
{
if (strncmp(szPhone + j, szBuffer, tLen) == 0)
{
printf("%d\t%-20s%s\r\n", i + 1, szName, szPhone);
flag = true;
break;
}
}
}
if (!flag)
{
puts("Not Found");
}
puts("-------------------------------------------------------");
}
getchar();
break;
case 3://根据索引来查找
{
int index = 0;
printf("Please input index number (MaxIndex: %d):", nOperationPoint);
scanf("%d", &index);
puts("-------------------------------------------------------");
if (index >= nOperationPoint || index < 0)
{
puts("Invalid index!");
}
else
{
TAGLINKMAN tagTemp = { 0 };
GetDataIndex(dOffset, index - 1, &tagTemp);
char szName[STRING_LENGTH] = { 0 };
char szPhone[STRING_LENGTH] = { 0 };
GetDataBin(dData, tagTemp.name.nOffset, tagTemp.name.nLen, szName);
GetDataBin(dData, tagTemp.phone.nOffset, tagTemp.phone.nLen, szPhone);
printf("%d\t%-20s%s\r\n", index, szName, szPhone);
}
puts("-------------------------------------------------------");
}
getchar();
getchar();
break;
default:
puts("没有此选项!");
getchar();
getchar();
break;
}
}
//修改联系人
void ChangeLinkman(DTYPE *dOffset, DTYPE *dData)
{
char szNewName[STRING_LENGTH] = { 0 };
char szNewPhone[STRING_LENGTH] = { 0 };
int index = 0;
Clear();
//输入修改的索引
printf("Please input change index:");
scanf("%d", &index);
//打印出信息
ShowLinkman(dOffset, dData, index);
getchar();
//输入修改后的字符串
printf("Please input change name,input '-' is not Change : ");
scanf(FORMAT_STRING(STRING_LENGTH), szNewName);
printf("Please input change phone,input '-' is not Change : ");
scanf(FORMAT_STRING(STRING_LENGTH), szNewPhone);
int nNameLen = strlen(szNewName) + 1;
int nPhoneLen = strlen(szNewPhone) + 1;
int nOperationPoint = GetDataIndexCount(dOffset);
puts("\n-------------------------------------------------------");
if (index > nOperationPoint || index < 0)
{
puts("Invalid index!");
}
else
{
TAGLINKMAN tagTemp = { 0 };
int nOffset = 0;
GetDataIndex(dOffset, index - 1, &tagTemp);
int nCountLen = 0;
int nTempLen = tagTemp.name.nLen + tagTemp.phone.nLen;
//名字
if (strcmp(szNewName, "-") != 0)
{
nCountLen = nCountLen + nNameLen;
tagTemp.name.nLen = nNameLen;
}
else
{
nCountLen = nCountLen + tagTemp.name.nLen;
//读取名称
GetDataBin(dData, tagTemp.name.nOffset, tagTemp.name.nLen, szNewName);
}
//电话
if (strcmp(szNewPhone, "-") != 0)
{
nCountLen = nCountLen + nPhoneLen;
tagTemp.phone.nLen = nPhoneLen;
}
else
{
nCountLen = nCountLen + tagTemp.phone.nLen;
//读取号码
GetDataBin(dData, tagTemp.phone.nOffset, tagTemp.phone.nLen, szNewPhone);
}
if (nCountLen > nTempLen)
{
//添加回收
AddRecoverableDataIndex(dOffset, &tagTemp, sizeof(tagTemp));
nOffset = AddDataBin(dData, szNewName, nNameLen);
tagTemp.name.nOffset = nOffset;
nOffset = AddDataBin(dData, szNewPhone, tagTemp.phone.nLen);
tagTemp.phone.nOffset = nOffset;
}
else
{
//更新二进制
UpdateDateBin(dData, OFFSETSTART_DATA + tagTemp.name.nOffset, szNewName, nNameLen);
UpdateDateBin(dData, OFFSETSTART_DATA + tagTemp.phone.nOffset, szNewPhone, nPhoneLen);
}
//更新索引
UpdateDateIndex(dOffset, index - 1, &tagTemp, sizeof(TAGLINKMAN));
//打印信息
printf("Change suceess! index : %d\n", index);
}
puts("-------------------------------------------------------");
getchar();
getchar();
return;
}
//显示联系人信息
void ShowLinkman(DTYPE *dOffset, DTYPE *dData, int index)
{
int nOperationPoint = GetDataIndexCount(dOffset);
puts("\n-------------------------------------------------------");
if (index > nOperationPoint || index < 0)
{
puts("Invalid index!");
}
else
{
TAGLINKMAN tagTemp = { 0 };
GetDataIndex(dOffset, index - 1, &tagTemp);
char szName[STRING_LENGTH] = { 0 };
char szPhone[STRING_LENGTH] = { 0 };
GetDataBin(dData, tagTemp.name.nOffset, tagTemp.name.nLen, szName);
GetDataBin(dData, tagTemp.phone.nOffset, tagTemp.phone.nLen, szPhone);
printf("%d\t%-20s%s\r\n", index, szName, szPhone);
}
puts("-------------------------------------------------------\n");
}
/*
统计信息
*/
void TotolLinkman(DTYPE *dOffset, DTYPE *dData)
{
int nCount = GetDataIndexCount(dOffset);
TOTOLINFO aryTotolInfo[40] = { 0 };
int nTotolLen = 0;
Clear();
puts("\tLetterTotol");
puts("-------------------------------------------------------\n");
for (int i = 0; i < nCount; i++)
{
TAGLINKMAN tagTemp = { 0 };
GetDataIndex(dOffset, i, &tagTemp);
char szName[ STRING_LENGTH ] = { 0 };
GetDataBin(dData, tagTemp.name.nOffset, tagTemp.name.nLen, szName);
//统计名字里的字符
for (int j = 0; j < tagTemp.name.nLen; j++)
{
int nIndex = isAryTotol(aryTotolInfo, nTotolLen, szName[j]);
if (nIndex == -1)
{
aryTotolInfo[nTotolLen].chLetter = szName[j];
aryTotolInfo[nTotolLen].count++;
nTotolLen ++;
}
else
{
aryTotolInfo[nIndex].count++;
}
}
}
for (int j = 0; j < nTotolLen; j++)
{
printf("'%c' == %d\r\n", aryTotolInfo[j].chLetter, aryTotolInfo[j].count);
}
puts("\n-------------------------------------------------------\n");
printf("input enter exit!");
getchar();
}
/*
*/
int isAryTotol(PTOTOLINFO pAry, int nLen, char ch)
{
for (int i = 0; i < nLen; i++)
{
if (pAry[ i ].chLetter == ch)
{
return i;
}
}
return -1;
}
/*
显示内存信息
*/
void ShowMemoryInfo(DTYPE *dOffset, DTYPE *dData)
{
char szBuf[100] = { 0 };
int size = GetDataBinSize(dData);
int end = size + (10 - (size % 10));
device_read(dData, OFFSETSTART_DATA, szBuf, 100);
Clear();
printf("----------------------------- MEMORY INFO -----------------------------");
for (int i = 0;i < end && size != 0; i++)
{
if (i == 0)
{
printf("\n[0x%08X - 0x%08X] ", i, i + 10);
}
if (IsRecoverable(dOffset, i) == true)
{
printf("%s ", "F");
}
else
{
if (szBuf[i] != -1)
{
printf("%s ", "U");
}
else
{
printf("%s ", "F");
}
}
if ((i + 1) % 10 == 0 && i != 0)
{
printf(" ");
for (int j = 0; j < 10; j++)
{
char ch = szBuf[(i - 9) + j];
printf("%c ", ch == -1 ? '.' : ch);
}
if (i + 1 != end)
{
printf("\n[0x%08X - 0x%08X] ", i, i + 10);
}
}
}
printf("\n-----------------------------------------------------------------------\n");
printf("是否碎片整理? (y) : ");
char ch = getchar();
if (ch == 'y')
{
Debris(dOffset, dData);
}
}
/*
碎片整理
*/
void Debris(DTYPE *dOffset, DTYPE *dData)
{
//计数
int nCount = GetDataIndexCount(dOffset);
TAGLINKMAN tagTemp1 = { 0 };
TAGLINKMAN tagTemp0 = { 0 };
//先排序
for (int i = 0; i < nCount; i++)
{
for (int j = 1; j < nCount - i; j++)
{
GetDataIndex(dOffset, j, &tagTemp1);
GetDataIndex(dOffset, j - 1, &tagTemp0);
if (tagTemp0.name.nOffset > tagTemp1.name.nOffset)
{
device_write(dOffset, OFFSETHEAD_INDEX + sizeof(int) + j * sizeof(tagTemp0), &tagTemp0, sizeof(tagTemp0));
device_write(dOffset, OFFSETHEAD_INDEX + sizeof(int) + (j - 1) * sizeof(tagTemp1), &tagTemp1, sizeof(tagTemp1));
}
}
}
//从后往前整理碎片
//先排序
for (int i = 0; i < nCount; i++)
{
if (i == 0) //头
{
GetDataIndex(dOffset, i, &tagTemp1);
if (tagTemp1.name.nOffset > OFFSETTAIL_DATA)
{
//计算数据大小
int nSize = tagTemp1.name.nLen + tagTemp1.phone.nLen;
//分配交换空间
char *swapBuf = (char *) malloc(nSize);
if (swapBuf == NULL)
{
puts("Debris fail!");
getchar();
return;
}
//开始交换
if (device_read(dData, OFFSETSTART_DATA + tagTemp1.name.nOffset, swapBuf, nSize) == true)
{
if (device_write(dData, OFFSETSTART_DATA, swapBuf, nSize) == true)
{
//提交物理写盘
fflush(dData);
//修复分配表
tagTemp1.name.nOffset = OFFSETTAIL_DATA;
tagTemp1.phone.nOffset = tagTemp1.name.nOffset + tagTemp1.name.nLen;
UpdateDateIndex(dOffset, i, &tagTemp1, sizeof(tagTemp1));
}
}
if (swapBuf != NULL)
{
free(swapBuf);
swapBuf = NULL;
}
}
}
else
{
GetDataIndex(dOffset, i, &tagTemp1);
GetDataIndex(dOffset, i - 1, &tagTemp0);
int nOffset0 = tagTemp0.name.nOffset + tagTemp0.name.nLen + tagTemp0.phone.nLen;
//计算空隙
int nInterspace = tagTemp1.name.nOffset - nOffset0;
if (nInterspace > 0)
{
//计算数据大小
int nSize = tagTemp1.name.nLen + tagTemp1.phone.nLen;
//分配交换空间
char *swapBuf = (char *)malloc(nSize);
if (swapBuf == NULL)
{
puts("Debris fail!");
getchar();
return;
}
//开始交换
if (device_read(dData, OFFSETSTART_DATA + tagTemp1.name.nOffset, swapBuf, nSize) == true)
{
if (device_write(dData, OFFSETSTART_DATA + nOffset0, swapBuf, nSize) == true)
{
//提交物理写盘
fflush(dData);
//修复分配表
tagTemp1.name.nOffset = nOffset0;
tagTemp1.phone.nOffset = tagTemp1.name.nOffset + tagTemp1.name.nLen;
UpdateDateIndex(dOffset, i, &tagTemp1, sizeof(tagTemp1));
}
}
if (swapBuf != NULL)
{
free(swapBuf);
swapBuf = NULL;
}
}
}
}
//修复尾巴
int nOffsetTail = OFFSETSTART_DATA + tagTemp1.name.nOffset + tagTemp1.name.nLen + tagTemp1.phone.nLen;
int nSize = GetDataBinSize(dData) + OFFSETSTART_DATA;
fseek(dData, nOffsetTail, SEEK_SET);
for (int i = 0; i < nSize - nOffsetTail; i++)
{
fputc(-1, dData);
}
nOffsetTail = nOffsetTail - 4;
device_write(dData, OFFSETTAIL_DATA, &nOffsetTail, sizeof(int));
//清除回收站
int n = 0;
device_write(dOffset, OFFSETHEAD_RECOVERABLE, &n, sizeof(n));
fflush(dData);
fflush(dOffset);
puts("Debris success!");
getchar();
getchar();
return;
}