ReactOS  0.4.12-dev-945-g5a2911d
deptool.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for deptool.c:

Go to the source code of this file.

Macros

#define ERROR_SUCCESS   0
 
#define ERROR_NOTENOUGHPARAMS   1
 
#define ERROR_DEPENDFILENOTFOUND   2
 
#define ERROR_OUTOFMEMORY   3
 
#define ERROR_READERROR   4
 
#define ERROR_WRITEERROR   5
 

Functions

int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ ERROR_DEPENDFILENOTFOUND

#define ERROR_DEPENDFILENOTFOUND   2

Definition at line 12 of file deptool.c.

◆ ERROR_NOTENOUGHPARAMS

#define ERROR_NOTENOUGHPARAMS   1

Definition at line 11 of file deptool.c.

◆ ERROR_OUTOFMEMORY

#define ERROR_OUTOFMEMORY   3

Definition at line 13 of file deptool.c.

◆ ERROR_READERROR

#define ERROR_READERROR   4

Definition at line 14 of file deptool.c.

◆ ERROR_SUCCESS

#define ERROR_SUCCESS   0

Definition at line 10 of file deptool.c.

◆ ERROR_WRITEERROR

#define ERROR_WRITEERROR   5

Definition at line 15 of file deptool.c.

Function Documentation

◆ main()

int main ( int argc  ,
char argv[] 
)

Definition at line 17 of file deptool.c.

18 {
19  FILE* DependFile;
20  int DependFileSize;
21  char* DependFileData;
22  char* NewDependFileData;
23  int CurIdx;
24  int CurIdx2;
25  int RuleDependencySplit = 0;
26 
27  // Make sure they passed enough command line parameters
28  if (argc < 2)
29  {
30  printf("Usage: deptool srcfile.d\n");
31  return ERROR_NOTENOUGHPARAMS;
32  }
33 
34  // Try to open the dependency file
35  DependFile = fopen(argv[1], "r+t");
36  if (DependFile == NULL)
37  {
38  printf("deptool: No such dependency file: %s\n", argv[1]);
40  }
41 
42  // Get the file size
43  fseek(DependFile, 0, SEEK_END);
44  DependFileSize = ftell(DependFile);
45  rewind(DependFile);
46 
47  // Allocate memory
48  DependFileData = (char *)malloc(DependFileSize);
49  NewDependFileData = (char *)malloc(DependFileSize * 3);
50  if (!DependFileData || !NewDependFileData)
51  {
52  printf("deptool: Out of memory!\n");
53  if (DependFileData != NULL)
54  free(DependFileData);
55  if (NewDependFileData != NULL)
56  free(NewDependFileData);
57  fclose(DependFile);
58  return ERROR_OUTOFMEMORY;
59  }
60  memset(DependFileData, 0, DependFileSize);
61  memset(NewDependFileData, 0, DependFileSize * 3);
62 
63  // Read in file data
64  fread(DependFileData, 1, DependFileSize, DependFile);
65  if (ferror(DependFile))
66  {
67  printf("deptool: Dependency file read error.\n");
68  free(DependFileData);
69  free(NewDependFileData);
70  fclose(DependFile);
71  return ERROR_READERROR;
72  }
73 
74  // Loop through the dependency file data and
75  // insert the rule for the dependency file itself
76  for (CurIdx=0,CurIdx2=0; DependFileData[CurIdx]; CurIdx++,CurIdx2++)
77  {
78  // Find the first colon ':' in the file and insert
79  // the rule right before it
80  if (DependFileData[CurIdx] == ':')
81  {
82  NewDependFileData[CurIdx2] = ' ';
83  CurIdx2++;
84  strcat(&NewDependFileData[CurIdx2], argv[1]);
85  CurIdx2 += strlen(argv[1]);
86  NewDependFileData[CurIdx2] = ' ';
87  CurIdx2++;
88  strcat(NewDependFileData, &DependFileData[CurIdx]);
89  CurIdx2 += 2;
90  RuleDependencySplit = CurIdx + 2;
91  break;
92  }
93  else
94  {
95  NewDependFileData[CurIdx2] = DependFileData[CurIdx];
96  }
97  }
98 
99  // Now loop through all the rule dependencies and
100  // turn them into rules themselves
101  strcat(NewDependFileData, "\n\n");
102  CurIdx = RuleDependencySplit;
103  CurIdx2 = strlen(NewDependFileData);
104  for (; DependFileData[CurIdx]; CurIdx++,CurIdx2++)
105  {
106  // If it's a line continuation char '\' then skip over it
107  if (DependFileData[CurIdx] == '\\')
108  {
109  CurIdx2--;
110  continue;
111  }
112 
113  // If it's a new line char '\n' then insert a colon ':' to make it a rule
114  if (DependFileData[CurIdx] == '\n')
115  {
116  NewDependFileData[CurIdx2] = ':';
117  CurIdx2++;
118  }
119 
120  NewDependFileData[CurIdx2] = DependFileData[CurIdx];
121  }
122 
123  // Write out file data
124  rewind(DependFile);
125  fwrite(NewDependFileData, 1, strlen(NewDependFileData), DependFile);
126  if (ferror(DependFile))
127  {
128  printf("deptool: Dependency file write error.\n");
129  fclose(DependFile);
130  free(DependFileData);
131  free(NewDependFileData);
132  return ERROR_WRITEERROR;
133  }
134 
135  fclose(DependFile);
136  free(DependFileData);
137  free(NewDependFileData);
138  return ERROR_SUCCESS;
139 }
static int argc
Definition: ServiceArgs.c:12
_Check_return_ _CRTIMP int __cdecl ferror(_In_ FILE *_File)
#define ERROR_SUCCESS
Definition: deptool.c:10
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define free
Definition: debug_ros.c:5
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
#define argv
Definition: mplay32.c:18
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
smooth NULL
Definition: ftsmooth.c:416
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
_CRTIMP void __cdecl rewind(_Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
#define ERROR_DEPENDFILENOTFOUND
Definition: deptool.c:12
#define ERROR_NOTENOUGHPARAMS
Definition: deptool.c:11
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
#define SEEK_END
Definition: cabinet.c:27
#define malloc
Definition: debug_ros.c:4
#define ERROR_WRITEERROR
Definition: deptool.c:15
#define ERROR_READERROR
Definition: deptool.c:14
#define memset(x, y, z)
Definition: compat.h:39
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
#define printf
Definition: config.h:203